Firefox webextension Error: Could not establish connection. Receiving end does not exist
Firefox webextension Error: Could not establish connection. Receiving end does not exist
我正在尝试将变量从后台脚本发送到与 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"
]
}
你得到错误:
Error: Could not establish connection. Receiving end does not exist.
当您尝试与内容脚本未侦听消息的选项卡通信(例如 tabs.sendMessage()
, tabs.connect()
)时。这包括选项卡中不存在内容脚本的情况。
您不能将内容脚本注入 about:*
URLs
对于您的问题,您收到此错误是因为没有在选项卡中注入内容脚本。当您尝试发送消息时,在 main_frame
webRequest.onErrorOccurred
事件中,选项卡的 URL 已经 about:neterror?[much more, including the URL where the error occurred]
。您 无法将内容脚本注入 about:*
URLs。因此,收听您消息的选项卡中没有内容脚本。
具体来说,您在 manifest.json content_scripts
条目中使用了 <all_urls>
match pattern。 <all_urls>
匹配:
The special value "<all_urls>"
matches all URLs under any of the supported schemes: that is, "http", "https", "file", "ftp", "app".
它 不 匹配 about:*
URLs.
有关 Firefox 在 main_frame
中获取 webRequest.onErrorOccurred
事件时使用的 URL 的更多讨论,请参阅“”
我也有同样的错误。
我的问题和解决方案不同,但我添加它以防它有帮助。
就我而言,我的 content.js 脚本最初没有 browser.runtime.onMessage.addListener() 函数 (FireFox)。
当我稍后将此侦听器添加到 content.js 脚本时,我没有在 FireFox 的 "about:debugging" 页面中重新加载临时扩展。我得到了上面的错误。
在 "about:debugging" 选项卡中单击 "reload" 后,内容脚本收到消息。
我用下一种方式处理相同的任务:
我在制作上下文菜单时遇到了类似的问题。
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "mymenu") {
// some code
browser.tabs.sendMessage(tabs[0].id, {greeting: "Hi from background script"});
}
});
我收到错误:
Could not establish connection. Receiving end does not exist
我添加一个函数和一个监听器:
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
正在运行并保持连接。
对于扩展开发人员:如果您重新加载扩展(作为开发循环的正常部分)它会切断与内容脚本的所有连接.
您还必须记得将内容脚本的页面重新加载到 re-listen 正确。
Firefox does not run content scripts on PDF pages 这意味着如果在 PDF 页面上执行扩展后台脚本并尝试向内容脚本发送消息,它将失败并出现此错误。
我正在尝试将变量从后台脚本发送到与 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"
]
}
你得到错误:
Error: Could not establish connection. Receiving end does not exist.
当您尝试与内容脚本未侦听消息的选项卡通信(例如 tabs.sendMessage()
, tabs.connect()
)时。这包括选项卡中不存在内容脚本的情况。
您不能将内容脚本注入 about:*
URLs
对于您的问题,您收到此错误是因为没有在选项卡中注入内容脚本。当您尝试发送消息时,在 main_frame
webRequest.onErrorOccurred
事件中,选项卡的 URL 已经 about:neterror?[much more, including the URL where the error occurred]
。您 无法将内容脚本注入 about:*
URLs。因此,收听您消息的选项卡中没有内容脚本。
具体来说,您在 manifest.json content_scripts
条目中使用了 <all_urls>
match pattern。 <all_urls>
匹配:
The special value
"<all_urls>"
matches all URLs under any of the supported schemes: that is, "http", "https", "file", "ftp", "app".
它 不 匹配 about:*
URLs.
有关 Firefox 在 main_frame
中获取 webRequest.onErrorOccurred
事件时使用的 URL 的更多讨论,请参阅“
我也有同样的错误。
我的问题和解决方案不同,但我添加它以防它有帮助。
就我而言,我的 content.js 脚本最初没有 browser.runtime.onMessage.addListener() 函数 (FireFox)。
当我稍后将此侦听器添加到 content.js 脚本时,我没有在 FireFox 的 "about:debugging" 页面中重新加载临时扩展。我得到了上面的错误。
在 "about:debugging" 选项卡中单击 "reload" 后,内容脚本收到消息。
我用下一种方式处理相同的任务:
我在制作上下文菜单时遇到了类似的问题。
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "mymenu") {
// some code
browser.tabs.sendMessage(tabs[0].id, {greeting: "Hi from background script"});
}
});
我收到错误:
Could not establish connection. Receiving end does not exist
我添加一个函数和一个监听器:
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
正在运行并保持连接。
对于扩展开发人员:如果您重新加载扩展(作为开发循环的正常部分)它会切断与内容脚本的所有连接.
您还必须记得将内容脚本的页面重新加载到 re-listen 正确。
Firefox does not run content scripts on PDF pages 这意味着如果在 PDF 页面上执行扩展后台脚本并尝试向内容脚本发送消息,它将失败并出现此错误。