javascriptandroidfirefoxfirefox-addon-webextensionsfirefox-android

FireFox Android: how long does a webextensions background script last?


Specifically, if I crate a variable in my background.js, and I close firefox (to the point that all tabs must be re-loaded, but are still saved), will the variable still exist in the state I left it?

In my specific case, I want to store some data about tabs whenever they update. I can't rely on the data being there whenever I query the tabs, so I must get it as soon as they are updated. If I have a global object, will it retain info about the tabs that were updated before FF Android was closed and re-opened, or will it be re-set to empty, and begin filling up again when new tabs are updated. Simplified:

var count = 0;

browser.tabs.onUpdate.adListener(function()
{
    count = count + 1;
});

//----------------------
// user updates a 3 tabs
//----------------------

console.log(count); // this prints 3

//===========================
// FF android is killed here!
//===========================
// FF android is re-opened
//===========================

//-------------------------
// user updates 3 more tabs
//-------------------------

console.log(count); // does this print 3 or 6?

Solution

  • A WebExtension's background script is just another (invisible) tab.

    If you restart Firefox to the point that all tabs are reloaded, that invisible tab is reloaded; there should be no special treatment for persistence there - your code is essentially run again from a blank state.

    If you do need persistence yourself, you need to use something like browser.storage API.