How to manually suspend Firefox extension's background script?

General discussion about web technology.
Post Reply
barbaz
Senior Member
Posts: 11002
Joined: Sat Aug 03, 2013 5:45 pm

How to manually suspend Firefox extension's background script?

Post 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.
*Always* check the changelogs BEFORE updating that important software!
Mozilla/5.0 (X11; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0
barbaz
Senior Member
Posts: 11002
Joined: Sat Aug 03, 2013 5:45 pm

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

Post by barbaz »

bump
*Always* check the changelogs BEFORE updating that important software!
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
User avatar
GµårÐïåñ
Lieutenant Colonel
Posts: 3366
Joined: Fri Mar 20, 2009 5:19 am
Location: PST - USA
Contact:

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

Post 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.
~.:[ Lï£ê ï§ å Lêmðñ åñÐ Ì Wåñ† M¥ Mðñê¥ ßå¢k ]:.~
________________ .: [ Major Mike's ] :. ________________
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0
barbaz
Senior Member
Posts: 11002
Joined: Sat Aug 03, 2013 5:45 pm

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

Post 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?
*Always* check the changelogs BEFORE updating that important software!
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
barbaz
Senior Member
Posts: 11002
Joined: Sat Aug 03, 2013 5:45 pm

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

Post 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.
*Always* check the changelogs BEFORE updating that important software!
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Post Reply