1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-03 12:06:31 +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

@ -35,23 +35,18 @@
logging.notice("send message to main script");
extension.message.send({"canvasBlocker-clear-domain-rnd": true});
},
clearPersistentRndForContainer: function(){
browser.contextualIdentities.query({}).then(function(identities){
return modal.select(
extension.getTranslation("clearPersistentRndForContainer_title"),
identities.map(function(identity){
return {
name: `${identity.name} (${identity.cookieStoreId})`,
object: identity
};
})
);
}).then(function(identity){
extension.message.send({"canvasBlocker-clear-container-rnd": identity.cookieStoreId});
return;
}).catch(function(error){
logging.warning("Unable to clear persistent rnd for container:", error);
});
clearPersistentRndForContainer: async function(){
const identities = await browser.contextualIdentities.query({});
const identity = await modal.select(
extension.getTranslation("clearPersistentRndForContainer_title"),
identities.map(function(identity){
return {
name: `${identity.name} (${identity.cookieStoreId})`,
object: identity
};
})
);
extension.message.send({"canvasBlocker-clear-container-rnd": identity.cookieStoreId});
},
inspectSettings: function(){
logging.verbose("open settings inspection");
@ -96,9 +91,9 @@
logging.verbose("open whitelist inspection");
window.open("whitelist.html", "_blank");
},
loadSettings: function(){
loadSettings: async function(){
logging.verbose("load settings");
new Promise(function(resolve, reject){
const text = await new Promise(function(resolve, reject){
const input = document.createElement("input");
input.type = "file";
input.addEventListener("change", function(){
@ -114,32 +109,27 @@
}
});
input.click();
}).then(function(text){
return JSON.parse(text);
}).then(function(json){
while (settingsMigration.transitions.hasOwnProperty(json.storageVersion)){
let oldVersion = json.storageVersion;
json = settingsMigration.transitions[json.storageVersion](json);
if (oldVersion === json.storageVersion){
break;
}
});
let json = JSON.parse(text);
while (settingsMigration.transitions.hasOwnProperty(json.storageVersion)){
let oldVersion = json.storageVersion;
json = settingsMigration.transitions[json.storageVersion](json);
if (oldVersion === json.storageVersion){
break;
}
const keys = Object.keys(json);
keys.forEach(function(key){
const setting = settings.getDefinition(key);
if (!settings){
throw new Error("Unknown setting " + key + ".");
}
if (!setting.fixed && setting.invalid(json[key])){
throw new Error("Invalid value " + json[key] + " for " + key + ".");
}
});
keys.forEach(function(key){
settings[key] = json[key];
});
return;
}).catch(function(error){
alert(error);
}
const keys = Object.keys(json);
keys.forEach(function(key){
const setting = settings.getDefinition(key);
if (!settings){
throw new Error("Unknown setting " + key + ".");
}
if (!setting.fixed && setting.invalid(json[key])){
throw new Error("Invalid value " + json[key] + " for " + key + ".");
}
});
keys.forEach(function(key){
settings[key] = json[key];
});
},
resetSettings: async function(){
@ -165,18 +155,7 @@
}
};
new Promise(function(resolve){
const port = browser.runtime.connect();
port.onMessage.addListener(function(data){
if (data.hasOwnProperty("tabId")){
logging.notice("my tab id is", data.tabId);
port.disconnect();
resolve(data.tabId);
}
});
}).then(function(tabId){
return browser.tabs.get(tabId);
}).then(function(tab){
browser.tabs.getCurrent().then(function(tab){
document.querySelector("head title").textContent = extension.getTranslation("options_title");
let head = document.createElement("header");
document.body.insertBefore(head, document.body.firstChild);
@ -236,7 +215,7 @@
linkDiv.appendChild(link);
head.appendChild(linkDiv);
}
return;
return undefined;
}).catch(function(error){
logging.warning("Unable to identify tab:", error);
});
@ -569,7 +548,7 @@
return response.json();
}).then(function(manifest){
version.textContent = "Version " + manifest.version;
return;
return manifest.version;
}).catch(function(error){
version.textContent = "Unable to get version: " + error;
});
@ -578,7 +557,7 @@
settings.onloaded(function(){
const reCaptchaEntry = "^https://www\\.google\\.com/recaptcha/api2/(?:b?frame|anchor).*$";
const {url: urlContainer} = settings.getContainers();
settings.on("protectWindow", function({newValue}){
settings.on("protectWindow", async function({newValue}){
if (newValue){
const urlValue = urlContainer.get();
const matching = urlValue.filter(function(entry){
@ -591,20 +570,16 @@
matching[0].protectWindow
)
){
modal.confirm(
const addException = await modal.confirm(
extension.getTranslation("protectWindow_askReCaptchaException"),
{
node: document.querySelector("[data-storage-name=protectWindow]"),
selector: ".settingRow .content"
}
).then(function(addException){
if (addException){
settings.set("protectWindow", false, reCaptchaEntry);
}
return;
}).catch(function(error){
logging.warning("Error while adding reCaptcha exception:", error);
});
);
if (addException){
settings.set("protectWindow", false, reCaptchaEntry);
}
}
}
});

View file

@ -1,7 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
(async function(){
"use strict";
const extension = require("../lib/extension");
@ -76,8 +76,8 @@
if (settingsList.childNodes.length){
const button = document.createElement("button");
button.textContent = extension.getTranslation("apply");
button.addEventListener("click", function(){
Promise.all(Object.keys(preset).map(function(settingName){
button.addEventListener("click", async function(){
await Promise.all(Object.keys(preset).map(function(settingName){
const value = preset[settingName];
if ((typeof value) === "object"){
return Promise.all(Object.keys(value).map(function(url){
@ -87,12 +87,9 @@
else {
return settings.set(settingName, value);
}
})).then(function(){
window.location.reload();
return;
}).catch(function(error){
logging.warning("Unable to apply preset:", error);
});
}));
window.location.reload();
});
container.appendChild(button);
}
@ -100,20 +97,7 @@
return container;
}
Promise.all([
settings.loaded,
fetch("presets.json").then(function(data){
return data.json();
})
// eslint-disable-next-line no-unused-vars
]).then(function([settingsLoaded, presets]){
Object.keys(presets).map(function(presetName){
return buildPresetGui(presetName, presets[presetName]);
}).forEach(function(node){
document.body.appendChild(node);
});
// fit content to the window size
function fitContentToWindowSize(){
if (window.innerHeight > document.body.getBoundingClientRect().bottom){
const computedStyle = window.getComputedStyle(document.body);
const availableHeight = window.innerHeight - parseFloat(computedStyle.marginBottom);
@ -141,10 +125,7 @@
document.body.style.fontSize = fontSize + "px";
}
}
return;
}).catch(function(error){
logging.warning("Unable to setup presets:", error);
});
}
document.querySelector("head title").textContent = extension.getTranslation("presets_title");
let head = document.createElement("header");
@ -153,7 +134,7 @@
let heading = document.createElement("h1");
heading.textContent = extension.getTranslation("presets");
head.appendChild(heading);
if (searchParameters.has("notice")){
const noticeName = `presets_${searchParameters.get("notice")}Notice`;
const noticeText = extension.getTranslation(noticeName);
@ -195,4 +176,17 @@
introduction.className = "introduction";
introduction.textContent = extension.getTranslation("presets_introduction");
head.appendChild(introduction);
const [settingsLoaded, presets] = await Promise.all([
settings.loaded,
(await fetch("presets.json")).json()
]);
Object.keys(presets).map(function(presetName){
return buildPresetGui(presetName, presets[presetName]);
}).forEach(function(node){
document.body.appendChild(node);
});
fitContentToWindowSize();
}());