mirror of https://github.com/gorhill/uBlock.git
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:
parent
66c70cf746
commit
bec6cad2c0
|
@ -82,6 +82,7 @@
|
||||||
"open_in_tab": true
|
"open_in_tab": true
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
|
"alarms",
|
||||||
"contextMenus",
|
"contextMenus",
|
||||||
"privacy",
|
"privacy",
|
||||||
"storage",
|
"storage",
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -90,6 +90,7 @@
|
||||||
"open_in_tab": true
|
"open_in_tab": true
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
|
"alarms",
|
||||||
"dns",
|
"dns",
|
||||||
"menus",
|
"menus",
|
||||||
"privacy",
|
"privacy",
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -1276,35 +1276,17 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
let createTimer;
|
|
||||||
let destroyTimer;
|
|
||||||
|
|
||||||
const destroy = function() {
|
const destroy = function() {
|
||||||
|
if ( µb.selfieIsInvalid === false ) {
|
||||||
io.remove(/^selfie\//);
|
io.remove(/^selfie\//);
|
||||||
µb.selfieIsInvalid = true;
|
µb.selfieIsInvalid = true;
|
||||||
createTimer = vAPI.setTimeout(( ) => {
|
|
||||||
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 };
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
Loading…
Reference in New Issue