Port error: Could not establish connection. Receiving end does not exist. In Chromiume
Google Chrome-ExtensionGoogle Chrome-Extension Problem Overview
I'm developing an extension in Chrome, and there's a problem. In my inject.js
, I make a request like:
chrome.extension.sendRequest({command:'skip'},callback)
and in my `background.js I simply add a request Listener like:
chrome.extension.onrequest.addListener(function(req,sender,res){console.log("procession"})
But there's an error:
Port error: Could not establish connection. Receiving end does not exist
It seems a bug in chrome?
PS:
part of my manifest.json
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["< all_urls >"],
"js": ["inject.js"]
}
],
I'm in Chromium 17, and I tried reloading the extension, reopening the browser... nothing happened
some one get some ideas?
Google Chrome-Extension Solutions
Solution 1 - Google Chrome-Extension
This isn't always the cause, but if you have an error in your background.js
, you should check this link:
Inspect views: _generated_background_page.html
in the Extensions page, which will show you any JavaScript errors.
This was preventing my connections from getting established.
Solution 2 - Google Chrome-Extension
Some of the other answers here have good debugging advice or small pieces of the puzzle, however if you want to inject into (3rd party) web pages like me, then no answers here listed the correct components.
There are 4 steps:
- an external background listener,
- the foreground message sender,
- injecting extension id for the message sender
- and a rule in the manifest to link everything together.
The example below should be everything you need to allow you to inject a js that sends messages from any page on google.com to your own extension
First in the manifest.json you need to add the messaging rules which are described here:
"externally_connectable": {
"matches": ["*://*.google.com/*"]
}
Then in your background script or page, you need an external listener (Not the regular chrome.runtime.onMessage.addListener which has been mentioned in msot answers), this is described here:
chrome.runtime.onMessageExternal.addListener( (request, sender, sendResponse) => {
console.log("Received message from " + sender + ": ", request);
sendResponse({ received: true }); //respond however you like
});
Once you have these parts, you can use a message sender as you usually do, but with the extension id as first parameter:
chrome.runtime.sendMessage(myExtId, { /* whatever you want to send goes here */ },
response => {
/* handle the response from background here */
}
);
If you don't know how to get the external id I used as first param, you can inject your extension id like below. This is required because chrome.runtime.id and @@extension_id both fail to work in injected scripts:
//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
Because we set it as a var, the other script can directly access the value now
Solution 3 - Google Chrome-Extension
The problem could be that and sendRequest()
have been deprecated and replaced with onRequest
sendMessage()
and onMessage
. Since a recent Chrome 20 update they seem to be gone completely.
The official documentation on Message Passing doesn't even mention anymore.sendRequest()
Here is a link which documents the change a little bit: http://codereview.chromium.org/9965005/
Solution 4 - Google Chrome-Extension
I found myself having the same issue as you describe here. The solution I found that works for me is to use a backgroundpage instead of a background script, like so:
"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"]
},
*/
I hope this works for you too.
Solution 5 - Google Chrome-Extension
An HTML background page didn't work for me in Chrome 20.0.1132.57 m on Windows 7 with the same error:
Port error: Could not establish connection. Receiving end does not exist. miscellaneous_bindings:232
I tried using a background.js
script with the following content:
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
// May be empty.
});
});
That solved the immediate onDisconnect
in my content script:
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.
});
The correct code came from Chromium Extensions messaging example: http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/messaging/timer/page.js?view=markup
The example also contains the second part that serves as a backend for sendRequest
, but I haven't tested it myself:
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
// Don't know if it may be empty or a sendResponse call is required.
});
Solution 6 - Google Chrome-Extension
Confronting with the same issue now.
//Here is my former 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)
You see, the chrome.runtime.onMessage.addListener is fired when the event onInstalled happpens, and when I get it out of this scope, and make my code like this:
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
});
}
});
It works well now. Hope to be helpful.
Solution 7 - Google Chrome-Extension
I was seeing this error using manifest_version: 2
and chrome.runtime.sendMessage
. I am connecting from a web page to the extension instead of within the extension.
The solution was to make sure I had the correct values in the externally_connectable.matches
array in manifest.json. You need to list the URLs that you want to be able to connect to the extension. See https://developer.chrome.com/extensions/manifest/externally_connectable#without-externally-connectable.
Solution 8 - Google Chrome-Extension
I'm using sendMessage
and onMessage
for communication too, and in my workflow I first send a message from injected.js to my background.js and I also got this error:
> Port error: Could not establish connection. Receiving end does not > exist.
So I decided to deal with using the responses functionalities ( like ACK ), and if background doesn't respond I keep trying with a setTimeout
like so.
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();
My code is now running properly so I think that the explanation is easy to view. Chrome don't prepare Background.js and connection stuffs when it inject your code in the pages where you want to, and so makes nobody listen for your sent message, so if no one is listening, just keep trying like I do.
Solution 9 - Google Chrome-Extension
try to inject some contect scripts into a tab which url is chrome://* or https://chrome.google.com/\* , when connect these tabs in backgroundpage ,
for example
chrome.tabs.connect(tab.id).postMessage(msg)
then will throw
Port error: Could not establish connection. Receiving end does not exist.
these page not allow inject content scripts, and the connection will disconnect immediately .
so, check the url and not connect these tabs ,then the exception is not thrown
Solution 10 - Google Chrome-Extension
After some time spent on investigation I found the problem in my case.
I'm also getting:
Port error: Could not establish connection. Receiving end does not exist.
Before explaining, I want to say that I'm using sendMessage and onMessage for communication.
For me this error appear when I'm sending a message from the background page to one tab where my content script is running.
My extension runs only on tabs where youtube is open. So when I'm changing some preferences I'm looking to see what tabs I have open and send a message to update updated preference.
In a normal case it works fine, but if I have n (n >= 1) tabs with youtube open. I click in "Reload" button for extension, I change something ... youtube tabs are not refreshed and they lost message listener so I'm getting this error.
It seems that is pretty normal.
If I refresh youtube tabs after extension reload I don't get this error.
I found one solution, it may not apply for all cases:
When I had this issue my code was:
chrome.tabs.sendMessage(tabId, {proprName: proprName, newValue: newValue}, function(response) {});
Now my code is:
chrome.tabs.sendMessage(tabId, {proprName: proprName, newValue: newValue});
For my I didn't needed response callback and because that was not responding I had this error.
Solution 11 - Google Chrome-Extension
If the message handling is in a pop-up window or html page that is not always visible, before I send any message, I check if the pop-up is visible, like so:
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;
}
Solution 12 - Google Chrome-Extension
Check the latest manuals: http://developer.chrome.com/extensions/messaging.html > Note: If multiple pages are listening for onMessage events, only the > first to call sendResponse() for a particular event will succeed in > sending the response. All other responses to that event will be > ignored.
Close your tabs, leave only one page, and check. In my case this was the issue.
Solution 13 - Google Chrome-Extension
For manifest 2.0 and sendMessage case its fairly straight-forward:
This happens if you try to use sendMessage
inside the popup and there's no listener setup on background script end or the listener has somehow been removed.
By listener I mean - chrome.runtime.onMessage.addListener
.
Solution 14 - Google Chrome-Extension
I place chrome.runtime.sendMessage
and chrome.runtime.onMessage.addEventListener
in content script intowindow.onload
or self-execution function and it's work.
And in manifest I use
"background": { "scripts": ["background.js"], "persistent": false },
manifest_version: 2. Hope it helps.
Solution 15 - Google Chrome-Extension
I met up with this problem because I added a listener in background.js, and when invoked it would send a message to the popup page for view state update. While sometimes the listener is invoked, the popup page does not indeed exist. I check to make sure the popup page exists before sending a message and avoid this problem.
Solution 16 - Google Chrome-Extension
If you get the problem, mostly because you are referring the outdated document, update it!
Solution 17 - Google Chrome-Extension
I was seeing this problem, where my content script wasn't loading, so posting a message was never happening.
The problem turned out to be that I had the background script inspector open and was just pushing Cmd+R (refresh), but there was a bug in my manifest.json
. When I actually went to the extensions page and reloaded that page, I got the alert showing the manifest error.
Basically, I saw this because my content scripts never loaded in the first place and I thought I was refreshing my manifest, but I wasn't.
Solution 18 - Google Chrome-Extension
Check your JavaScript code their might be an error. I have found in mine