1

I have a browser extension that is working perfectly in chrome. when I ported it across to firefox, the following snippet of code, which runs in the extensions background thread, throws an exception.

if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('/firebase-messaging-sw.js',{
        updateViaCache: 'none'
    })
    .then(function(registration) {
        console.log('Registration successful, scope is:', registration.scope);
    }).catch(function(err) {
        console.log('Service worker registration failed, error:', err);
    });
}

The error is as follows: DOMException: "The operation is insecure."

I am experiencing this issue in Firefox 70.0.1 and so I believe that these existing answers around cookie expiration settings out of date (??): Firefox: Service Worker: SecurityError: DOMException: The Operation is insecure

I have tried every combination of change to the manifest.json that I can think of and the manifest currently looks like this:

"permissions": ["storage", "contextMenus", "tabs", "notifications", "*://localhost/*"],
  "content_security_policy": "script-src 'self' '<MY-SHA256>' https://www.gstatic.com https://storage.googleapis.com; object-src 'self'",
  "background": {
    "page": "background.html",
    "persistent": true
  },
  "content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["/assets/js/browser-polyfill.min.js","/load.js"]
  }],
  "web_accessible_resources" : ["*.html", 
    "https://sitecontent.loopworks.com/scripts/fa-5-11-2/js/all.js",
    "https://www.gstatic.com/firebasejs/6.5.0/firebase.js",
    "https://www.gstatic.com/firebasejs/6.5.0/firebase-messaging.js"
  ]

This is the content of the backgound page:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />

    <script src="/assets/js/firebase.js"></script>
    <script src="/assets/js/firebase-messaging.js"></script>

    <script src="/assets/js/browser-polyfill.min.js"></script>
    <script src="/background.js"></script>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
  </body>
</html>

I'm absolutely stuck here so would really appreciate any suggestions.

Matt Austin
  • 219
  • 1
  • 3
  • 13
  • That sounds like a bug in Firefox to me, and I'd recommend filing it at https://bugzilla.mozilla.org/home If Firefox assumes that SWs can't be registered from inside a Firefox Add-on, then they should ensure that `'serviceWorker' in navigator` evaluates to `false`, like it does in other unsupported environments, like web pages served via `http://` – Jeff Posnick Nov 04 '19 at 20:49
  • 1
    In case anyone else stumbles across this, I raised this bug with Mozilla in Nov-2019: https://bugzilla.mozilla.org/show_bug.cgi?id=1593931 The comments indicate that service workers are NOT supported in Firefox Add-Ons but they are likely to be in the future. – Matt Austin Aug 10 '20 at 08:54

1 Answers1

0

This use case is simply not supported as part of add-ons at this point, and definitely buggy in terms of reporting availability at runtime. See this existent bug for details.

Dharman
  • 30,962
  • 25
  • 85
  • 135
rasdev
  • 1
  • 1
  • yes this is my understanding too. I've seen a few Mozilla bugs where this feature is being considered. I'd love to find a way to be notified if and when Mozilla does implement service workers in background.js/AddOns! – Matt Austin Aug 10 '20 at 09:00