我实际上是在尝试通过使用localStorage
后台脚本中的对象来为我的 chrome 扩展获取持久存储。
这是设置:
我有一个背景脚本和一个内容脚本。内容脚本需要从扩展的localStorage
.
但是,我被困住了要发送到后台脚本的消息。
这是后台脚本中的代码:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
和内容脚本:
setTimeout(function () {
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
}, 5000);
理想情况下,后台脚本应该输出from a content script: [some url]
,内容脚本应该输出goodbye
。相反,我收到以下错误:
Port: Could not establish connection. Receiving end does not exist.
任何帮助/建议表示赞赏!
谢谢 :)
编辑:清单
{
"manifest_version": 2,
"name": "Helios",
"description": "Helios chrome extension.",
"version": "0.0.1",
"permissions": [
"https://secure.flickr.com/",
"storage"
],
"background": {
"page": "background.html"
},
"browser_action": {
"default_icon": {
"19": "img/icon-19.png",
"38": "img/icon-38.png"
},
"default_title": "Helios",
"default_popup": "popup.html"
},
"content_scripts":[
{
"matches": ["*://*/*"],
"css": ["css/main.css"],
"js": [
"js/vendor/jquery-1.9.1.js",
"js/vendor/handlebars-1.0.0-rc.4.js",
"js/vendor/ember-1.0.0-rc.7.js",
"js/vendor/d3.v3.js",
"js/vendor/socket.io.min.js",
"js/templates.js",
"js/main.js"
],
"run_at": "document_end"
}
],
"web_accessible_resources": [
"img/**",
"fonts/**"
]
}
编辑:Chrome版本
Google Chrome 29.0.1547.65 (Official Build 220622)
OS Mac OS X
Blink 537.36 (@156661)
JavaScript V8 3.19.18.19
Flash 11.8.800.170
User Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36