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