Javascript chrome.storage不工作

Javascript chrome.storage不工作,javascript,google-chrome,google-chrome-app,Javascript,Google Chrome,Google Chrome App,我有一个谷歌chrome应用程序,我尝试使用chrome.storage.sync.get,它说,“无法读取未定义的属性sync”。有人能告诉我发生了什么事吗?我试着从chrome开发者网站上复制并粘贴这一行 代码字面上是: $(".thing").click(function() {chrome.storage.sync.set(stuff)} 据我所知,错误只是我试图在Google chrome中使用chrome存储API,而不是作为应用程序使用。请在使用chrome.storage.sy

我有一个谷歌chrome应用程序,我尝试使用chrome.storage.sync.get,它说,“无法读取未定义的属性sync”。有人能告诉我发生了什么事吗?我试着从chrome开发者网站上复制并粘贴这一行

代码字面上是:

$(".thing").click(function() {chrome.storage.sync.set(stuff)}

据我所知,错误只是我试图在Google chrome中使用chrome存储API,而不是作为应用程序使用。

请在使用
chrome.storage.sync

  • 在清单中声明存储权限
  • manifest.json

    {
      "manifest_version": 2,
    
      "name": "Storage",
      "description": "This extension shows a Google Image search result for the current page",
      "version": "1.0",
      "icons":{
    "256":"img/icon.png"
        },
      "permissions": [
        "storage"
      ],
      "app": {
        "launch": {
          "local_path": "options.html"
        }
    
      },
       "background": {
        "scripts": ["js/app/background.js"]
      }
    
    }
    
  • chrome://extensions
    以便在浏览器中更新manifest.json

  • 使用其回调函数遵循chrome.storage.sync的确切语法

  • Sample.js

    $(".thing").click(function() {
          chrome.storage.sync.set({"myKey": "testPrefs"},function(){
           alert("object stored");
    })
        chrome.storage.sync.get("myKey",function(obj){
            alert(obj.myKey);
        })
    });
    

    这对你有用。我在我的chrome应用程序中测试了这段代码。

    结果是我试图在google chrome(浏览器)中运行这段代码,而不是作为chrome应用程序运行。Chrome不知道应该将代码作为应用程序进行解析,并且忽略了关于
    Chrome.storage
    的一行。值得注意的是,chrome开发者

    您是否已在中声明存储权限?@ProfessorAllman Yes这些答案对您有帮助吗?没有足够的上下文。这个问题是幸运猜测比赛。我猜您是从一个DOM注入脚本访问
    chrome.storage
    ,这是行不通的。@wOxxOm我也有同样的问题,那么如何将变量从options.js传递到script.js呢?谢谢