Javascript 在Chrome扩展中设置本地存储

Javascript 在Chrome扩展中设置本地存储,javascript,google-chrome,google-chrome-extension,local-storage,Javascript,Google Chrome,Google Chrome Extension,Local Storage,我使用以下代码在Chrome扩展中设置本地存储 localStorage.setItem('userkey','123'); /* I've also tried this: chrome.storage.sync.set({'userkey': 123}, function() { // Notify that we saved. message('Settings saved'); }); */ manifest.json: {

我使用以下代码在Chrome扩展中设置本地存储

localStorage.setItem('userkey','123');

/* I've also tried this:
chrome.storage.sync.set({'userkey': 123}, function() {
          // Notify that we saved.
          message('Settings saved');
        });
*/
manifest.json:

{
  "manifest_version": 2,

  "name": "Shopaholic",
  "description": "All the latest sale items from your favourite clothes shops right on your home screen",
  "version": "1.0",

  "chrome_url_overrides": {
    "newtab": "index.html"
  },

"content_scripts": [
    {
      "matches": ["*://*/index.html"],
      "css": ["css/style.css"],
      "js": ["js/jquery.min.js"]

    }
  ],

 "options_page": "options.html",

"permissions": [
          "storage"
        ]
}
但是,我遇到了以下错误:

未捕获的安全性错误:未能读取“localStorage”属性 从“窗口”:文档是沙盒,缺少 “允许相同来源”标志

如果我使用上面注释掉的代码,我会得到:

未捕获的TypeError:无法读取未定义的属性“sync”

我哪里出错了?

读这篇-

基本上,这是一个web应用程序安全模型概念

所以,为了避免这种情况,你需要在谷歌浏览器上禁用同源策略

所以,如果在桌面上,用这个命令从命令行打开chrome

chrome.exe --disable-web-security

然后运行你的脚本,它不应该给出这个错误

谢谢,在
manifest.json
permissions中没有办法绕过这个问题吗?否则,扩展的每个用户都必须在命令行中禁用同一源策略。您可以将其添加到清单--“权限”:[“”,“存储”]我的域是什么?我尝试了index.html,但它没有改变任何内容。如果您在本地托管它,那么“localhost”会添加类似“permissions”的内容:[“localhost”]您是否使用html文档覆盖newtab,然后将内容脚本注入该html文档?为什么不像普通的HTML标记那样直接包含内容脚本文件呢?您能否更具体地说明与localStorage交互的位置/内容?从我的manifest.json可以看出,我正在用一个名为index.html的文件覆盖新的选项卡页面。我正在尝试将localStorage作为登录脚本的一部分来存储用户密钥,可以检查该密钥以查看用户是否登录。