Google chrome extension 端口错误:无法建立连接。接收端不存在。含铬
我正在用Chrome开发一个扩展,但出现了一个问题。在我的Google chrome extension 端口错误:无法建立连接。接收端不存在。含铬,google-chrome-extension,Google Chrome Extension,我正在用Chrome开发一个扩展,但出现了一个问题。在我的inject.js中,我提出如下请求: chrome.extension.sendRequest({command:'skip'},callback) 在我的'background.js中,我只需添加一个请求侦听器,如: chrome.extension.onrequest.addListener(function(req,sender,res){console.log("procession"}) 但有一个错误:
inject.js
中,我提出如下请求:
chrome.extension.sendRequest({command:'skip'},callback)
在我的'background.js中,我只需添加一个请求侦听器,如:
chrome.extension.onrequest.addListener(function(req,sender,res){console.log("procession"})
但有一个错误:
端口错误:无法建立连接。接收端不存在
这似乎是chrome中的一个bug?
PS:我的manifest.json的一部分
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["< all_urls >"],
"js": ["inject.js"]
}
],
“背景”:{
“脚本”:[“background.js”]
},
“内容脚本”:[
{
“匹配项”:[“”,
“js”:[“inject.js”]
}
],
我在Chromium 17中,我尝试重新加载扩展,重新打开浏览器。。。什么也没发生有人有什么想法吗?我发现自己的问题和你在这里描述的一样。我发现适合我的解决方案是使用背景页面而不是背景脚本,如下所示:
"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"]
},
*/
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;
}
我希望这也适用于您。在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消息传递示例:
该示例还包含用作sendRequest
后端的第二部分,但我自己还没有测试过它:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
// Don't know if it may be empty or a sendResponse call is required.
});
问题可能是
sendRequest()
和onRequest
已被弃用,并替换为sendMessage()
和onMessage
。自从最近的Chrome20更新以来,它们似乎完全消失了
上的官方文档甚至不再提到sendRequest()
这里有一个链接记录了一些更改:这并不总是原因,但是如果您的
background.js中有错误,您应该检查此链接:
Inspect views: _generated_background_page.html
在Extensions页面中,将显示所有JavaScript错误
这阻止了我建立连接。尝试将一些contect脚本注入url为chrome://*或https://chrome.google.com/*,在backgroundpage中连接这些选项卡时
比如说
chrome.tabs.connect(tab.id).postMessage(msg)
然后我会扔
Port error: Could not establish connection. Receiving end does not exist.
这些页面不允许插入内容脚本,连接将立即断开
因此,请检查url,不要连接这些选项卡,然后不会引发异常。经过一段时间的调查后,我发现我的案例中存在问题
我还得到:
Port error: Could not establish connection. Receiving end does not exist.
在解释之前,我想说我正在使用sendmages和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});
对于我的应用程序,我不需要响应回调,因为它没有响应,所以我出现了此错误。我正在使用sendMessage
和onMessage
进行通信,在我的工作流中,我首先将一条消息从injected.js发送到我的background.js,我还遇到了以下错误:
端口错误:无法建立连接。接收端不工作
存在
所以我决定使用responses功能(比如),如果后台没有响应,我会继续尝试使用setTimeout
这样的功能
background.js
...
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
...
//some code
sendResponse({status: 'everything ok'})
return true;
});
注入的.js
var myInjectedFunctionality = function() {
chrome.extension.sendMessage({method: "Steroids:loadScripts"}, function(res) {
if(res && res.status) {
} else {
setTimeout(myInjectedFunctionality, 3000);
}
});
};
myInjectedFunctionality();
我的代码现在运行正常,因此我认为解释很容易查看。当Chrome将你的代码注入到你想要的页面时,它不会准备Background.js和连接内容,因此不会有人监听你发送的消息,因此如果没有人监听,就继续像我一样尝试。查看最新手册:
注意:如果多个页面正在侦听onMessage事件,则只有
首先为特定事件调用sendResponse()将在中成功
发送响应。对该事件的所有其他回应将是
忽略
关闭选项卡,只留下一页,然后进行检查。就我而言,这就是问题所在 如果您遇到问题,主要是因为您引用的是过时的文档,请更新它
访问:我发现了这个问题,我的内容脚本没有加载,因此从未发布过消息
问题是我打开了后台脚本检查器,只是按下了Cmd+R(刷新),但是我的manifest.json
中有一个bug。当我实际转到扩展页面并重新加载该页面时,我收到了显示清单错误的警报
基本上,我看到这一点是因为我的内容脚本一开始从未加载过,我以为我正在刷新清单,但我没有。现在我面临着同样的问题
//这是我以前的background.js:
你看,当事件发生在已安装的happpens上,并且当我将其移出此范围并使我的代码如下所示时,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
});
}
});
它现在运行良好。希望对您有所帮助。我使用manifest\u version:2
和chrome.runtime.sendMessage
看到了这个错误。我正在从网页连接到分机,而不是在分机内
"externally_connectable": {
"matches": ["*://*.google.com/*"]
}
chrome.runtime.onMessageExternal.addListener( (request, sender, sendResponse) => {
console.log("Received message from " + sender + ": ", request);
sendResponse({ received: true }); //respond however you like
});
chrome.runtime.sendMessage(myExtId, { /* whatever you want to send goes here */ },
response => {
/* handle the response from background here */
}
);
//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
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;
}