Chrome扩展程序中显示“未定义同步”错误

问题描述 投票:0回答:2

同步未定义“在使用存储权限时在chrome扩展中显示错误。请确认错误在哪里以及如何解决消息:”未定义同步“stack:”ReferenceError:未在eval定义同步(eval at( http://localhost:46950/app/popup.js:31:9),:1:1)↵在HTMLInputElement。 (http://localhost:46950/app/popup.js:31:9)↵在HTMLInputElement.dispatch(http://localhost:46950/app/img/jquery-3.2.1.min.js:6:12417)↵在HTMLInputElement.q.handle

//manifest.json
    {
  "manifest_version": 2,     

  "name": "One-click ",
  "description": "This extension demonstrates a browser .",
  "version": "1.0",
   "app": {
    "launch": {
      "local_path": "popup.html"
    }

  },
  "browser_action": {
    "default_icon": "img/icon.png",
    "default_popup": "popup.html"
  },
  "background": {
    "scripts": [ "popup.js", "img/jquery-3.2.1.min.js" ],
    "persistent": false
  },
  "content_scripts": [
    {
      "css": [ "img/bootstrap.min.css"],
      "js": [ "app/popup.js", "img/jquery-3.2.1.min.js" ],
      "matches": [ "http://*/*", "https://*/*" ]
    }
  ],
   "permissions": [
    "activeTab",
    "storage",
    "tabs",
    "identity"
  ]
}

//popup.js
     chrome.storage.sync.set({ 'data':"helloset" }, function () {
            alert("saved");

        });
google-chrome google-chrome-extension google-chrome-app
2个回答
0
投票

*method* is not defined errors通常意味着在库尚未加载时调用该方法,或者实际上没有这样的方法。在你的情况下,我认为这是前者。查看IzumiSy/manifest.json同步示例,首先需要使用document.body.onload然后调用该函数:

// popup.js
document.body.onload = function() {
  chrome.storage.sync.get("data", function(items) {
    if (!chrome.runtime.error) {
      console.log(items);
      document.getElementById("data").innerText = items.data;
    }
  });
}

0
投票

如果您需要在本地保存数据,可以使用

document.body.onload = function() {
  chrome.storage.sync.get("data", function(items) {
    if (!chrome.runtime.error) {
      console.log(items);
      document.getElementById("data").innerText = items.data;
    }
  });
}

document.getElementById("set").onclick = function() {
  var d = document.getElementById("text").value;
  chrome.storage.sync.set({ "data" : d }, function() {
    if (chrome.runtime.error) {
      console.log("Runtime error.");
    }
  });
  window.close();
}
© www.soinside.com 2019 - 2024. All rights reserved.