Javascript Chrome 扩展:端口错误:无法建立连接。接收端不存在。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11811554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:11:43  来源:igfitidea点击:

Chrome Extension: Port error: Could not establish connection. Receiving end does not exist.

javascriptgoogle-chromegoogle-chrome-extension

提问by Gumble

When trying to communicate between my Content- and Background Script I get the following errors:

尝试在我的内容脚本和后台脚本之间进行通信时,出现以下错误:

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

背景.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

流云.js

function contactBackground(nachricht){
    chrome.extension.sendMessage({message: nachricht}, function(response) {
        console.log("The Background Script got the following Message: " + response.message);
    });
}

and my manifest.json

和我的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"]
    }
  ]
}

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.

我找到了添加 background_page: "background.html" 和一个空的 background.html 的解决方案,但是由于从 manifest_version: 2 起不支持 background_page,我不能使用它。

回答by Rob W

sendMessageand onRequestare not compatible.

sendMessage并且onRequest不兼容

If you need to support Chrome 19 and earlier, use onRequestand sendRequest:

如果您需要支持Chrome 19 及更早版本,请使用onRequestsendRequest

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    // Warning: Chrome 19- [receiver]
});
chrome.extension.sendRequest(message, optional_sendResponse);


For Chrome 20 - 25, use chrome.extension.onMessageand chrome.extension.sendMessage:

对于Chrome 20-25,使用和:chrome.extension.onMessagechrome.extension.sendMessage

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    // Chrome 20+
});
chrome.extension.sendMessage(message, optional_sendResponse);


For Chrome 26+, use chrome.runtime.onMessageand chrome.runtime.sendMessage.

对于Chrome 26+,使用和。chrome.runtime.onMessagechrome.runtime.sendMessage



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.
See this answerfor code to create a which is compatible with Chrome 20+.

注意:从 Chrome 26 开始,仍然支持已弃用的方法,尽管没有记录。如果有机会,请更新您的扩展程序以使用新方法,以确保您的扩展程序将来仍能正常工作。
有关创建与 Chrome 20+ 兼容的代码的答案,请参阅此答案

回答by Udbhav

Instead of

代替

chrome.extension.onRequest.addListener(onRequest);

Use

chrome.extension.onMessage.addListener(onRequest);

Since you are using sendMessage and not sendRequest.

由于您使用的是 sendMessage 而不是 sendRequest。

Message parsing has been updated in the new version of Chrome. sendRequest and onRequest are being deprecated. It is recommended to go with sendMessage and onMessage.

新版 Chrome 更新了消息解析。sendRequest 和 onRequest 正在被弃用。建议使用 sendMessage 和 onMessage。

Refer docs for message parsing between Content Script and Background.

请参阅文档以了解Content Script 和 Background 之间的消息解析