If a Firefox add-on extension sets an alarm using browser.alarms.create and then the user quits Firefox, will the alarm still exist the next time Firefox is opened?

If so, will alarms created using delayInMinutes be triggered immediately if the delay has been exceeded?

If so, will alarms created using periodInMinutes continue to be triggered based on the time of their initial firing before the browser was quit or the time of the browser being opened?

1 Answers

0
JeremiahLee On Best Solutions

Alarms do not persist across browser sessions.

The code below, when signed and installed (temporarily loaded extensions get unloaded when the browser quits), will output:

Extension running. Current alarms:
Array []
background.js:4:13
2019-03-16T17:15:10.841Z: Alarm should go off every minute. background.js:13:13
2019-03-16T17:16:10.912Z: test-alarm-1 background.js:16:17
2019-03-16T17:17:10.919Z: test-alarm-1 background.js:16:17
2019-03-16T17:18:10.922Z: test-alarm-1 background.js:16:17

​// Restart Firefox, reopen debugging.

Extension running. Current alarms: 
Array []
background.js:4:13
2019-03-16T17:18:40.689Z: Alarm should go off every minute. background.js:13:13
2019-03-16T17:19:40.796Z: test-alarm-1 background.js:16:17

If alarms persisted, the array would have 1 item.

manifest.json:

{
    "manifest_version": 2,
    "name": "Test WebExtension",
    "author": "Jeremiah Lee",
    "developer": {
        "name": "Jeremiah Lee",
        "url": "https://www.jeremiahlee.com/"
    },
    "version": "0.1.0",
    "description": "Better documentation is needed",
    "homepage_url": "https://stackoverflow.com/questions/55173095/do-alarms-persist-when-quitting-firefox",
    "permissions": [
        "<all_urls>",
        "alarms",
        "tabs"
    ],
    "background": {
        "page": "./lib/background.html"
    }
}

background.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="module" src="./background.js"></script>
</head>
</html>

background.js:

(async function() {
    const alarms = await browser.alarms.getAll();

    console.log(`Extension running. Current alarms:`, alarms);

    await browser.alarms.create(
        "test-alarm-1",
        {
            periodInMinutes: 1
        }
    );

    console.log(`${new Date().toISOString()}: Alarm should go off every minute.`);

    function handleAlarm(alarmInfo) {
        console.log(`${new Date().toISOString()}: ${alarmInfo.name}`);
    }

    browser.alarms.onAlarm.addListener(handleAlarm)
})();