Search code examples
javascriptfirefox-addonfirefox-addon-webextensions

How is browser.idle.setDetectionInterval scoped?


idle.setDetectionInterval() will set the interval used to determine when the system is in an idle state for idle.onStateChanged events, but is this scoped to the browser extension that sets it or does this change the detection interval for all browser extensions?

If it matters, I care about Firefox specifically.


Solution

  • Idle detection is scoped on a per-extension basis.

    To test, I created two extensions. One extension's idle detection was set to 15 seconds and the other to 45 seconds. In the logs, we see the second extension's idle event trigger 30 seconds after the first extension.

    Logs:

    Thu Apr 11 2019 09:52:15 GMT+0200: 15 test: initialized
    Thu Apr 11 2019 09:52:27 GMT+0200: 45 test: initialized
    
    Thu Apr 11 2019 09:52:41 GMT+0200: 15 test: idle
    Thu Apr 11 2019 09:53:11 GMT+0200: 45 test: idle
    
    Thu Apr 11 2019 09:54:00 GMT+0200: 15 test: active
    Thu Apr 11 2019 09:54:00 GMT+0200: 45 test: active
    

    First extension:

    manifest.json:

    {
        "manifest_version": 2,
        "name": "Test WebExtension 1",
        "author": "Jeremiah Lee",
        "developer": {
            "name": "Jeremiah Lee",
            "url": "https://www.jeremiahlee.com/"
        },
        "version": "1.0.0",
        "description": "Better documentation is needed",
        "homepage_url": "https://stackoverflow.com/questions/53918121/how-is-browser-idle-setdetectioninterval-scoped",
        "permissions": [
            "idle"
        ],
        "background":  {
            "scripts": ["background.js"]
        }
    }
    

    background.js:

    console.log(`${new Date()}: 15 test: initialized`);
    
    browser.idle.setDetectionInterval(15);
    
    browser.idle.onStateChanged.addListener((state) => {
        console.log(`${new Date()}: 15 test: ${state}`);
    });
    

    Second extension:

    manifest.json:

    {
        "manifest_version": 2,
        "name": "Test WebExtension 2",
        "author": "Jeremiah Lee",
        "developer": {
            "name": "Jeremiah Lee",
            "url": "https://www.jeremiahlee.com/"
        },
        "version": "2.0.0",
        "description": "Better documentation is needed",
        "homepage_url": "https://stackoverflow.com/questions/53918121/how-is-browser-idle-setdetectioninterval-scoped",
        "permissions": [
            "idle"
        ],
        "background":  {
            "scripts": ["background.js"]
        }
    }
    

    background.js:

    console.log(`${new Date()}: 45 test: initialized`);
    
    browser.idle.setDetectionInterval(45);
    
    browser.idle.onStateChanged.addListener((state) => {
        console.log(`${new Date()}: 45 test: ${state}`);
    });