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

@ -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);
});
});