1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 20:46:39 +02:00

Switch to asych/await where useful

This commit is contained in:
kkapsner 2019-12-28 23:23:55 +01:00
parent 372ee755f7
commit 10413a89c3
11 changed files with 269 additions and 349 deletions

View file

@ -114,11 +114,10 @@
}
return lists[type];
};
scope.appendTo = function appendToList(type, entry){
scope.appendTo = async function appendToList(type, entry){
const oldValue = settings[type + "List"];
return settings.set(type + "List", oldValue + (oldValue? ",": "") + entry).then(function(){
return updateList(type);
});
await settings.set(type + "List", oldValue + (oldValue? ",": "") + entry);
return updateList(type);
};
scope.update = updateList;
scope.updateAll = function updateAllLists(){

View file

@ -13,10 +13,11 @@
}
let settings = false;
scope.setSettings = function(realSettings){
scope.setSettings = async function(realSettings){
if (!settings){
settings = realSettings;
return settings.loaded.then(scope.clearQueue);
await settings.loaded;
return scope.clearQueue();
}
else {
warning("logging: Settings can only be set once.");

View file

@ -14,7 +14,7 @@
const registerSettingsContentScript = (function(){
let unregisterSettingsContentScript = function(){};
let lastRegistering;
return function registerSettingsContentScript(){
return async function registerSettingsContentScript(){
logging.message("Register content script for the settings.");
logging.verbose("Unregister old content script, if present.");
unregisterSettingsContentScript();
@ -23,7 +23,7 @@
data[def.name] = def.get();
});
lastRegistering = data;
browser.contentScripts.register({
const api = await browser.contentScripts.register({
matches: ["<all_urls>"],
matchAboutBlank: true,
allFrames: true,
@ -52,19 +52,16 @@
}
}(${JSON.stringify(data)}))`
}]
}).then(function(api){
logging.verbose("Content script registered.");
if (data !== lastRegistering){
logging.verbose("Multiple content scripts registered at once. Remove unnecessary one.");
api.unregister();
}
else {
unregisterSettingsContentScript = api.unregister;
}
return;
}).catch(function(error){
logging.warning("Unable to register content script:", error);
});
logging.verbose("Content script registered.");
if (data !== lastRegistering){
logging.verbose("Multiple content scripts registered at once. Remove unnecessary one.");
api.unregister();
}
else {
unregisterSettingsContentScript = api.unregister;
}
};
}());
@ -79,7 +76,7 @@
persistentRndStorage.init();
logging.message("register non port message listener");
browser.runtime.onMessage.addListener(function(data){
browser.runtime.onMessage.addListener(async function(data){
logging.notice("got data without port", data);
const keys = Object.keys(data);
if (data["canvasBlocker-new-domain-rnd"]){
@ -105,13 +102,9 @@
}
}
logging.notice("pass the message to the tabs");
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.tabs.sendMessage(tab.id, data);
});
return;
}).catch(function(error){
logging.warning("Unable to get browser tabs:", error);
const tabs = await browser.tabs.query({});
tabs.forEach(function(tab){
browser.tabs.sendMessage(tab.id, data);
});
});
@ -190,10 +183,9 @@
}
// mobile default settings
mobile.ifMobile(function(){
return browser.storage.local.get().then(mobile.applyMobileDefaults).catch(function(error){
logging.error("Unable to set mobile default values:", error);
});
mobile.ifMobile(async function(){
const settings = await browser.storage.local.get();
mobile.applyMobileDefaults(settings);
});
});

View file

@ -15,31 +15,29 @@
const settings = require("./settings");
const settingDefinitions = require("./settingDefinitions");
scope.isMobile = function isMobile(){
scope.isMobile = async function isMobile(){
// todo: proper mobile check (e.g. over browser.runtime.getBrowserInfo()) and no feature check
return Promise.resolve(
!browser.pageAction ||
return !browser.pageAction ||
!browser.pageAction.show ||
!browser.pageAction.openPopup
);
;
};
scope.ifMobile = function ifMobile(ifCallback, elseCallback){
return scope.isMobile().then(function(isMobile){
if (isMobile){
return ifCallback();
}
else if (elseCallback){
return elseCallback();
}
else {
return false;
}
});
scope.ifMobile = async function ifMobile(ifCallback, elseCallback){
const isMobile = await scope.isMobile();
if (isMobile){
return ifCallback();
}
else if (elseCallback){
return elseCallback();
}
else {
return false;
}
};
scope.applyMobileDefaults = function applyMobileDefaults(storage = false){
return Promise.all(settingDefinitions.filter(function(definition){
scope.applyMobileDefaults = async function applyMobileDefaults(storage = false){
await Promise.all(settingDefinitions.filter(function(definition){
return definition.hasOwnProperty("mobileDefaultValue") && (
!storage ||
!storage.hasOwnProperty(definition.name)

View file

@ -163,16 +163,12 @@
});
};
settings.on("showNotifications", function({newValue}){
settings.on("showNotifications", async function({newValue}){
if (!newValue){
logging.message("notifications were disabled -> hide all page actions");
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.pageAction.hide(tab.id);
});
return;
}).catch(function(error){
logging.warning("Unable to get browser tabs:", error);
const tabs = await browser.tabs.query({});
tabs.forEach(function(tab){
browser.pageAction.hide(tab.id);
});
}
});
@ -180,20 +176,16 @@
browser.tabs.onRemoved.addListener(function(tabId){
tabsData.delete(tabId);
});
settings.on("displayBadge", function({newValue}){
settings.on("displayBadge", async function({newValue}){
if (!newValue){
logging.message("badge was disabled -> hide all badges");
if (browser.browserAction.setBadgeText){
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.browserAction.setBadgeText({
tabId: tab.id,
text: ""
});
const tabs = await browser.tabs.query({});
tabs.forEach(function(tab){
browser.browserAction.setBadgeText({
tabId: tab.id,
text: ""
});
return;
}).catch(function(error){
logging.warning("Unable to get browser tabs:", error);
});
}
}

View file

@ -89,17 +89,13 @@
};
}();
browser.windows.onRemoved.addListener(function(){
browser.windows.getAll().then(function(windows){
if (windows.every(function(window){
return !window.incognito;
})){
clearIncognito();
}
return;
}).catch(function(error){
logging.warning("Unable to get browser windows:", error);
});
browser.windows.onRemoved.addListener(async function(){
const windows = await browser.windows.getAll();
if (windows.every(function(window){
return !window.incognito;
})){
clearIncognito();
}
});
function registerTimeout(){
@ -117,14 +113,10 @@
}
}
}
function broadcast(data){
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.tabs.sendMessage(tab.id, data);
});
return;
}).catch(function(error){
logging.warning("Unable to get browser tabs:", error);
async function broadcast(data){
const tabs = await browser.tabs.query({});
tabs.forEach(function(tab){
browser.tabs.sendMessage(tab.id, data);
});
}
function clearIncognito(){

View file

@ -129,24 +129,24 @@
}
return true;
};
const storeValue = function storeValue(newValue){
const storeValue = async function storeValue(newValue){
logging.verbose("Trying to store new value for %s", name, newValue);
settings[name] = newValue;
if (!settingDefinition.transient){
const storeObject = {};
storeObject[name] = newValue;
const promise = browser.storage.local.set(storeObject);
promise.then(function(){
try {
await browser.storage.local.set(storeObject);
logging.verbose("New value stored for %s:", name, newValue);
return;
}).catch(function(error){
}
catch (error){
logging.error("Unable to store new value for %s:", name, newValue, error);
});
return promise;
throw error;
}
}
else {
logging.warning("Transient setting %s cannot be stored.", name);
return Promise.reject("Transient setting " + name + " cannot be stored.");
throw "Transient setting " + name + " cannot be stored.";
}
};