问题描述
尝试在我的内容脚本和后台脚本之间进行通信时,出现以下错误:
端口错误:无法建立连接。接收结束不存在。
'undefined'的事件处理程序错误:无法读取未定义的属性'message'$ b $ TypeError:无法读取未定义的属性'message'
$ b $ $ strong> background.js 函数onRequest (request,sender,callbackFunction){
console.log(Me(BS)become this Message:+ request.message);
sendResponse({message:request.message})
};
chrome.extension.onRequest.addListener(onRequest);
streamcloud.js
function contactBackground(nachricht){
chrome.extension.sendMessage({message:nachricht},function(response){
console.log(The后台脚本得到以下消息:+ response.message);
});
}
和我的 manifest.json
$ b
{
name:InstantWatch - Dev,
manifest_version:2,
版本:0.7,
权限:[tabs,http:// * /,https:// * /],
background:{
scripts:[background.js]
},
browser_action:{
default_title:InstantWatch,
default_icon:图标.ico
,
content_scripts:[
{
matches:[http:// * / *,http:// * / * ],
js:[jquery.js,streamcloud.js]
}
]
}
我找到了添加background_page的解决方案:background.html,其中有一个空的background.html,但由于自manifest_version以来不支持background_page:2 ,我无法使用它。
sendMessage
和 onRequest
不兼容。
如果y您需要支持 Chrome 19及更早版本,使用 onRequest
和 sendRequest
:
chrome.extension.onRequest.addListener(function(request,sender,sendResponse ){
//警告:Chrome 19- [接收器]
});
chrome.extension.sendRequest(message,optional_sendResponse);
对于 Chrome 20 - 25 chrome.extension。 onMessage 和 对于 Chrome 26 + ,请使用和。 When trying to communicate between my Content- and Background Script I get the following errors: background.js streamcloud.js and my manifest.json I found the solution to add an background_page: "background.html" with an empty background.html, but since background_page isn't supported since manifest_version: 2, I can't use that. If you need to support Chrome 19 and earlier, use For Chrome 20 - 25, use For Chrome 26+, use Note: As of Chrome 26, the deprecated methods are still supported, albeit undocumented. If you get a chance, update your extension to use the new methods, to ensure that your extension will still work in the future. 这篇关于Chrome扩展程序:端口错误:无法建立连接。接收结束不存在。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! chrome.extension。 sendMessage
$ b $ pre $ chrome.extension.onMessage.addListener(function(request,sender,sendResponse){
// Chrome 20+
});
chrome.extension.sendMessage(message,optional_sendResponse);
注意:从Chrome 26开始,已弃用的方法仍然受支持,虽然没有记录。如果您有机会,请更新您的扩展程序以使用新方法,以确保您的扩展程序将来仍能正常运行。
请参阅,以便创建与Chrome 20 +兼容的代码。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
function onRequest(request, sender, callbackFunction) {
console.log("Me (BS) became this Message:" + request.message);
sendResponse({message: request.message})
};
chrome.extension.onRequest.addListener(onRequest);
function contactBackground(nachricht){
chrome.extension.sendMessage({message: nachricht}, function(response) {
console.log("The Background Script got the following Message: " + response.message);
});
}
{
"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"]
}
]
}
sendMessage
and onRequest
are not compatible.onRequest
and sendRequest
:chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
// Warning: Chrome 19- [receiver]
});
chrome.extension.sendRequest(message, optional_sendResponse);
chrome.extension.onMessage
and chrome.extension.sendMessage
:chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
// Chrome 20+
});
chrome.extension.sendMessage(message, optional_sendResponse);
chrome.runtime.onMessage
and chrome.runtime.sendMessage
.
See this answer for code to create a which is compatible with Chrome 20+.