我正在尝试实现从后台脚本传递到内容脚本的消息
的manifest.json
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts" : [
{
"matches" : [ "http://*/*" ],
"js": ["hello.js"],
"css": [ "hello.css" ]
}
background.js
function showpopup()
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
chrome.alarms.onAlarm.addListener(showpopup);
内容脚本js又名hello.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"});
});
我在(未知)事件处理程序中收到错误错误:TypeError:无法读取未定义属性'告别'
我已经广泛使用了StackOverflow并且还实现了this solution,但无济于事。