如何解决Chrome扩展程序:端口错误:无法建立连接接收端不存在
sendMessage
并且onRequest
是 。
如果您需要支持 ,请使用 和 :
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
// Warning: Chrome 19- [receiver]
});
chrome.extension.sendRequest(message, optional_sendResponse);
对于 ,请使用和:chrome.extension. **onMessage**``chrome.extension.
**sendMessage**
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
// Chrome 20+
});
chrome.extension.sendMessage(message, optional_sendResponse);
对于 ,请使用和。chrome **.runtime.**
onMessage
chrome **.runtime.**
sendMessage
注意:从Chrome 26开始,仍支持不推荐使用的方法,尽管未记录。如果有机会,请更新您的扩展程序以使用新方法,以确保您的扩展程序将来仍然可以使用。
解决方法
尝试在内容脚本和后台脚本之间进行通信时,出现以下错误:
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
background.js
function onRequest(request,sender,callbackFunction) {
console.log("Me (BS) became 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 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_page:“
background.html”和一个空白background.html的解决方案,但是由于manifest_version:2不支持background_page,所以我不能使用它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。