我试图通过 content_script 在扩展中使用 chrome 存储,但我一直失败
未捕获类型错误:无法读取未定义的属性“同步”
这是我的代码:
testChromeStorage();
function testChromeStorage() {
console.log("Saving");
chrome.storage.sync.set({'value': theValue}, function() {
message('Settings saved');
});
chrome.storage.sync.get("value", function (retVal) {
console.log("Got it? " + retVal.value);
});
}
您必须在manifest.json文件中添加“存储”权限,即:
//...
"permissions": [
"storage"
],
//...
如果有人在 Firefox 上遇到此问题,请注意,尚不支持该问题:
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage#Chrome_incompatibilities
就我的目的而言,用
chrome.storage.sync
替换 chrome.storage.local
就足够了。
关于 Firefox 的实现状态,有时也值得看看这里:
重新启动开发服务器
尽管添加了权限并重新加载扩展程序,但我的同步仍然未定义。
结果我需要重新启动开发服务器才能获取新的清单。 这是有道理的,因为该文件没有被观看。 嚯!
请参阅 https://developer.chrome.com/extensions/content_scripts.html:
但是,内容脚本有一些限制。他们不能:
使用 chrome。 API(部分 chrome.extension 除外)*
(强调)