I am building a chrome extension where I need to send a message from background.js to content.js on tab change, but its failing everytime.
I am seeing the following error on the chrome’s extension tab –
Here is how my manifest file looks –
{
"manifest_version": 3,
"version": "1.0",
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
}
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"permissions": [
"tabs"
],
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
}
}
Background.js
chrome.tabs.onActivated.addListener((tabId, tab) => {
//Sends a message to the active tab
chrome.tabs.sendMessage(tabId, {
type: "NEW",
});
});
Content.js
chrome.runtime.onMessage.addListener((obj, sender, response) => {
const { type, value } = obj;
if (type === "initObserver") {
initObserver;
}
});
2
Answers
Do not send messages to New Tab or tabs starting with chrome:// because content scripts cannot be injected.
background.js
The invocation error means one of the parameters of sendMessage is incorrect but it can’t happen with the code currently posted in the question, so apparently it’s an old error. Click the trashcan icon to remove it.
After the extension is reloaded or installed/updated you’ll need to inject the content scripts explicitly into currently open tabs because Chrome doesn’t do it automatically, example.
There will be tabs that can’t run content script such as other extensions or chrome:// pages as well web sites forbidden for your extension via a global runtime_blocked_hosts policy. You can suppress and ignore the connection error so it doesn’t pollute the logs: