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;
}
});