0

So I'm migrating from MV2 to MV3 and have quite an issue. On MV2 I've used

  "background": {
    "page": [
      "background.html"
    ],
  },

On MV3, we have service worker, which accepts only .js files. My background.html is being injected with webpack scripts. And was running then in the background obviously. As for now, I'm unable to inject or load my scripts to background.js file. There are a lot of scripts and moving everything to background.js is just nonsense I think.

I've tried iframing my background.html into index.html (front-end), so it would run together, but then I'm facing Extension manifest must request permission to access this host error. Meaning I am unable to access chrome:// URL's. Maybe are there any more options to somehow run background.html together with index.html and just keep service worker very simple?

  • There's no way to use html. You can configure webpack to produce a single js file or generate a simple loader yourself as [shown here](https://stackoverflow.com/a/66408379). Dynamic loading is complicated but there may be plugins for webpack like sw-precache-webpack-plugin. – wOxxOm Oct 27 '22 at 09:53
  • That makes a bit of sense, I'm trying to generate a loader, but receiving errors, – Karolis Šiaulys Oct 27 '22 at 10:21

1 Answers1

0

Use in your 1 background script worker file the import to add your other JavaScript files:

import { mySecondFunction} from "./myscript2.js";
import { myThirdFunction} from "./myscript3.js";
import { myFourFunction} from "./myscript4.js";

Migrating from background pages to service workers https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/

user1731468
  • 864
  • 2
  • 9
  • 30
  • My real background.js, the one that does all the job, is deep inside src, not a public folder. And it's not a function, there are a lot of stuff going on there. I've tried using importScripts, but receiving errors. – Karolis Šiaulys Oct 27 '22 at 10:19