MV3 keep data in memory when popup closes

Each time I open my MV3 extension the javascript runs from the beginning, it’s not persistent. If I set some variable and then close the extension, when I open it again the value stored in the variable is gone.
I’m trying to keep a few variables in memory. I believe I can do something similar with, but that’s not available on Firefox, so what can I do?
Also keep in mind that the data I’m trying to get to be persistent could be sensitive, so I can’t just save it in local storage. It should be in memory while the browser is open and then get wiped.

To load the script from the manifest I’m using

"background": {
    "page": "background.html"

where background.html is a page that just loads the script. I’m doing it like this because it’s the only way I was able to get the script to load as a module.

I don’t think you can right now. Session storage is not yet implemented in Firefox:

You may need to downgrade to MV2 for now.

Or maybe use some crazy workaround, for example Alarms API is not persistent in Firefox (all alarms are removed after restarting Firefox). So you could create dummy alarm and encode the “session data” as string into alarm name :smiley: (using JSON.stringify you can encode whole objects and who knows if there is an alarm name length limit, probably not :slight_smile: ).

EDIT: now I remember discussion about making alarms persistent in MV3 (as it should be) so now I’m not sure if it’s fixed yet or not

That’s disappointing… do you have an idea of how long it will take for them to implement it?

Well, I can’t tell for sure. But currently the bug has dependency to:

which is a huge thing that’s gonna take a long time to implement.

So I would say we will be lucky to get it by the end of September when the new Firefox ESR 115 is released.

1 Like