【问题标题】:Port error: Could not establish connection. Receiving end does not exist. In Chromiume端口错误:无法建立连接。接收端不存在。在铬
【发布时间】:2021-09-08 01:01:52
【问题描述】:

我正在 Chrome 中开发一个扩展程序,但出现了问题。在我的inject.js 中,我提出如下请求:

chrome.extension.sendRequest({command:'skip'},callback)

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

chrome.extension.onrequest.addListener(function(req,sender,res){console.log("procession"})

但是有一个错误:

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

这似乎是 chrome 中的一个错误? PS:
我的 manifest.json 的一部分

"background": {
    "scripts": ["background.js"]
},
"content_scripts": [
  {
    "matches": ["< all_urls >"], 
    "js": ["inject.js"]
  }
],

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

【问题讨论】:

  • 我遇到了类似的问题。尽管这可能无济于事,但请确保“background.js”实际上正在加载。我的问题是我使用了“background.js”而不是“scripts/background.js”
  • 对于 sendMessage 的情况,这是因为您可能没有在后台脚本中设置(或错误地删除)相应的 onMessage 侦听器。 stackoverflow.com/a/54945389/1469614.

标签: google-chrome-extension


【解决方案1】:

这并不总是原因,但如果您的background.js 中有错误,您应该检查此链接:

Inspect views: _generated_background_page.html

在扩展页面中,它将显示所有 JavaScript 错误。

这阻止了我的连接建立。

【讨论】:

  • 看起来当background.js中有错误时调用会失败。
【解决方案2】:

问题可能是 sendRequest() 和 onRequest 已被弃用并替换为 sendMessage() 和 onMessage。由于最近的 Chrome 20 更新,它们似乎完全消失了。

Message Passing 的官方文档甚至不再提及 sendRequest()。

这里有一个链接,其中记录了一些更改:http://codereview.chromium.org/9965005/

【讨论】:

  • 我认为这是最好的答案,感觉 google 很快不会支持 background_page
  • 这是任何使用 Manifest 2.0 版的人的解决方案
【解决方案3】:

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

有4个步骤:

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

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

首先在ma​​nifest.json中添加described here:

"externally_connectable": {
    "matches": ["*://*.google.com/*"]
}

然后在您的后台脚本或页面中,您需要一个外部监听器(不是常规的chrome.runtime.onMessage。 msot 答案中提到的 addListener),这是described here:

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

一旦你有了这些部分,你就可以像往常一样使用message sender,但将extension id作为第一个参数:

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

如果您不知道如何获取我用作第一个参数的外部 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 

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

【讨论】:

  • 谢谢。这应该是公认的答案。
  • 这是否意味着您必须将所有要允许注入的网站“列入白名单”bg-script 通信?还是我缺少什么?
  • 您似乎混淆了术语 - 您在谈论一些不同的东西。为此,您需要添加您希望能够将消息直接发送到您的扩展程序的每个第 3 方域
  • 谢谢伙计。在过去的几个小时里一直试图解决这个问题,externally_connectable 和 onMessageExternal 成功了!
【解决方案4】:

我发现自己遇到了与您在此处描述的相同的问题。我发现对我有用的解决方案是使用背景页面而不是后台脚本,如下所示:

"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"]
  },
  */

我希望这对你也有用。

【讨论】:

  • 我遇到了同样的问题,一旦添加了 background_page 就可以了。该页面是空的,但看起来 API 需要使用“端口”。
  • 我正在尝试升级到 Manifest v2,不幸的是,“background_page”设置在加载解压缩的扩展程序时会引发警告,并且未在 Manfest 页面上列出 (code.google.com/chrome/extensions/manifest.html)。仍在尝试以新格式跟踪工作修复。
  • @IBBoard,请看我的回答。我认为问题在于 sendRequest() 已被弃用,取而代之的是 sendMessage() 应该在 Manifest v2 扩展中工作。
  • Manifest >= 2.0 版不能再使用background_page,所以这个答案不再有效。
  • @Neil 该功能并没有消失,只是根据developer.chrome.com/extensions/background_pages.html 以不同的方式定义"background" : { "page" : "html/background.html" }
【解决方案5】:

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

Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:232

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

chrome.extension.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
        // May be empty.
    });
});

这解决了我的内容脚本中的即时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.
});

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

该示例还包含用作sendRequest 后端的第二部分,但我自己没有测试过:

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

【讨论】:

  • 你能详细说明每个代码块按什么顺序进入哪个文件吗?我仍然遇到同样的错误...
【解决方案6】:

现在面临同样的问题。

//这里是我以前的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)

你看,当 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
        });
    }
});

现在运行良好。希望对您有所帮助。

【讨论】:

  • 这应该被列为 Google 的 Chrome 扩展文档中的一个陷阱。我像你一样在 onInstall 中有我的 onMessage 侦听器,它工作正常,直到后台脚本变为非活动状态。后续消息会激活后台脚本但不会被处理,导致和OP一样的错误。
【解决方案7】:

我在使用 manifest_version: 2 和 chrome.runtime.sendMessage 时看到了这个错误。我是从网页连接到扩展程序,而不是在扩展程序内。

解决方案是确保我在 manifest.json 中的 externally_connectable.matches 数组中具有正确的值。您需要列出希望能够连接到扩展的 URL。见https://developer.chrome.com/extensions/manifest/externally_connectable#without-externally-connectable。

【讨论】:

  • 是的,必须在此处添加 https://localhost:3000 以供本地开发人员使用。避免 Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. 还要确保您请求正确的扩展 ID...
【解决方案8】:

我也在使用sendMessage 和onMessage 进行通信,在我的工作流程中,我首先将消息从injected.js 发送到我的background.js,我也收到了这个错误:

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

所以我决定使用响应功能(例如 ACK )来处理,如果背景没有响应,我会继续尝试使用 setTimeout 像这样。

background.js

...
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
...
//some code

sendResponse({status: 'everything ok'})
return true;    
});

injected.js

var myInjectedFunctionality = function() {

    chrome.extension.sendMessage({method: "Steroids:loadScripts"}, function(res) {
        if(res && res.status) {
            } else {
                setTimeout(myInjectedFunctionality, 3000);
            }
    });
};
myInjectedFunctionality();

我的代码现在运行正常,所以我认为解释很容易查看。 Chrome 在将您的代码注入您想要的页面时不会准备 Background.js 和连接内容,因此不会有人收听您发送的消息,因此如果没有人在收听,请像我一样继续尝试。

【讨论】:

    【解决方案9】:

    在连接这些时,尝试将一些连接脚本注入到 url 为 chrome://* 或 https://chrome.google.com/* 的标签中背景页中的标签,

    例如

    chrome.tabs.connect(tab.id).postMessage(msg)
    

    然后会抛出

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

    这些页面不允许注入内容脚本,连接会立即断开。

    所以,检查 url 并且不连接这些选项卡,那么不会抛出异常

    【讨论】:

      【解决方案10】:

      经过一段时间的调查,我发现了我的问题。

      我也得到了:

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

      在解释之前,我想说的是我使用sendMessage和onMessage进行通信。

      对我来说,当我从后台页面向运行内容脚本的一个选项卡发送消息时会出现此错误。

      我的扩展程序仅在打开 youtube 的标签上运行。 因此,当我更改某些首选项时,我希望查看我打开了哪些选项卡并发送消息以更新更新的首选项。

      在正常情况下它工作正常,但如果我有 n (n >= 1) 个标签并打开 youtube。我单击“重新加载”按钮进行扩展,我更改了一些内容... youtube 标签未刷新,并且它们丢失了消息侦听器,因此我收到此错误。

      这似乎很正常。

      如果我在重新加载扩展程序后刷新 youtube 标签,我不会收到此错误。

      我找到了一种解决方案,它可能不适用于所有情况:

      当我遇到这个问题时,我的代码是:

      chrome.tabs.sendMessage(tabId, {proprName: proprName, newValue: newValue}, function(response) {});
      

      现在我的代码是:

      chrome.tabs.sendMessage(tabId, {proprName: proprName, newValue: newValue});
      

      对于我来说,我不需要响应回调,因为它没有响应,所以我遇到了这个错误。

      【讨论】:

        【解决方案11】:

        查看最新的手册: http://developer.chrome.com/extensions/messaging.html

        注意:如果多个页面正在监听 onMessage 事件,则只有 首先为特定事件调用 sendResponse() 将成功 发送响应。对该事件的所有其他响应将是 忽略。

        关闭标签,只留下一页,然后检查。就我而言,这就是问题所在。

        【讨论】:

          【解决方案12】:

          对于 manifest 2.0 和 sendMessage 情况,它相当简单:

          如果您尝试在弹出窗口中使用 sendMessage 并且在后台脚本端没有设置侦听器或侦听器已以某种方式被删除,则会发生这种情况。

          我的意思是听众 - chrome.runtime.onMessage.addListener。

          【讨论】:

            【解决方案13】:

            我将chrome.runtime.sendMessage 和chrome.runtime.onMessage.addEventListener 放入内容脚本中的window.onload 或自执行函数中,它的工作。 在清单中我使用

            "background": { "scripts": ["background.js"], "persistent": false },

            清单版本:2。 希望对您有所帮助。

            【讨论】:

              【解决方案14】:

              如果消息处理在弹出窗口或不总是可见的 html 页面中,在我发送任何消息之前,我会检查弹出窗口是否可见,如下所示:

              function isPopupVisible() { //required for firefox before sending any message ore we get the stupid message 'receiveing end does not exist'
                  var views = chrome.extension.getViews({ type: "popup" }); //https://stackoverflow.com/questions/8920953/how-determine-if-the-popup-page-is-open-or-not
                  if (views.length > 0) {
                      console.log("Popup is visible");
                      return true;
                  }
                  return false;
              }
              

              【讨论】:

                【解决方案15】:

                我遇到了这个问题,因为我在 background.js 中添加了一个监听器,当被调用时,它会向弹出页面发送一条消息以更新视图状态。虽然有时会调用侦听器,但弹出页面确实不存在。我在发送消息之前检查以确保弹出页面存在并避免此问题。

                【讨论】:

                  【解决方案16】:

                  如果您遇到问题,主要是因为您引用的是过时的文档,请更新它!

                  访问:chrome extension: messaging.html

                  【讨论】:

                    【解决方案17】:

                    我看到了这个问题,我的内容脚本没有加载,因此从未发生过发布消息。

                    问题原来是我打开了后台脚本检查器,只是按下 Cmd+R(刷新),但我的 manifest.json 中有一个错误。当我真正进入扩展页面并重新加载该页面时,我收到了显示清单错误的警报。

                    基本上,我看到这个是因为我的内容脚本一开始就没有加载,我以为我正在刷新我的清单,但我没有。

                    【讨论】:

                      【解决方案18】:

                      检查您的 JavaScript 代码,它们可能是错误的。我在我的找到了

                      【讨论】:

                      • 答案如何?
                      猜你喜欢
                      • 2013-02-28
                      • 1970-01-01
                      • 2012-08-02
                      • 1970-01-01
                      • 1970-01-01
                      • 2022-08-22
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多