google-chrome-extension - 为什么 chrome.storage.local.get 返回 undefined 的值?
问题描述
我正在开发谷歌扩展并使用谷歌存储。
下面是我保存和访问已保存数据的代码。我检查了控制台,数据保存正确。但是,当我使用 chrome.storage.local.get 访问此数据时,控制台返回未定义的值而不是保存的值。
保存代码:popup.html 的 save.js
function save() {
# the id is for a textarea in another html page
var text = document.getElementById("text").value;
chrome.storage.sync.set({"txt": text}, function(){
# console states the correct value
console.log('Value is set to ' + text);
});
# this is the page I want to retrieve the saved data in
window.location.replace("anotherHTMLpage.html");
}
访问代码:retrieve.js for anotherHTMLpage.html
function retrieve() {
chrome.storage.sync.get(["txt"], function(data) {
# console says "value currently is undefined"
console.log('Value currently is ' + data.key);
});
}
我在这里先向您的帮助表示感谢!
解决方案
Chrome 将数据保存在您设置的密钥下。在您的示例中,您将其保存为txt
,因此它将位于data.txt
not之下data.key
。
查看文档以获取更多详细信息https://developer.chrome.com/docs/extensions/reference/storage/#usage
推荐阅读
- r - 提取R中两个符号之间的子字符串
- c++ - 类过程中的 OpenGL 空白屏幕
- r - 将 geom_point 更改为渐变填充形状/多边形
- security - 检测到请求获取我的网络服务器的假用户代理(又名刮板)?
- aws-lambda - Serilog CloudWatchSink 不适用于 Lambda 函数
- javascript - regex to mask phone number such that except first 2 and last 2 characters in a number are replaced with * in javascript
- jmeter - Opco 的 Jmeter 脚本
- apache-spark - 根据另一个表spark sql计算每一行
- ejs - 如何阻止 csslint 和 jshint 在 Atom 中对我的 EJS 进行 linting?
- openstack - Openstack HEAT:通过 HEAT 模板手动将新实例添加到现有堆栈