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?
This isn't always the cause, but if you have an error in your
background.js
, you should check this link:in the Extensions page, which will show you any JavaScript errors.
This was preventing my connections from getting established.
An HTML background page didn't work for me in Chrome 20.0.1132.57 m on Windows 7 with the same error:
I tried using a
background.js
script with the following content:That solved the immediate
onDisconnect
in my content script: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: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.
Confronting with the same issue now.
//Here is my former background.js:
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:
It works well now. Hope to be helpful.
Check the latest manuals: http://developer.chrome.com/extensions/messaging.html
Close your tabs, leave only one page, and check. In my case this was the issue.
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 "Port error: Could not establish connection. Receiving end does not exist." error.
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
//injected.js
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.