How to make sure background script starts with extension?

346 views
Skip to first unread message

Juraj M.

unread,
Dec 20, 2023, 4:02:08 AM12/20/23
to Chromium Extensions
I think the background script in MV3 stays sleeping until there is actual event.
But let's say I want to record "start time" in my session storage, how to do it properly?

There is "chrome.runtime.onStartup.addListener" which would help in most cases, but I guess if user disables / enables extension or when extension updates, it won't fire.

Is there some other way to force background script to start when the extension loads?

Juraj M.

unread,
Dec 20, 2023, 4:46:23 AM12/20/23
to Chromium Extensions, Juraj M.
OK, never mind, this has been actually discussed many times, we are waiting for "onSessionStart" or "onExtensionLoaded" API or something similar:

Until then I just create a bunch of dummy event handlers to make sure one of them fires when extension starts.

hrg...@gmail.com

unread,
Dec 20, 2023, 9:40:28 AM12/20/23
to Chromium Extensions, Juraj M.
The service worker must always start immediately after the extension is re-enabled, because the background script file may have changed from the last time it ran.
If this is not the case, then this is absolutely a bug, and it must be fixed.

Juraj M.

unread,
Dec 20, 2023, 9:47:33 AM12/20/23
to Chromium Extensions, hrg...@gmail.com, Juraj M.
Interesting!
Does that mean the service worker will auto-start also after extension update? (even if it wasn't changed).

So all I need to do is register one "chrome.runtime.onStartup.addListener" listener and my background script will always start when the extension starts?

hrg...@gmail.com

unread,
Dec 20, 2023, 9:52:33 AM12/20/23
to Chromium Extensions, Juraj M., hrg...@gmail.com
One thing is the "onStartup" event and another thing is when the service worker must run.
The service worker must run whenever the extension is loaded/reloaded/updated/enabled/re-enabled.

The "onStartup" event, on the other hand, must run only when the browser profile starts up.

Juraj M.

unread,
Dec 20, 2023, 10:07:04 AM12/20/23
to Chromium Extensions, hrg...@gmail.com, Juraj M.
I know about the the "onStartup", but I have it there only to force the service worker to run when the browser starts.
Are you saying the top level of the service worker is executed automatically when browser starts? For all extensions?
This can't be right. When I start Chrome I see many extensions with "inactive" worker (way before the 30s timeout).

hrg...@gmail.com

unread,
Dec 20, 2023, 10:44:45 AM12/20/23
to Chromium Extensions, Juraj M., hrg...@gmail.com
If the "onStartup" event has an event listener, then the next time the browser profile starts, the service worker will run too.
If there's no onStartup event listener, then the service worker will NOT run when the browser profile starts up.

However, the service worker MUST run regardless of any event listeners, on the following cases:
  • when the extension is first loaded
  • when the extension is reloaded
  • when the extension is updated
  • when the extension is enabled/re-enabled

Juraj M.

unread,
Dec 20, 2023, 10:53:14 AM12/20/23
to Chromium Extensions, hrg...@gmail.com, Juraj M.
Thank you for clearing it up for me!
The life cycle of the SW is a bit more complicated than I expected.
Also, when you say "first loaded", you mean "installed", right? That confused me too since "first loaded" sound like when browser starts :).

hrg...@gmail.com

unread,
Dec 20, 2023, 10:55:50 AM12/20/23
to Chromium Extensions, Juraj M., hrg...@gmail.com
Yes, I meant installed from the CWS or installed locally (i.e. loaded unpacked).
Reply all
Reply to author
Forward
0 new messages