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.