尝试在内容脚本和后台脚本之间进行通信时,出现以下错误:
Port error: Could not establish connection. Receiving end does not exist.
Error in event handler for 'undefined': Cannot read property 'message' of undefined
TypeError: Cannot read property 'message' of undefined
背景.js
function onRequest(request, sender, callbackFunction) {
console.log("Me (BS) became this Message:" + request.message);
sendResponse({message: request.message})
};
chrome.extension.onRequest.addListener(onRequest);
流云.js
function contactBackground(nachricht){
chrome.extension.sendMessage({message: nachricht}, function(response) {
console.log("The Background Script got the following Message: " + response.message);
});
}
和我的manifest.json
{
"name": "InstantWatch - Dev",
"manifest_version": 2,
"version": "0.7",
"permissions": ["tabs", "http://*/", "https://*/"],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"default_title": "InstantWatch",
"default_icon" : "icon.ico"
},
"content_scripts": [
{
"matches": ["http://*/*", "http://*/*"],
"js": ["jquery.js", "streamcloud.js"]
}
]
}
我找到了添加一个带有空 background.html 的 background_page: "background.html" 的解决方案,但由于自 manifest_version: 2 以来不支持 background_page,我不能使用它。