chrome.storage.sync undefined?

Yos*_*ale 43 google-chrome-extension google-chrome-storage

我试图通过content_script在扩展中使用chrome存储,但我一直在失败

Uncaught TypeError: Cannot read property 'sync' of undefined 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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);
    });
}
Run Code Online (Sandbox Code Playgroud)

sfa*_*ota 90

您必须在manifest.json文件中添加"storage"权限,即:

...
  "permissions": [
    "storage"
  ],
...
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅:https://developer.chrome.com/extensions/storage

  • google总是这样做...使帮助页面变得无用...他们为什么不提起这里的存储许可?:https://developer.chrome.com/extensions/options (4认同)
  • 谢谢,这是我相信的实际正确答案..不需要消息传递 (2认同)

Rah*_*ngh 7

重新加载扩展

我在manifest文件中添加了“ permissions”键,但仍然很难解决此问题。

添加权限后:

"permissions": [
    "storage"
 ]
Run Code Online (Sandbox Code Playgroud)

使用以下网址转到您的扩展程序:chrome:// extensions /,然后点击重新加载按钮:-

在此处输入图片说明

  • 关键在这里...重新加载扩展 (4认同)