0

My Chrome extension has the following two javascript files:

background.js:

      chrome.tabs.sendMessage(/* tab.id */, { type: MessageType.ON_EXTENSION_INSTALL_OR_UPDATE }, res => {
        chrome.tabs.executeScript(/* tab.id */, {
          file: script,
        });
      });

script.js:

chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
  sendResponse('ok');
});

And get response undefined back in the content.js, and this error:

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

If I understand the issue, is when the backgeound.js sends the message to the content, the content is not already listen.

Is there a way to make sure the content is ready and listen ?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Ballon Ura
  • 882
  • 1
  • 13
  • 36
  • 1
    It can happen after reloading/updating the extension. Use executeScript: [example](https://stackoverflow.com/a/68933079). – wOxxOm Aug 27 '21 at 17:15
  • Yes i need this code for my install/update logic :), but cant get it work. In this example, the fact that "there is error", is not helping me so much, because the msg is not get sent. – Ballon Ura Aug 27 '21 at 17:18
  • What do you mean? I've linked an example that shows how to fix the problem. – wOxxOm Aug 27 '21 at 17:22
  • I really cant understand your solution there (is alsao me there :)), how this is fix the problem ? thx man. Because the "param" var ? – Ballon Ura Aug 27 '21 at 17:23
  • 1
    That example injects content scripts. After you injected them all you can send a message, not the other way around. More info here: [Chrome extension content script re-injection after upgrade or install](https://stackoverflow.com/q/10994324) – wOxxOm Aug 27 '21 at 17:44

0 Answers0