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

前端 未结 17 2353
甜味超标
甜味超标 2020-12-05 09:26

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:         


        
相关标签:
17条回答
  • 2020-12-05 09:33

    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.

    0 讨论(0)
  • 2020-12-05 09:33

    If you get the problem, mostly because you are referring the outdated document, update it!

    Visit: chrome extension: messaging.html

    0 讨论(0)
  • 2020-12-05 09:36

    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.

    0 讨论(0)
  • 2020-12-05 09:37

    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

    0 讨论(0)
  • 2020-12-05 09:40

    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.
        });
    
    0 讨论(0)
  • 2020-12-05 09:41

    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.

    0 讨论(0)
提交回复
热议问题