Port Error: Connection failed. End of reception does not exist. In chrome

I am developing an extension in Chrome and there is a problem. In my inject.js I make a request like:

 chrome.extension.sendRequest({command:'skip'},callback) 

and in my `background.js I just add a Listener query like:

 chrome.extension.onrequest.addListener(function(req,sender,res){console.log("procession"}) 

But there is an error:

Port Error: Connection failed. End of reception does not exist

It seems a mistake in chrome? PS:
part of my manifest.json

 "background": { "scripts": ["background.js"] }, "content_scripts": [ { "matches": ["&lt all_urls &gt"], "js": ["inject.js"] } ], 

I'm in Chromium 17 and I tried to restart the extension, open the browser again ... nothing happened
Does anyone have any ideas?

+52
google-chrome-extension
Feb 02 2018-12-12T00:
source share
15 answers

I found that I have the same problem as you describe here. The solution I found for me is to use a background page instead of a background script, for example:

 "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"] }, */ 

Hope this works for you too.

+9
Feb 06 '12 at 9:30
source share

This is not always the reason, but if you have an error in background.js , you should check this link:

 Inspect views: _generated_background_page.html 

on the Extensions page, which will show you JavaScript errors.

This prevented the establishment of my connections.

+21
Jul 27 2018-12-12T00:
source share

The problem may be that sendRequest() and onRequest are deprecated and replaced with sendMessage() and onMessage . Since the recent Chrome 20 update, they seem to have completely disappeared.

The official documentation for Message Passing no longer mentions sendRequest() .

Here is a link that documents the change a bit: http://codereview.chromium.org/9965005/

+13
Jul 20 '12 at 21:18
source share

The HTML background page did not work for me on 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 background.js script with the following content:

 chrome.extension.onConnect.addListener(function(port) { port.onMessage.addListener(function(msg) { // May be empty. }); }); 

This 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 is from the 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, which serves as the backend for sendRequest , but I have not 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. }); 
+5
Jul 19 '12 at 18:05
source share

I saw this error using manifest_version: 2 and chrome.runtime.sendMessage . I am connecting from a webpage to the extension, not inside the extension.

The solution was to make sure that I have the correct values ​​in the externally_connectable.matches array in manifest.json. You need to specify the URLs that you want to connect to the extension. See https://developer.chrome.com/extensions/manifest/externally_connectable#without-externally-connectable .

+2
Jan 28 '16 at 15:27
source share

try entering some contect scripts into the tab where url is chrome: // * or https://chrome.google.com/* when you connect these tabs to the backgroundpage,

eg

 chrome.tabs.connect(tab.id).postMessage(msg) 

then quit

 Port error: Could not establish connection. Receiving end does not exist. 

these pages do not allow content scripting, and the connection will be disconnected immediately.

So check the url and don't connect these tabs then no exception is thrown

+1
Aug 11 2018-12-12T00:
source share

After some time spent researching, I found a problem in my case.

I also get:

 Port error: Could not establish connection. Receiving end does not exist. 

Before explaining, I want to say that I use sendMessage and onMessage for communication.

For me, this error appears when I send a message from the background page to one tab where my content script is running.

My extension only works on tabs where youtube is open. Therefore, when I change some settings, I look at which tabs I have open and send a message to update the updated preferences.

In the normal case, it works fine, but if I have n (n> = 1) tabs with youtube open. I click the Refresh button to expand, I’m changing something ... the youtube tabs are not updated and they have lost the message listener, so I get this error.

This seems to be normal.

If I refresh youtube tabs after reboot, I do not get this error.

I found one solution, it may not apply for all cases:

When I had this problem, 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 answer, I do not need an answer, and because he did not answer, I had this error.

+1
Nov 16 '12 at 23:22
source share

I use sendMessage and onMessage for communication, and in my workflow I first send a message from injected.js to my background.js, and I also got "Port error: connection failed. Connection end does not exist." mistake.

So, I decided to use response functions (like ACK ), and if the background doesn't respond, I keep trying like setTimeout, for example.

//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 now works correctly, so I think the explanation is easy to see. Chrome does not create Background.js files and connections when it enters your code on the pages where you want, and therefore no one listens to your sent message, so if no one listens, just keep trying like me.

+1
Jun 20 '13 at 10:22
source share

Confrontation with the same problem now.

// Here is my old 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 that chrome.runtime.onMessage.addListener is fired when the onInstalled happpens event, and when I exit this scope, and create my code as follows:

 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 }); } }); 

Now it works well. Hope to be helpful.

+1
Jan 24 '16 at 10:22
source share

Check out the latest tutorials: http://developer.chrome.com/extensions/messaging.html

Note. If several messages listen for onMessage events, first, to send sendResponse () for a particular event, it will send a response. All other responses to this event will be ignored.

Close the tabs, leave only one page and check. In my case, this was a problem.

0
Jun 30 '13 at 21:52
source share

Some of the other answers here contain useful debugging tips or small puzzle pieces, however, if you want to embed in (third-party) web pages like me, then there are no answers listing the correct components.

There are 4 steps:

  • External background listener
  • The sender of the foreground message
  • insert extension id for message sender
  • and the rule in the manifest is to tie everything together.

The example below should be all you need to let you embed js that sends messages from any page on google.com to your own extension

First, in manifest.json, you need to add the messaging rules that are described here :

 "externally_connectable": { "matches": ["*://*.google.com/*"] } 

Then in the background script or on the page you need an external listener ( not the usual chrome.runtime.onMessage.addListener 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 }); 

When you have these parts, you can use the sender of the message , as usual, but with the extension identifier as the first parameter:

 chrome.runtime.sendMessage(myExtId, { /* whatever you want to send goes here */ }, response => { /* handle the response from background here */ } ); 

If you do not know how to get the external identifier, which I used as the first parameter, you can insert your extension identifier , as shown below. This is necessary because chrome.runtime.id and @@ extension_id both do not work in embedded 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 

Since we set it to var, another script can now directly access the value

0
Aug 22 '19 at 13:46
source share

If you have a problem, mainly because you are referencing an outdated document, update it!

Visit: chrome extension: messaging.html

-one
Jul 26 '13 at 10:31 on
source share

I saw this problem when my script content did not load, so sending a message never occurred.

The problem was that I had a background inspector script open and just pressed Cmd + R (update), but there was an error in my manifest.json . When I really went to the extensions page and reloaded this page, I received a warning showing a manifest error.

Basically, I saw this because my content scripts never loaded in the first place, and I thought I was updating my manifest, but I wasn’t.

-one
Nov 14 '13 at 0:03
source share

For manifest 2.0 and the sendMessage case, this is pretty simple:

This happens if you try to use sendMessage in a popup window and there is no end to the listener setup at the end of the background script, or the listener is somehow removed.

By listener, I mean chrome.runtime.onMessage.addListener .

-one
Mar 01 '19 at 13:10
source share

I put chrome.runtime.sendMessage and chrome.runtime.onMessage.addEventListener in the content script in window.onload or in the self-realization function, and it works. And in the manifest I use

"background": { "scripts": ["background.js"], "persistent": false },

manifest_version: 2. Hope this helps.

-one
Apr 01 '19 at 8:25
source share



All Articles