1

I am making a chrome extension and am trying to get my background script to communicate with my content script each time a tab is update. This is the code I am using

//Background
chrome.tabs.onActivated.addListener(function(activeInfo) {
  console.log("Tab with id: " + activeInfo.tabId + " and window: " + activeInfo.windowId + " is now active");
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {message : "run", data: "replaceText"}, function(response) {});
});
});
//content
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
      if (request.message === "run") {
        console.log("Received message: " + request.message);
        Display();
      }
    })};
//manifest
    "permissions": ["tabs","activeTab","nativeMessaging"],

I am still getting the error Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

I have tried deactivating all my extensions and changing code nothing works.

  • 1) You need to [re-inject content scripts after install/update/reload](https://stackoverflow.com/q/10994324). 2) Remove chrome.tabs.query and simply use activeInfo.tabId. – wOxxOm Jan 16 '23 at 16:56

0 Answers0