How to reliably detect online offline events with mv3 on firefox

As the title says how do i reliably detect online offline events with mv3 on firefox. In chrome there is the offscreen document, but on firefox there is nothing like that. And it does not make sense to move the extension to mv2 to be able to use online offline events.

What are the alternatives ?

1 Like

You mean this event?

Note that Firefox doesn’t support service workers.
Instead, Firefox uses “non-persistent” background script, a.k.a. event page.
It has similar behavior like service worker (regarding lifetime).
But unlike service worker, it’s still a “normal webpage”, with full access to all “window / DOM” API.

So you probably don’t need to use a Offscreen document in Firefox, because you can use the API directly in the background script.

Does your extension need to wake up when an online/offline event fires?

1 Like

It’s been a few years since I last worked with it, but based on my experience with the offline event I wouldn’t exactly call it reliable. :grimacing:

@Ahmed_Ullah, can you share more about what you’re trying to accomplish? That is, why do you want to watch for offline events? Are you targeting desktop, mobile, or both?

While strictly speaking desktop Firefox’s event pages aren’t persistent like in MV2, we also aren’t in a rush to terminate them unnecessarily. As long as your background is doing something periodically, it should stay alive. Combine that with what @juraj.masiar shared above about being able to use the API directly from an extension’s background script, and you can probably accomplish what you’re after.

Sorry for not being clear.

So the use case is like this.

Extension is installed, event page is unloaded from memory and goes idle after some time, users internet or connection goes to the offline state, the extension does not know that this happens, and the icon does not change, the use case is for the ui to change appearance based on offline or online states.

I could poll an endpoint with alarms, but with 2 million users that does not sound like a good idea to me. I could even have a content script injected into every page and have them send me the event, which would be throttled/debounced and the ui could change, but this would have a limitation of only working when there is at least an active tab open.

With the offscreen document on chrome this is solved, i have it up and running even if the service worker dies off.

yes thats the gist of it.

In “extreme” cases, you can use workarounds to keep your service worker / event page alive indefinitely, for example placing this line into background script should “convert it” into persistent background script :slight_smile:

self.setInterval(() => browser.runtime.getPlatformInfo(), 2e4)

Note that if using ‘self.setInterval(() => browser.runtime.getPlatformInfo(), 2e4)’ to prevent an event page from terminating, the event page may immediately terminate when a computer running Windows (not Linux) awakens from sleep. The event page timer is affected by the amount of time that passed while a computer (running Windows) slept.

1 Like

Good point!
But I wonder if it’s really true, and I hope it’s not :smiley: .
And if it is, then it should be considered a bug.
This would mean, that any async function I have could be terminated at any await statement, if at that point I put my PC to sleep.

This could actually break some data consistency in some of my addons if one is very unlucky…

I remember a related discussion, but I forgot which API on which platform behaves in what way:

An event page with keep alive code may not terminate when coming out of sleep even if enough time has passed to go over 30 seconds. I’ve viewed where it terminates multiple times in a row after awakening from sleep and also when it continues running multiple times in a row.

Here is a bug report: 1834683 - Manifest v3 event page may terminate immediately after computer awakens from 30+ second sleep in Windows 10.

1 Like