1

I'm trying to develop a chrome extension, My manifest.json is :

{
  "name" : "Extension",
  "version" : "2.2",
  "description" : "Web collaboration chrome extension",
  "permissions": ["tabs", "activeTab"],
  "browser_action": {
     "default_icon": "logo.png"
  },
  "icons": {
    "128": "icon128.png"
  },
  "content_scripts": [{
     "matches": ["*://*/*"],
     "run_at": "document_idle",
     "match_about_blank": true,
     "all_frames": true,
     "js": ["content.js", "addon.js"]
  }],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "content_security_policy": "script-src 'self'   https://expample.com/umd/example.min.js; object-src 'self'",
  "manifest_version": 2
}

background.js is :

  chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
      let activeTab = tabs[0];
      chrome.tabs.sendMessage(activeTab.id, {"message": "request", "payload":    preferences}, function(response) {
        if (!chrome.runtime.lastError) {
          console.log('response', response);
        }else{
          console.log(chrome.runtime.lastError);
        }
      });
    });

content.js is:

   chrome.runtime.onMessage.addListener(
                  function(request, sender, sendResponse) {
                     if(request.message === "request") console.log('response received', request.payload)
                     sendResponse({status: "done"});
                  return true;
                  }
               )

Chrome Inspector is giving error: chrome.runtime.lastError {message: 'Could not establish connection. Receiving end does not exist.'}.

Need help

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
shguru
  • 11
  • 3
  • Totally related https://stackoverflow.com/questions/54181734/chrome-extension-message-passing-unchecked-runtime-lasterror-could-not-establi – Arnau Sep 26 '22 at 12:53
  • 3
    Please format your manifest.json properly. – Thomas Mueller Sep 26 '22 at 13:37
  • In `content.js`, you're calling `sendResponse()` synchronously *and* you're returning `true` from the `chrome.runtime.onMessage` handler. You're supposed to do one or the other, but not both. See https://developer.chrome.com/docs/extensions/mv3/messaging/#simple – Thomas Mueller Sep 26 '22 at 13:42
  • In your `manifest.json`, the match pattern `[":///*"]` doesn't have a scheme. – Thomas Mueller Sep 26 '22 at 13:54
  • Are you using Manifest V2 so you can include a remote script in your extension? MV2 won't work much longer, see https://developer.chrome.com/docs/extensions/mv3/mv2-sunset/ – Thomas Mueller Sep 26 '22 at 14:00
  • The problem is likely that you've reloaded/installed the extension on chrome://extensions page without [re-injecting the content scripts](https://stackoverflow.com/q/10994324). – wOxxOm Sep 26 '22 at 16:31
  • Remember to refresh the page were you're trying your plugin after refreshing the plugin itself. – 0stone0 Sep 26 '22 at 16:31

1 Answers1

1

After updating your Code, you need to reload your open Tabs. Otherwise the content-scripts will not get injected properly. Therefore, the listener on tab-side (content-script) is not initiated / existing.

However, a better sollution is to inject your content-scripts by code. For example on extension-invocation with the following code:

/*re-inject content-scripts on each open of extension*/
for (const tab of tabs) {
    chrome.scripting.executeScript({
        target : {tabId : tab.id},
        files : [ "scripts/content.js" ],
    });
}
Kolja
  • 21
  • 8