我有一个chrome扩展名,其中包含以下清单(部分)
"content_scripts": [
{
"matches": ["https://plus.google.com/*"],
"js": ["jquery-1.10.2.min.js","filter.js"]
}
],
"options_page": "settings.html",
我成功地从“settings.js”中读取和写入localstorage中的设置,该设置是从settings- page“settings.html”调用的
现在我需要从内容脚本“filter.js”
访问这些值我按照http://developer.chrome.com/extensions/messaging.html创建“settings.js”监听器和“filter.js”请求的示例:
settings.js:
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
filter.js:
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"});
});
它看起来不像是听众,请求可以“找到对方”。
我在控制台上的所有内容都是:
Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined
at chrome-extension://opojapnnibghjncfphindgjhddljcgej/settings.js:30:23
at extensions::messaging:320:11
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.dispatchToListener (extensions::event_bindings:386:22)
at Event.dispatch_ (extensions::event_bindings:371:27)
at Event.dispatch (extensions::event_bindings:392:17)
at dispatchOnDisconnect (extensions::messaging:280:27)
我做错了什么?
答案 0 :(得分:0)
通过在清单中添加一个事件页面来解决这个问题:
"background": {
"scripts": ["loader.js"],
"persistent": false
},
我放置了侦听器,并从选项脚本和内容脚本中调用了这些侦听器。