I’m trying to send a message from my popup.js to my content.js script but the content.js script is not receiving it. content.js seems to not receive messages from popup or background scripts.
popup.js
window.onload = function() {
browser.tabs.query({active: true, currentWindow: true}, function(tabs) {
browser.tabs.sendMessage(tabs[0].id, { greeting: "hello from the popup" });
browser.runtime.sendMessage({ tabId: tabs[0].id, greeting: "Going to background?"});
debugger;
});
}
content.js
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Received request: ", request);
return true;
});
background.js — this one does receive the message
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Received request in the bg: ", request);
browser.tabs.sendMessage(request.tabId, "Sent from bg");
});
manifest.json
{
"manifest_version": 3,
"name": "__MSG_extension_name__",
"description": "__MSG_extension_description__",
"version": "1.0",
"background": {
"scripts": [ "background.js" ],
"type": "module"
},
"content_scripts": [{
"js": [ "content.js" ],
"matches": [ "*://*/*" ],
"run_at": "document_end",
}],
"action": {
"default_popup": "popup.html",
"default_icon": "images/toolbar-icon.svg"
},
"permissions": [
"activeTab",
"nativeMessaging"
]
}