I am getting an error with not being able to establish a connection from background to content. I want the background to be able to send a message to every instance of the content page to globally run a function for all tabs. Any help would be greatly appreciated as I have been stuck on this for hours. Thank you!
Error:
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
content.js:
// Message listener from background.js to execute a function in this content script
chrome.runtime.onMessage.addListener((message, _sender, _sendResponse) => {
if (message.action === "resetTimer") {
resetInactiveTimer();
}
});
background.js:
if (message.action == "globalResetTimer") {
// Reset timer in all tabs
chrome.tabs.query({}, function (tabs) {
for (var i=0; i<tabs.length; ++i) {
chrome.tabs.sendMessage(tabs[i].id, { action: "resetTimer"});
}
});
}
manifest.json:
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start"
}
],
"background": {
"service_worker": "background.js"
},
Justin Zhao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.