Get the service worker status active or inactive

1,437 views
Skip to first unread message

Abdullah Al Naiem

unread,
Feb 18, 2022, 4:14:59 PM2/18/22
to Chromium Extensions
I'm using setInterval in the service worker, I already posted my approach here https://groups.google.com/a/chromium.org/g/chromium-extensions/c/3d27JflxyU8/m/D0w2PSHxAQAJ

How can get service worker status from the popup and options page of my extension?

PhistucK

unread,
Feb 19, 2022, 10:09:42 AM2/19/22
to Abdullah Al Naiem, Chromium Extensions
I believe service workers are active as long as the pages they "control" are active... Am I way off?

PhistucK


On Fri, Feb 18, 2022 at 9:15 PM Abdullah Al Naiem <naie...@gmail.com> wrote:
I'm using setInterval in the service worker, I already posted my approach here https://groups.google.com/a/chromium.org/g/chromium-extensions/c/3d27JflxyU8/m/D0w2PSHxAQAJ

How can get service worker status from the popup and options page of my extension?

--
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/816c20bb-701b-407f-a9a2-84d69d62db8fn%40chromium.org.

wOxxOm

unread,
Feb 19, 2022, 10:10:34 AM2/19/22
to Chromium Extensions, naie...@gmail.com
You can use messaging via chrome.runtime or BroadcastChannel or even navigator.serviceWorker.

Note that service worker is terminated after 30 seconds similarly to the event page in MV2 so your best course of action is to rework the extension as explained in MV3 introduction https://developer.chrome.com/docs/extensions/mv3/migrating_to_service_workers/

It won't work with timers shorter than 1 minute or with a per-second precision though, so in that case you'll have to force the worker to persist, see https://stackoverflow.com/q/66618136, but that's not 100% reliable as explained there. Hopefully, in the future there will be a solution, but so far the extensions team didn't even officially acknowledge the importance of the multiple use cases they broke in MV3 so it's quite possible these problems will be around for years.

wOxxOm

unread,
Feb 19, 2022, 10:16:07 AM2/19/22
to Chromium Extensions, PhistucK, Chromium Extensions, naie...@gmail.com
>  I believe service workers are active as long as the pages they "control" are active... Am I way off?

It terminates after 30 seconds by default or 5 minutes in case of an open port via chrome API like sendMessage/connect, and the only exception in the new Chrome  is chrome.runtime.connectNative that keeps the worker forever.

Service worker doesn't control the extension origin unless an extension uses `fetch` event, which they don't normally. This is one of the reasons why choosing service worker for the background script environment seems a big mistake as the main purpose of SW is unused in 99.999% of practical cases.

Abdullah Al Naiem

unread,
Feb 19, 2022, 6:59:42 PM2/19/22
to Chromium Extensions, wOxxOm, PhistucK, Chromium Extensions, Abdullah Al Naiem
navigator.serviceWorker returns the same result for service worker active and inactive both
and which versions of new Chrome support this chrome.runtime.connectNative?

wOxxOm

unread,
Feb 20, 2022, 8:12:39 AM2/20/22
to Chromium Extensions, naie...@gmail.com, wOxxOm, PhistucK, Chromium Extensions
Well, don't take my words for gospel, investigate it first :-) There should be many examples on how to check if the service worker is running and how to communicate with it. In short, it's navigator.serviceWorker.controller to check if the service worker is running; to start it you would use await navigator.serviceWorker.ready; to post a message use postMessage on controller and self.onmessage inside the worker.

connectNative behavior was changed several days ago, in Chrome 100, and usually such things don't get back-merged into current versions of Chrome.

Simeon Vincent

unread,
Feb 25, 2022, 9:01:12 PM2/25/22
to wOxxOm, Chromium Extensions, naie...@gmail.com, PhistucK
Let's take a step back. Why do you want to know whether the extensions service worker is running? What are you trying to accomplish?

Simeon - @dotproto
Chrome Extensions DevRel


Abdullah Al Naiem

unread,
Feb 26, 2022, 1:50:31 AM2/26/22
to Chromium Extensions, Simeon Vincent, Chromium Extensions, Abdullah Al Naiem, PhistucK, wOxxOm
@Simeon - Thank you for your query. I already posted my previous conversation link. My extension has some functionality where users can set a time to work the extension and it's necessary. From my side, I set setInterval for every 25 seconds, and when the time is matched with a user preferred time the extension will do the work, or the extension will execute the content script. But the service worker isn't active all time that's why the extension is unable to check the user time is matching or not in the background. It's a kind of a "To-do" concept. Here's the previous conversation link https://groups.google.com/a/chromium.org/g/chromium-extensions/c/3d27JflxyU8/m/D0w2PSHxAQAJ
Reply all
Reply to author
Forward
0 new messages