Background service worker example?

9,817 views
Skip to first unread message

Kent Brewster

unread,
Nov 14, 2019, 7:08:59 PM11/14/19
to Chromium Extensions
On my way to trying to execute a script under Manifest V3 it became clear that I'd need to first register a background service worker. Hit a wall; details here:

https://github.com/kentbrew/learning-manifest-v3/blob/master/ep_004.md

Has anyone gotten even the tiniest stub of a background service worker to run yet? If so, please share source?

Thanks very much,

--Kent

wOxxOm

unread,
Nov 15, 2019, 9:20:51 AM11/15/19
to Chromium Extensions
It's registered automatically when you declare it like this in manifest.json:

"background": { "service_worker": "background.js" }

Then use the standard chrome.* API listeners in background.js just like you would do in a standard background script with "persistent":false.

You can also inspect the test extensions in Chromium's source code:

Kent Brewster

unread,
Nov 15, 2019, 3:01:40 PM11/15/19
to wOxxOm, Chromium Extensions

> On Nov 15, 2019, at 6:20 AM, wOxxOm <wox...@gmail.com> wrote:
>
> It's registered automatically when you declare it like this in manifest.json:
>
> "background": { "service_worker": "background.js" }

Got it, thanks!

> Then use the standard chrome.* API listeners in background.js just like you would do in a standard background script with "persistent":false.
> More info: https://developer.chrome.com/extensions/migrating_to_service_workers
>
> You can also inspect the test extensions in Chromium's source code:
> https://cs.chromium.org/search/?q=file:manifest.json+%5C%22service_worker

These are great, thanks! Honestly had no idea this existed. I'm looking at the first service worker demo, here:

https://cs.chromium.org/chromium/src/chrome/test/data/extensions/context_menus/frames/service_worker

This works for both manifest v2 and manifest v3, except for scary warning about how certain features may not work as expected in v3.

But! When I try to load the service worker from a subdirectory:

"background": { "service_worker": "js/test.js" }

... it fails. Any idea what's going on here?

--Kent


> On Friday, November 15, 2019 at 3:08:59 AM UTC+3, Kent Brewster wrote:
> On my way to trying to execute a script under Manifest V3 it became clear that I'd need to first register a background service worker. Hit a wall; details here:
>
> https://github.com/kentbrew/learning-manifest-v3/blob/master/ep_004.md
>
> Has anyone gotten even the tiniest stub of a background service worker to run yet? If so, please share source?
>
> Thanks very much,
>
> --Kent
>
>
> --
> You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
> To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/d2b79f04-b5a6-448d-b302-d788568fa4af%40chromium.org.

wOxxOm

unread,
Nov 15, 2019, 3:12:57 PM11/15/19
to Chromium Extensions, wox...@gmail.com
Report it on https://crbug.com if it's not reported already.


On Friday, November 15, 2019 at 11:01:40 PM UTC+3, Kent Brewster wrote:
But! When I try to load the service worker from a subdirectory:
... it fails. Any idea what's going on here?

--Kent


> On Friday, November 15, 2019 at 3:08:59 AM UTC+3, Kent Brewster wrote:
> On my way to trying to execute a script under Manifest V3 it became clear that I'd need to first register a background service worker. Hit a wall; details here:
>
> https://github.com/kentbrew/learning-manifest-v3/blob/master/ep_004.md
>
> Has anyone gotten even the tiniest stub of a background service worker to run yet? If so, please share source?
>
> Thanks very much,
>
> --Kent
>
>
> --
> You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Alexis Hancock

unread,
Nov 15, 2019, 3:15:29 PM11/15/19
to Chromium Extensions
These are great, thanks! Honestly had no idea this existed. I'm looking at the first service worker demo, here:

https://cs.chromium.org/chromium/src/chrome/test/data/extensions/context_menus/frames/service_worker

This works for both manifest v2 and manifest v3, except for scary warning about how certain features may not work as expected in v3.

But! When I try to load the service worker from a subdirectory:

"background": { "service_worker": "js/test.js" }

... it fails. Any idea what's going on here?

--Kent

Kent,

Have you tried
"background": { "service_worker": "/js/test.js" } ? With the slash in the front?


Kent Brewster

unread,
Nov 15, 2019, 4:33:13 PM11/15/19
to Alexis Hancock, Chromium Extensions

> Have you tried
> "background": { "service_worker": "/js/test.js" } ? With the slash in the front?

Yes, thanks! In chrome://serviceworker-internals/ I have checked the checkbox to pause JavaScript execution on service workers.

With the background script URL set to /js/background.js, I see this in the log box:

Console: {"lineNumber":0,"message":"The path of the provided scope ('/') is not under the max scope allowed ('/js/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.","message_level":3,"sourceIdentifier":8,"sourceURL":""}
Error: {"columnNumber":-1,"lineNumber":-1,"message":"Failed to register a ServiceWorker for scope ('chrome-extension://icmcfccjipehgnldlcaokbnjdlmalffm/') with script ('chrome-extension://icmcfccjipehgnldlcaokbnjdlmalffm/js/background.js'): The path of the provided scope ('/') is not under the max scope allowed ('/js/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.","sourceURL":""}

With the background script URL set to js/background.js, the log box is empty.

Along the way I've developed a workflow that will actually allow me to get to a console inside my background file, which gives me hope for the future:

- Open chrome://serviceworker-internals/
- Reload the extension
- Switch back to serviceworker-internals
- Find your service worker (should be on top)
- Click Inspect
- The resulting window's console seems to be the internals of your service worker

FEATURE REQUEST: could we please have a direct link to this view via an Inspect link in the extension's card in chrome://extensions, just as if we were opening up the old-school background page?

--Kent

Simeon Vincent

unread,
Nov 15, 2019, 8:37:28 PM11/15/19
to Chromium Extensions, ale...@eff.org
Here's a dead simple MV3 extension that opens a new window on browserAction click (chrome.action not yet supported).

 
With the background script URL set to /js/background.js, I see this in the log box:

Yeah, we seem to have an issue ATM where your service worker must be a file in your extensions' root. I can imagine some technical reasons why this is the case (e.g. sub-paths may be handled by scoped service workers, so perhaps we can only guarantee proper resolution from root?) , but I haven't had a chance to talk to the engineers about this to be sure.

FEATURE REQUEST: could we please have a direct link to this view via an Inspect link in the extension's card in chrome://extensions, just as if we were opening up the old-school background page?

It's on our radar. Feature request welcome on crbug.com if there isn't already one.

Cheers,

Simeon - @dotproto
Extensions Developer Advocate


On Friday, November 15, 2019 at 1:33:13 PM UTC-8, Kent Brewster wrote:

> Have you tried
> "background": { "service_worker": "/js/test.js" } ? With the slash in the front?

Yes, thanks! In chrome://serviceworker-internals/ I have checked the checkbox to pause JavaScript execution on service workers.

With the background script URL set to /js/background.js, I see this in the log box:

Console: {"lineNumber":0,"message":"The path of the provided scope ('/') is not under the max scope allowed ('/js/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.","message_level":3,"sourceIdentifier":8,"sourceURL":""}
Error: {"columnNumber":-1,"lineNumber":-1,"message":"Failed to register a ServiceWorker for scope ('chrome-extension://icmcfccjipehgnldlcaokbnjdlmalffm/') with script ('chrome-extension://icmcfccjipehgnldlcaokbnjdlmalffm/js/background.js'): The path of the provided scope ('/') is not under the max scope allowed ('/js/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.","sourceURL":""}

With the background script URL set to js/background.js, the log box is empty.

Along the way I've developed a workflow that will actually allow me to get to a console inside my background file, which gives me hope for the future:

- Open chrome://serviceworker-internals/
- Reload the extension
- Switch back to serviceworker-internals
- Find your service worker (should be on top)
- Click Inspect
- The resulting window's console seems to be the internals of your service worker

FEATURE REQUEST: could we please have a direct link to this view via an Inspect link in the extension's card in chrome://extensions, just as if we were opening up the old-school background page?

--Kent

KARISHMA SINGH

unread,
Jan 6, 2020, 5:29:28 AM1/6/20
to Chromium Extensions, ale...@eff.org
Is there a way to use multiple scripts in background?
 Previously background scripts were accepting list of strings but service worker accpet only string. 

For example, I have background.js, const.js and utils.js. Where functionality is defined in background.js, constants are defined in const.js and common functions are defined in utils.js.

wOxxOm

unread,
Jan 6, 2020, 5:38:24 AM1/6/20
to Chromium Extensions, ale...@eff.org
Until https://crbug.com/824647 and https://crbug.com/1020812 are implemented you'll have to use the standard synchronous importScripts() in the main service worker script, see https://developer.mozilla.org/docs/Web/API/WorkerGlobalScope/importScripts
Reply all
Reply to author
Forward
0 new messages