I can find plenty of questions for sending a message from Bbackground to content scripts or from popup to background scripts, but I can’t find any on how to send a message from options.js
(triggered from my options page) to background.js
for my Chrome extension
manifest.js
{
...
"manifest_version": 3,
"permissions": [
"alarms",
"contextMenus",
"notifications",
"storage"
],
"background": {
"service_worker": "background.js"
},
"action": {
"default_popup": "popup.html"
},
"options_page": "options.html",
"icons": {
"48": "/assets/icons/icon-48.png",
"128": "/assets/icons/icon-128.png"
}
}
Code in options.js
// Trigger an update
chrome.runtime.sendMessage('', {
type: 'update'
});
Code in background.js
// Listen for messages
chrome.runtime.onMessage.addListener(data => {
console.log(data);
if (data.type === 'update') {
scheduleRequest();
}
});
But nothing is happening
I’ve tried using tabs.sendMessage
, but even if I put tabs
in permissions it tells me that it isn’t defined when I add it to the function chrome.tabs.sendMessage(tabs[0].id, {...