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.
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.
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.
@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.masiarshared above about being able to use the API directly from an extensionâs background script, and you can probably accomplish what youâre after.
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.
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
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.
Good point!
But I wonder if itâs really true, and I hope itâs not .
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.