0

I'm developing a Chrome extension and encountering an error message in the console: "Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist." I'm having trouble understanding its cause and how to resolve it.

Here's the relevant code snippet from my content script:

service-worker.js

chrome.webRequest.onBeforeRequest.addListener((details) => {
  chrome.runtime.sendMessage({
    message: "background_to_popup",
    data: details,
  })
  console.log(details); 
}, {
  urls: ['<all_urls>'],
  types: ['main_frame'],
});

I'm not sure what is causing this error and how to establish the connection correctly between the content script and the background script. Any insights or suggestions would be greatly appreciated.

  • 1) chrome.webRequest cannot be used in a content script so you shouldn't load this file as a content script. It should be loaded in exactly one place indicated in `service_worker` in manifest.json 2) You need to [re-inject content scripts explicitly](/q/10994324) after reloading/installing the extension. 3) Your code uses chrome.runtime.sendMessage to communicate with the popup and not the content script. 4) To send a message to a content script from the background script or service worker you need to use chrome.tabs.sendMessage and not chrome.runtime.sendMessage. The tab id is details.tabId. – wOxxOm Jul 01 '23 at 06:19

1 Answers1

0

You should always catch the rejected promises.