mirror of
https://github.com/gorhill/uBlock.git
synced 2024-11-10 01:02:08 +01:00
Reverse usage of browser.alarms
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2604
Related commit:
- bec6cad2c0
This commit is contained in:
parent
ef825245b9
commit
348f75af7f
6 changed files with 2 additions and 61 deletions
|
@ -82,7 +82,6 @@
|
|||
"open_in_tab": true
|
||||
},
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"contextMenus",
|
||||
"privacy",
|
||||
"storage",
|
||||
|
|
|
@ -126,61 +126,6 @@ if ( webext.storage.local.getBytesInUse instanceof Function ) {
|
|||
/******************************************************************************/
|
||||
/******************************************************************************/
|
||||
|
||||
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; }
|
||||
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://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network
|
||||
|
||||
|
|
|
@ -90,7 +90,6 @@
|
|||
"open_in_tab": true
|
||||
},
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"dns",
|
||||
"menus",
|
||||
"privacy",
|
||||
|
|
|
@ -78,7 +78,6 @@
|
|||
"name": "uBlock Origin",
|
||||
"options_page": "dashboard.html",
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"contextMenus",
|
||||
"privacy",
|
||||
"storage",
|
||||
|
|
|
@ -66,7 +66,6 @@
|
|||
"open_in_tab": true
|
||||
},
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"privacy",
|
||||
"storage",
|
||||
"tabs",
|
||||
|
|
|
@ -1283,10 +1283,10 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
|
|||
io.remove(/^selfie\//);
|
||||
µb.selfieIsInvalid = true;
|
||||
}
|
||||
createAlarm.offon(µb.hiddenSettings.selfieAfter);
|
||||
createTimer.offon({ min: µb.hiddenSettings.selfieAfter });
|
||||
};
|
||||
|
||||
const createAlarm = vAPI.alarms.create(create);
|
||||
const createTimer = vAPI.defer.create(create);
|
||||
|
||||
µb.selfieManager = { load, destroy };
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue