1

This is for manifest v3, I've seen a lot of other discussions on this issue but most of the past discussions I can find on this seem to be for v2 or older and nothing I've found has worked for me. I am testing on Chrome Version 116.0.5845.4 (Official Build) dev (x86_64).

I was trying to get started with simple message passing based on the documentation.

manifest.json:

{
  "name": "Test App",
  "version": "1.0",
  "manifest_version": 3,
  "action": {},
  "permissions": ["storage", "activeTab", "scripting"],
  "host_permissions": ["<all_urls>"],
  "background": {
    "service_worker": "background.js"
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["contentScript.js"]
    }
  ] 
}

background.js

chrome.action.onClicked.addListener((tab) => {
    console.log(tab);  // logs successfully
    console.log(tab.id);  // logs successfully
    const response = chrome.tabs.sendMessage(tab.id, {greeting: "hello"});  // fails here
  });

contentScript.js

console.log('content script');  // not logged
chrome.runtime.onMessage.addListener(request => {
  console.log('received', request)
})

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

Based on the error and that the content script is not logging that one output, it seems like the content script is not injected at all.

So I thought I would start over with some out of the box implementation using the "popup" boilerplate found in the vs code extension. I won't share what that looks like here but it basically has a popup.js script that runs from the extension pop up and tries to send a message to background.js, but it fails with the exact same error.

There's a comment on this post which states that "content scripts don't auto-run after you reload the extension" and this seemed to fix things for the OP. I've tried refreshing my pages and my browser and this has also not fixed the issue for me.

Ken Myers
  • 596
  • 4
  • 21
  • The code is correct and the explanation of the problem is also correct, so reloading the tab should work. Another solution is [shown here](https://stackoverflow.com/q/10994324). If it doesn't work then check `chrome://policy` for ExtensionSettings that contains runtime_blocked_hosts for this domain. – wOxxOm Jun 28 '23 at 16:20
  • Try `"host_permissions": ["*://*/*"]`. It looks like the problem has nothing to do with message passing if you don't see the `console.log('content script');` output – Andrew Parks Jun 28 '23 at 16:21
  • Thanks all, I tried changing host permissions and started seeing the content script log, changed it back and started seeing it again (not sure what was going on before). Still got the error but what fixed it was I was having the content script send a message back. I guess it was erroring out because of that? I'm not sure why it couldn't find the receiving end because of that. – Ken Myers Jun 28 '23 at 17:19
  • There will not be a receiving end if the host_permissions prevented the content script from running – Andrew Parks Jun 28 '23 at 17:35

0 Answers0