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

前端 未结 17 2416
甜味超标
甜味超标 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: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.
        });
    

提交回复
热议问题