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

问题描述 投票:7回答:3

我正在尝试将后台脚本中的变量发送到与HTML页面关联的内容脚本。内容脚本使用从后台脚本接收的变量更新HTML内容。

问题是我收到此错误消息:

Error: Could not establish connection. Receiving end does not exist.

后台脚本main.js

var target = "<all_urls>";
function logError(responseDetails) {
  errorTab = responseDetails.tabId;
  console.log("Error tab: "+errorTab);

  errorURL = responseDetails.url;
  console.log("Error URL: "+errorURL);

  //send errorURL variable to content script
  var sending = browser.tabs.sendMessage(errorTab, {url: errorURL})
    .then(response => {
      console.log("Message from the content script:");
      console.log(response.response);
    }).catch(onError);

  //direct to HTML page
  browser.tabs.update(errorTab,{url: "data/error.html"});
}//end function

browser.webRequest.onErrorOccurred.addListener(
  logError,
  {urls: [target],
  types: ["main_frame"]}
);

error.html是:

<html>
<head>
  <meta charset="UTF-8">
</head>
<body>
  The error received is <span id="error-id"></span>
  <script src="content-script.js"></script>
</body>
</html>

content-script.js

//listen to errorURL from the background script.
browser.runtime.onMessage.addListener(request => {
  console.log("Message from the background script:");
  console.log(request.url);
  return Promise.resolve({response: "url received"});
}); //end onMessage.addListener

//update the HTML <span> tag with the error
document.getElementById("error-id").innerHTML = request.url;

manifest.json

{
  "manifest_version": 2,
  "name": "test",
  "version": "1.0",
  "background": {
    "scripts": ["main.js"]
  },

  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["webextension/data/content-script.js"]
    }
  ],

  "permissions": [
    "<all_urls>",
    "activeTab",
    "tabs",
    "storage",
    "webRequest"
  ]
}
firefox firefox-addon firefox-webextensions
3个回答
9
投票

你得到错误:

Error: Could not establish connection. Receiving end does not exist.

当您尝试与内容脚本未侦听消息的选项卡进行通信时(例如tabs.sendMessage()tabs.connect())。这包括选项卡中不存在内容脚本的时间。

您无法将内容脚本注入about:* URL

对于您的问题,您收到此错误,因为没有在选项卡中注入内容脚本。当您尝试发送消息时,在main_frame webRequest.onErrorOccurred事件中,该选项卡的URL已经是about:neterror?[much more, including the URL where the error occurred]。您无法将内容脚本注入about:* URL。因此,选项卡中没有内容脚本可以监听您的消息。

具体来说,您已经在manifest.json <all_urls>条目中使用了match pattern content_scripts<all_urls>匹配:

特殊值"<all_urls>"匹配任何支持的方案下的所有URL:即“http”,“https”,“file”,“ftp”,“app”。

它与about:*网址不匹配。

有关Firefox在webRequest.onErrorOccurred中获取main_frame事件时使用的URL的更多讨论,请参阅“Injecting into navigation error page gets: Error: No window matching {“matchesHost”:[“”]}


0
投票

我也有同样的错误。

我的问题和解决方案是不同的,但我添加它,以防它有所帮助。

在我的情况下,我的content.js脚本最初没有browser.runtime.onMessage.addListener()函数(FireFox)。

当我稍后将此监听器添加到content.js脚本时,我没有在FireFox的“about:debugging”页面中重新加载临时扩展。我收到了上述错误。

单击“about:debugging”选项卡中的“reload”后,内容脚本收到了该消息。


0
投票

我以下一个方式决定了同样的任务:

我正在制作上下文菜单并遇到类似的问题。

browser.contextMenus.onClicked.addListener((info, tab) => {
       if (info.menuItemId === "mymenu") {
       // some code
       browser.tabs.sendMessage(tabs[0].id, {greeting: "Hi from background script"});
       }
        });

我收到错误:

无法建立连接。接收端不存在

我添加了一个函数和一个监听器:

browser.contextMenus.onClicked.addListener((info, tab) => {
       if (info.menuItemId === "mymenu") {
       // some code
       browser.tabs.sendMessage(tabs[0].id, {greeting: "Hi from background script"});
       }
        });
   // -----function and Listener ------
   function connectToContent() {
      browser.tabs.query({ currentWindow: true, active: true
      }).then((tabs) => {
       browser.tabs.sendMessage(tabs[0].id, {greeting: "Activate Tab"});
       });
    }

    browser.tabs.onActivated.addListener(connectToContent);

现在它正在工作.browser.tabs.onActivated.addListener确实并保持联系。


0
投票

此问题的另一个解决方案是:如果重新加载扩展(作为开发人员的正常部分),它会切断与内容脚本的所有连接。

您必须记住使用内容脚本重新加载页面,以便正确地重新监听。

© www.soinside.com 2019 - 2024. All rights reserved.