Start using browser.alarms instead of setTimeout() where applicable

Related documentation:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Background_scripts#convert_to_non-persistent
This commit is contained in:
Raymond Hill 2023-04-07 20:24:20 -04:00
parent 66c70cf746
commit bec6cad2c0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
5 changed files with 65 additions and 25 deletions

View File

@ -82,6 +82,7 @@
"open_in_tab": true "open_in_tab": true
}, },
"permissions": [ "permissions": [
"alarms",
"contextMenus", "contextMenus",
"privacy", "privacy",
"storage", "storage",

View File

@ -95,6 +95,61 @@ vAPI.storage = webext.storage.local;
/******************************************************************************/ /******************************************************************************/
/******************************************************************************/ /******************************************************************************/
vAPI.alarms = {
create(callback) {
this.uniqueIdGenerator += 1;
const name = this.uniqueIdGenerator.toString(36);
const client = new this.Client(name, callback);
this.clientMap.set(name, client);
return client;
},
Client: class {
constructor(name, callback) {
this.name = name;
this.callback = callback;
}
on(delay) {
const delayInMinutes = this.normalizeDelay(delay);
browser.alarms.get(this.name, alarm => {
if ( alarm ) { return; }
return browser.alarms.create(this.name, { delayInMinutes });
});
}
offon(delay) {
const delayInMinutes = this.normalizeDelay(delay);
return browser.alarms.create(this.name, { delayInMinutes });
}
off() {
return browser.alarms.clear(this.name);
}
normalizeDelay(delay) {
let delayInMinutes = 0;
if ( typeof delay === 'number' ) {
delayInMinutes = delay;
} else if ( typeof delay === 'object' ) {
if ( delay.sec !== undefined ) {
delayInMinutes = delay.sec / 60;
} else if ( delay.ms !== undefined ) {
delayInMinutes = delay.ms / 60000;
}
}
return Math.max(delayInMinutes, 1);
}
},
onAlarm(alarm) {
const client = this.clientMap.get(alarm.name);
if ( client === undefined ) { return; }
client.callback(alarm);
},
clientMap: new Map(),
uniqueIdGenerator: 1000000,
};
browser.alarms.onAlarm.addListener(alarm => vAPI.alarms.onAlarm(alarm));
/******************************************************************************/
/******************************************************************************/
// https://github.com/gorhill/uMatrix/issues/234 // https://github.com/gorhill/uMatrix/issues/234
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network

View File

@ -90,6 +90,7 @@
"open_in_tab": true "open_in_tab": true
}, },
"permissions": [ "permissions": [
"alarms",
"dns", "dns",
"menus", "menus",
"privacy", "privacy",

View File

@ -78,6 +78,7 @@
"name": "uBlock Origin", "name": "uBlock Origin",
"options_page": "dashboard.html", "options_page": "dashboard.html",
"permissions": [ "permissions": [
"alarms",
"contextMenus", "contextMenus",
"privacy", "privacy",
"storage", "storage",

View File

@ -1276,35 +1276,17 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
return false; return false;
}; };
let createTimer;
let destroyTimer;
const destroy = function() { const destroy = function() {
io.remove(/^selfie\//); if ( µb.selfieIsInvalid === false ) {
µb.selfieIsInvalid = true; io.remove(/^selfie\//);
createTimer = vAPI.setTimeout(( ) => { µb.selfieIsInvalid = true;
createTimer = undefined;
create();
}, µb.hiddenSettings.selfieAfter * 60000);
};
const destroyAsync = function() {
if ( destroyTimer !== undefined ) { return; }
if ( createTimer !== undefined ) {
clearTimeout(createTimer);
createTimer = undefined;
} }
destroyTimer = vAPI.setTimeout( createAlarm.offon(µb.hiddenSettings.selfieAfter);
( ) => {
destroyTimer = undefined;
destroy();
},
1019
);
µb.selfieIsInvalid = true;
}; };
µb.selfieManager = { load, destroy: destroyAsync }; const createAlarm = vAPI.alarms.create(create);
µb.selfieManager = { load, destroy };
} }
/******************************************************************************/ /******************************************************************************/