I'm writing a web extension for Chromium and I'm using a service worker to do some background activities (something that with manifest V2 would have been done using a BackgroundScript). I need to send messages from a script that is injected into a web page to the service worker and get responses back. To do this, inside the injected script I use chrome.runtime.sendMessage like this:
chrome.runtime.sendMessage(extensionID,
{
content: emailBody,
identifiers: recipientsAddressesArray,
request: "encrypt"
}, (response)=>console.log("Got response: ", response)
)
While inside the service worker i have the following code:
import {encrypt, decrypt, getHiddenPolicies} from "./crypto";
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
console.log("Message listener invoked")
})
console.log("Service worker started!")
Now, when the message is sent from the page to the service worker, I get "Got response: undefined" in the page's console regardless of whether I write code in the message handler to send a response back or not, while if I inspect the service worker output using the devtools, I see "Service worker started!" and some output form the script I'm importing, but no "Message listener invoked". What I'm getting wrong?
Thank you for your help,
Giacomo