我开始学习在Chrome中调试扩展程序。我在Chrome中遇到此问题但在Firefox中我们没有问题。问题是在Chrome中我无法获取已保存到存储空间的数据。
我在后台脚本中有这个功能。我将断点添加到storage.set的行。我重新启动了addon,我可以访问MyNamespace.data中的所有数据...插件会打印安装成功的通知,但是当我尝试获取数据时,日志会显示空对象。
MyNamespace.checkIfExists = function (item) {
if (chrome.runtime.lastError) {
MyNamespace.notify("check-fail");
console.log("error:" + chrome.runtime.lastError);
console.log(chrome.runtime.lastError);
} else {
if ( ! ( "initiated" in item ) )
{
console.log("I'm initiating .. item:");
console.log(item);
/* Insert new data object */
/* This saves default values into storage */
chrome.storage.local.set({
easy: MyNamespace.data.easy,
advanced: MyNamespace.data.advanced,
images: MyNamespace.data.images,
inspector:MyNamespace.data.inspector,
extractor:MyNamespace.data.extractor,
profiles: MyNamespace.data.profiles,
default_profiles: MyNamespace.data.default_profiles
},
() => {
if (chrome.runtime.lastError)
{
MyNamespace.notify("write-fail");
console.log("local.set Error: " + chrome.runtime.lastError); }
else
{
MyNamespace.notify("write-succeess");
chrome.storage.local.get( {initiated: undefined, easy:undefined},
(result) => {
if (typeof result === undefined )
MyNamespace.notify("write-fail");
console.log("found:");
console.log(result);
console.log("/end");
}
)
chrome.storage.local.set( {initiated:true} );
MyNamespace.initiated = true;
}
} );
}
else
MyNamespace.initiated = true;
}
};
控制台中的结果:
I'm initiating .. item:
namespace.js:125 Object {}
namespace.js:149 found:
namespace.js:150 Object {}
namespace.js:151 /end
控制台没有错误。在Chrome中,对象为空。在Firefox中,我看到一个要保存的对象,一个属性未定义。
的manifest.json
{
"applications": {
"gecko": {
"id": "some-options@mozilla.org",
"strict_min_version": "48.0a1"
}
},
"description": "An example options ui",
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/favourite-colour",
"manifest_version": 2,
"name": "Favourite colour",
"version": "1.0",
"icons": {
"48": "icons/eye-icon-colored-48.png"
},
"options_ui": {
"page": "options.html",
"open_in_tab": true
},
"browser_action": {
"default_title": "Adapt page",
"browser_style": true,
"default_popup": "popup/settings.html",
"default_icon": {
"48": "icons/eye-icon-48.png"
}
},
"web_accessible_resources": [
"icons/hotkeys-icon-32.png"
],
"permissions": ["storage", "tabs", "activeTab", "notifications"],
"background": {
"page": "background_scripts/background_scripts_bug_fix.html"
},
"content_scripts": [
{
"matches": ["<all_urls>" ],
"run_at": "document_start",
"js": [
"content_scripts/jquery-3.0.0.js",
"content_scripts/namespace.js",
"content_scripts/easy.js",
"content_scripts/addListeners.js"
]
}
],
"commands": {
"easy": {
"suggested_key": {
"default": "Ctrl+Shift+U"
},
"description": ""
}
}
}
我已添加此侦听器,以便在保存存储数据时触发:
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
console.log('Storage key "%s" in namespace "%s" changed. ' +
'Old value was "%s", new value is "%s".',
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
}
});
我得到了这个日志:
namespace.js:241 Storage key "default_profiles" in namespace "local" changed. Old value was "Object", new value is "undefined".
namespace.js:241 Storage key "easy" in namespace "local" changed. Old value was "Object", new value is "undefined".
namespace.js:241 Storage key "extractor" in namespace "local" changed. Old value was "Object", new value is "undefined".
namespace.js:241 Storage key "images" in namespace "local" changed. Old value was "Object", new value is "undefined".
namespace.js:241 Storage key "initiated" in namespace "local" changed. Old value was "true", new value is "undefined".
namespace.js:241 Storage key "inspector" in namespace "local" changed. Old value was "Object", new value is "undefined".
namespace.js:241 Storage key "profiles" in namespace "local" changed. Old value was "Object", new value is "undefined".
namespace.js:124 I'm initiating .. item:
namespace.js:125 Object {}
namespace.js:241 Storage key "advanced" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:241 Storage key "default_profiles" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:241 Storage key "easy" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:241 Storage key "extractor" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:241 Storage key "images" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:241 Storage key "inspector" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:241 Storage key "profiles" in namespace "local" changed. Old value was "undefined", new value is "Object".
namespace.js:152 found:
namespace.js:153 Object {}
namespace.js:154 /end
namespace.js:241 Storage key "initiated" in namespace "local" changed. Old value was "undefined", new value is "true".
那是什么意思?我在Chrome中看到了单词Object(),白色字母(黑暗主题),但它不像我可以点击并查看对象的链接。这表明数据已加载。但为什么我不能在对象中观察数据?
我已使用此代码检查了它: for(结果中的var键) if(hasOwnProperty.call(result,key)) console.log(&#34; key:&#34; + key);
在Chrome中没有列出任何属性,在Firefox工具箱/控制台中列出了它们。
据我所知,storage.set回调应该在保存更改完成后触发,并关闭db文件进行写入。所以我希望在storage.set
中解冻storage.get不存在任何问题有人可以澄清这里发生了什么吗?为什么在Firefox中这有效,但在Chrome中没有?如何解决问题?
答案 0 :(得分:2)
chrome.storage.local.get({initiated:undefined,easy:undefined},
将不会得到任何结果,因为只使用参数对象的中间字符串化检索定义的键(1,2):self = this;
生成class MessageList extends React.Component {
constructor(props){
super(props);
this.state = {
messages: []
};
componentDidMount() {
var self = this;
var firebaseRef = firebase.database().ref();
firebaseRef.once('value')
.then(function(dataSnapshot) {
self.setState({
messages: messages
});
});
}
}
render() { ... }
}
- 它&#39; s空。
当您不提供默认值时,请使用一组键名:
JSON.stringify({a:undefined,b:undefined})
P.S。使用console.log
format string中的'{}'
或chrome.storage.local.get( ['initiated', 'easy'],
来显示对象的实际内容。