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

时间:2017-12-28 13:09:42

标签: google-chrome google-chrome-extension google-chrome-app

  

同步未定义“在使用存储权限时在chrome扩展中显示错误。请确认错误的位置以及方式   将解决消息:“未定义同步”堆栈:“ReferenceError:   同步未在eval中定义(eval at   (http://localhost:46950/app/popup.js:31:9),:1:1)↵at   HTMLInputElement。   (http://localhost:46950/app/popup.js:31:9)↵at   HTMLInputElement.dispatch   (http://localhost:46950/app/img/jquery-3.2.1.min.js:6:12417)↵at   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");

        });

2 个答案:

答案 0 :(得分: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;
    }
  });
}

答案 1 :(得分: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();
}