端口错误:无法建立连接.接收端不存在.在Chromiume

Lan*_*ton 52 google-chrome-extension

我正在开发一个Chrome扩展程序,这是一个问题.在我inject.js,我提出的请求如下:

chrome.extension.sendRequest({command:'skip'},callback)
Run Code Online (Sandbox Code Playgroud)

在我的`background.js中,我只是添加一个请求监听器,如:

chrome.extension.onrequest.addListener(function(req,sender,res){console.log("procession"})
Run Code Online (Sandbox Code Playgroud)

但是有一个错误:

端口错误:无法建立连接.接收端不存在

这似乎是Chrome中的一个bug?PS:
我的manifest.json的一部分

"background": {
    "scripts": ["background.js"]
  },
  "content_scripts": [
    {
      "matches": ["&lt all_urls &gt"], 
      "js": ["inject.js"]
    }
  ],
Run Code Online (Sandbox Code Playgroud)

我在Chromium 17,我尝试重新加载扩展程序,重新打开浏览器......没有发生任何事情,
有人得到一些想法?

Nei*_*eil 22

这并不总是原因,但如果您有错误background.js,请检查以下链接:

Inspect views: _generated_background_page.html
Run Code Online (Sandbox Code Playgroud)

在"扩展"页面中,它将显示任何JavaScript错误.

这阻止了我的联系建立.

  • 这是正确的答案,上面的内容无效. (3认同)
  • 看起来当background.js中有错误时,调用将失败。 (2认同)

Jan*_*nes 13

问题可能是sendRequest()并且onRequest已被弃用并替换为sendMessage()onMessage.自从最近的Chrome 20更新以来,它们似乎完全消失了.

消息传递的官方文档甚至sendRequest()不再提及.

这是一个记录变化的链接:http://codereview.chromium.org/9965005/


Nic*_*oso 12

这里的其他一些答案有很好的调试建议或小块拼图,但是如果你想像我一样注入(3rd 方)网页,那么这里没有列出正确组件的答案。

有4个步骤:

  • 一个外部背景监听器,
  • 前台消息发送者,
  • 为消息发送者注入扩展 ID
  • 清单中的规则将所有内容链接在一起。

下面的示例应该是允许您注入 js 所需的一切,该 js 将消息从 google.com 上的任何页面发送到您自己的扩展程序

首先在manifest.json 中,您需要添加此处描述的消息传递规则:

"externally_connectable": {
    "matches": ["*://*.google.com/*"]
}
Run Code Online (Sandbox Code Playgroud)

然后在您的后台脚本或页面中,您需要一个外部侦听器不是msot 答案中提到的常规 chrome.runtime.onMessage.addListener)此处描述

chrome.runtime.onMessageExternal.addListener( (request, sender, sendResponse) => {
    console.log("Received message from " + sender + ": ", request);
    sendResponse({ received: true }); //respond however you like
});
Run Code Online (Sandbox Code Playgroud)

拥有这些部分后,您可以像往常一样使用消息发送器,但将扩展名 id作为第一个参数:

chrome.runtime.sendMessage(myExtId, { /* whatever you want to send goes here */ },
    response => {
         /* handle the response from background here */
    }
);
Run Code Online (Sandbox Code Playgroud)

如果您不知道如何获取我用作第一个参数的外部 ID,您可以像下面一样注入您的扩展 ID。这是必需的,因为chrome.runtime.id@@extension_id都无法在注入的脚本中工作:

//create a script tag to inject, then set a variable with the id in that script
let idScript = document.createElement("script");
idScript.setAttribute("type", "application/javascript");
idScript.textContent = 'var myExtId = "' + chrome.runtime.id +'";';
let parent = ( document.head || document.documentElement );
parent.insertBefore( idScript, parent.firstChild );

//inject and run your other script here

idScript.remove(); //then cleanup 
Run Code Online (Sandbox Code Playgroud)

因为我们将它设置为var,其他脚本现在可以直接访问该值

  • 谢谢你。这可能应该是公认的答案。 (2认同)
  • 谢啦。在过去的几个小时里一直试图解决这个问题,externally_connectable & onMessageExternal 成功了! (2认同)
  • Lifesaver — onMessageExternal 正是我正在寻找的东西。谢谢你! (2认同)

小智 10

我发现自己遇到了和你在这里描述的问题相同的问题.我发现适合我的解决方案是使用backgroundpage而不是后台脚本,如下所示:

"background_page": "src/background.html",
  // maybe this seems to work instead of background { scripts [] }

  /* this gives a Port error: Could not ...
  "background": {
  "scripts": ["src/background.js"]
  },
  */
Run Code Online (Sandbox Code Playgroud)

我希望这也适合你.

  • 你不能再使用Manifest> = version 2.0的`background_page`,所以这个答案不再适用. (38认同)
  • @Neil这个功能没有消失,只是定义了不同的"背景":{"page":"html/background.html"}`,根据http://developer.chrome.com/extensions/background_pages.html (7认同)
  • @IBBoard,请看我的答案.我认为问题是sendRequest()已被弃用,而不是sendMessage(),它应该在Manifest v2扩展中有效. (3认同)

som*_*sar 5

HTML后台页面在Windows 7上的Chrome 20.0.1132.57 m中对我不起作用,出现相同错误:

Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:232
Run Code Online (Sandbox Code Playgroud)

我尝试使用background.js具有以下内容的脚本:

chrome.extension.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
        // May be empty.
    });
});
Run Code Online (Sandbox Code Playgroud)

解决了onDisconnect我的内容脚本中的立即数

port = chrome.extension.connect();
port.onDisconnect.addListener(function (event) {
    // Happened immediately before adding the proper backend setup.
    // With proper backend setup, allows to determine the extension being disabled or unloaded.
});
Run Code Online (Sandbox Code Playgroud)

正确的代码来自Chromium Extensions消息传递示例:http : //src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/messaging/timer/page.js?view=标记

该示例还包含用作的后端的第二部分sendRequest,但我自己尚未对其进行测试:

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        // Don't know if it may be empty or a sendResponse call is required.
    });
Run Code Online (Sandbox Code Playgroud)

  • 您能否详细说明每个代码块按什么顺序进入哪个文件?我仍然遇到同样的错误... (2认同)

小智 5

现在面临同样的问题。

//这是我以前的background.js

chrome.runtime.onInstalled.addListener(function () {
  //some other code here
  chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
    if (message.url) {
        chrome.downloads.download({
            url: message.url,
            conflictAction: 'uniquify',
            saveAs: false
        });
    }
});});//(Sorry, I tried but failed to put the last bracket on next line)
Run Code Online (Sandbox Code Playgroud)

您会看到,当 onInstalled 事件发生时,以及当我将其移出此范围时,将触发chrome.runtime.onMessage.addListener ,并使我的代码如下所示:

chrome.runtime.onInstalled.addListener(function () {
  //some other code here
});  
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
    if (message.url) {
        chrome.downloads.download({
            url: message.url,
            conflictAction: 'uniquify',
            saveAs: false
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

现在效果很好。希望对您有所帮助。