【问题标题】:chrome.storage not workingchrome.storage 不工作
【发布时间】:2016-12-26 08:02:21
【问题描述】:

我有一个谷歌浏览器应用程序,我尝试使用 chrome.storage.sync.get,它说“无法读取未定义的属性同步”。有人可以告诉我发生了什么吗?我已尝试从 chrome 开发者网站复制并粘贴确切的行。

代码字面意思是:

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

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

【问题讨论】:

  • 您是否在manifest 中声明了存储权限?
  • @ProfessorAllman 是的
  • 这些答案对您有帮助吗? stackoverflow.com/q/14657833/2033671
  • 上下文不足。这个问题是幸运猜谜比赛。我的猜测是您正在从 DOM 注入脚本访问 chrome.storage,这是行不通的。
  • @wOxxOm 我有同样的问题,那么如何将变量从 options.js 传递给 script.js?谢谢

标签: javascript google-chrome google-chrome-app


【解决方案1】:

请在使用chrome.storage.sync时按照这些步骤操作

  1. 在清单中声明存储权限。

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"]
  }

}
  1. chrome://extensions 中重新加载您的应用,以便您的 manifest.json 在您的浏览器中得到更新。

  2. 遵循 chrome.storage.sync 的确切语法及其回调函数。

示例.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 应用中测试了这段代码。

【讨论】:

    【解决方案2】:

    事实证明,我试图在 google chrome(浏览器)中运行它,而不是作为 chrome 应用程序。 Chrome 不知道它应该将代码解析为应用程序,并忽略了关于chrome.storage 的行。 chrome 开发者需要注意的地方!

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 1970-01-01
      • 2013-01-14
      • 2017-04-23
      • 2020-03-16
      • 2013-10-21
      • 2018-05-04
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多