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

前端 未结 17 2417
甜味超标
甜味超标 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:52

    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

    ...
    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.

提交回复
热议问题