I’m working on a browser extension. In Chrome, I can simply do this:
manifest.json:
"background": {
"service_worker": "scripts/vh_service_worker.js",
},
and in my service worker, I can use ìmportScript("somefile.js")
without issues.
In Firefox, background.service_worker
is not implemented for Manifest v3.
Attempt #1:
I load the service worker as background script:
manifest.json:
"background": {
"scripts": ["scripts/vh_service_worker.js"]
},
This generally works well, but unfortunately it does not seem to be executed in the WorkerGlobalScope
, so the function importScripts()
is not available.
Attempt #2:
I tried to use the background script to register a service worker manually:
manifest.json:
"background": {
"scripts": ["scripts/vh_service_worker_loader.js"]
},
vh_service_worker_loader.js:
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("scripts/vh_service_worker.js")
.then((registration) => {
console.log("Service Worker registered with scope:", registration.scope);
})
.catch((error) => {
console.error("Service Worker registration failed:", error);
});
}
But unfortunately there is no navigator.serviceWorker
entry available, so the if condition always return false.
So my question is:
How can I use importScripts()
in Firefox for an extension using Manifest V3 ?