Page 1 of 1

How to manually suspend Firefox extension's background script?

Posted: Sun Oct 27, 2024 5:58 pm
by barbaz
For converting Firefox extension from Manifest V2 to Manifest V3, the background script needs to be adapted to be non-persistent. Which means it's necessary to test that the background script behaves correctly when it gets suspended and then reloaded in a variety of states.

Such testing would be greatly facilitated by manually requesting immediate suspend of the background script.

How to do this in Firefox?

The reason to only "request" suspend is because of this:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onSuspend wrote:Note: If something prevents the event page from being unloaded, the runtime.onSuspendCanceled event will be sent and the page won't be unloaded.
... so testing this properly requires not forcing suspend in situations where an automatic suspend attempt would have been cancelled.

Thanks for any ideas.

Re: How to manually suspend Firefox extension's background script?

Posted: Sat Feb 08, 2025 10:57 pm
by barbaz
bump

Re: How to manually suspend Firefox extension's background script?

Posted: Sun Feb 09, 2025 4:07 am
by GµårÐïåñ
What's worse is that now in order to inject user-scripts into the page, you have to put it in developer mode, which is a huge suck. TamperMonkey had to deal with that. Not sure the scope affects every extension that injects, but I think at some level it might, unless it only applies to user scripts and not extension scripts, I have to read it again, been a while since the draft came out.

Re: How to manually suspend Firefox extension's background script?

Posted: Wed Feb 12, 2025 3:55 pm
by barbaz
GµårÐïåñ wrote: Sun Feb 09, 2025 4:07 am What's worse is that now in order to inject user-scripts into the page, you have to put it in developer mode,
I wasn't aware Firefox had a "developer mode" that could be put on and off? Although the Firefox builds I use have Firefox Developer Edition stuff enabled at build time, so maybe this developer mode is effectively always on for me?

Re: How to manually suspend Firefox extension's background script?

Posted: Wed Feb 12, 2025 4:16 pm
by barbaz
Back to the original subject of this thread, digging through Firefox source code turned up something interesting:
barbaz wrote: Sun Oct 27, 2024 5:58 pm manually requesting immediate suspend of the background script.
Turns out that in about:debugging, for Temporary extensions, there is a button to "Terminate background script".

But this is only available for temporary extensions. I could not find this option because I was installing test extensions the normal way, running the browser in a disposable sandbox that gets dumped on quit and only installing the test extension in the disposable-sandboxed browser instance.

Looks like temporary extensions can be installed from xpi, and installing a temporary extension "on top of" the permanent one looks to work the same as with the normal install method Image
barbaz wrote: Sun Oct 27, 2024 5:58 pm The reason to only "request" suspend is because of this:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onSuspend wrote:Note: If something prevents the event page from being unloaded, the runtime.onSuspendCanceled event will be sent and the page won't be unloaded.
... so testing this properly requires not forcing suspend in situations where an automatic suspend attempt would have been cancelled.
Not sure if the button maybe more forceful than automatic suspend in this way, but the fact there is a UI for this (as opposed to needing to write code in the Browser Console) would seem to indicate that any such differences, if any, are likely unimportant for testing.