1

I am building a browser extension. I want to trigger a notification when the user clicks on the extension's icon. When I try to load the extension in Chrome, I get this error: Service worker registration failed

enter image description here

As the source of the error, it highlights "background.js" in the manifest (which you find further down). No further information is given. However, the problem only occurs if I have the first snippet in background.ts, as indicated by the comments:

import { browser } from "webextension-polyfill-ts";

// This code prevents the compiled extension to be loaded into Chrome
browser.browserAction.onClicked.addListener(() => {
  console.log('browserAction.onClicked');
});

// I also have this code in the file, which works just fine.
browser.runtime.onMessage.addListener(() => {
  console.log('browser.runtime.onMessage');
});

(Note that this is of course compiled into Javascript)

Of course I suspected a permissions issue, but could not find anything suspicious. Here is the complete manifest:

{
  "name": "Test",
  "description": "Test",
  "version": "0.0.1",
  "manifest_version": 3,
  "permissions": ["storage", "activeTab", "scripting"],
  "background": {
    "service_worker": "background.js"
  },
 "action": {
    "default_title": "Click to show an alert"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "js/content-script.js"
      ]
    }
  ],
  "content_security_policy": {}
}

Any thoughts? Thanks!

jastram
  • 733
  • 7
  • 19
  • Do you see any error messages when you click on `Inspect views background page` in the chrome extension menu. – lejlun Jul 22 '21 at 16:19
  • 3
    1) There's no `browserAction` in MV3, it uses a combined `action` API, see the migration guide. 2) MV3 uses Promise for most of its API and more are coming so you shouldn't need the polyfill. – wOxxOm Jul 22 '21 at 18:26
  • Wait it does? I spent so much effort getting webextension-polyfill to work with MV3 because I thought I was stuck with the callback-based API, even using webpack and stuff... – Albert Sun Jul 22 '21 at 19:12
  • @lejlun - I cannot inspect the background page, as the extension does not load in the first place. :-( – jastram Jul 23 '21 at 06:20
  • @wOxxOm - Are you referring to the Chrome migration guide? But that's chrome-specific (chrome.action.onClicked...) How do I write this browser-agnostic? That's why I tried to use polyfill. – jastram Jul 23 '21 at 06:28
  • Well, don't use the polyfill as it's almost completely useless for MV3. To debug the error, see [Service worker registration failed. Chrome extension](https://stackoverflow.com/q/66114920) – wOxxOm Jul 23 '21 at 17:46

0 Answers0