mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Big linting
This commit is contained in:
parent
b5e6d049ce
commit
aef6bd3d59
58 changed files with 2074 additions and 1856 deletions
|
@ -7,22 +7,17 @@
|
|||
const extension = require("../lib/extension");
|
||||
const settings = require("../lib/settings");
|
||||
const {parseErrorStack} = require("../lib/callingStack");
|
||||
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = require("../lib/logging");
|
||||
setLogPrefix("page action script");
|
||||
const logging = require("../lib/logging");
|
||||
logging.setPrefix("page action script");
|
||||
|
||||
const domainNotification = require("./domainNotification");
|
||||
const Notification = require("./Notification");
|
||||
const {createActionButtons, modalPrompt, modalChoice} = require("./gui");
|
||||
const lists = require("../lib/lists");
|
||||
require("../lib/theme").init("pageAction");
|
||||
|
||||
Promise.all([
|
||||
browser.tabs.query({active: true, currentWindow: true}),
|
||||
settings.loaded
|
||||
]).then(function(values){
|
||||
const tabs = values[0];
|
||||
|
||||
notice("create global action buttons");
|
||||
|
||||
function registerActionButtons(){
|
||||
logging.notice("create global action buttons");
|
||||
|
||||
createActionButtons(
|
||||
document.getElementById("globalActions"),
|
||||
|
@ -44,7 +39,9 @@
|
|||
isIcon: true,
|
||||
callback: function(){
|
||||
settings.set("showNotifications", false).then(function(){
|
||||
window.close();
|
||||
return window.close();
|
||||
}).catch(function(error){
|
||||
logging.warning("Unable to disable notifications:", error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -52,164 +49,130 @@
|
|||
undefined,
|
||||
true
|
||||
);
|
||||
|
||||
if (!tabs.length){
|
||||
throw new Error("noTabsFound");
|
||||
}
|
||||
else if (tabs.length > 1){
|
||||
error(tabs);
|
||||
throw new Error("tooManyTabsFound");
|
||||
}
|
||||
|
||||
function domainOrUrlPicker(domain, urls, selectText, urlInputText){
|
||||
const choices = Array.from(urls).map(function(url){
|
||||
return {
|
||||
text: url,
|
||||
value: "^" + url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "$"
|
||||
}
|
||||
|
||||
const domainActions = [
|
||||
{
|
||||
name: "ignorelist",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls}){
|
||||
return domainOrUrlPicker(
|
||||
domain,
|
||||
urls,
|
||||
extension.getTranslation("selectIgnore"),
|
||||
extension.getTranslation("inputIgnoreURL")
|
||||
).then(function(choice){
|
||||
if (choice){
|
||||
return settings.set("showNotifications", false, choice);
|
||||
}
|
||||
return;
|
||||
}).then(function(){
|
||||
return window.close();
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "whitelist",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls, api}){
|
||||
const whitelistingSettings = {
|
||||
all: {name: "blockMode", value: "allow"},
|
||||
canvas: {name: "protectedCanvasPart", value: "nothing"},
|
||||
audio: {name: "protectAudio", value: false},
|
||||
domRect: {name: "protectDOMRect", value: false},
|
||||
history: {name: "historyLengthThreshold", value: 10000},
|
||||
navigator: {name: "protectNavigator", value: false},
|
||||
windows: {name: "protectWindow", value: false}
|
||||
|
||||
};
|
||||
});
|
||||
if (domain){
|
||||
choices.unshift(domain);
|
||||
return domainOrUrlPicker(
|
||||
domain,
|
||||
urls,
|
||||
extension.getTranslation("selectWhitelist"),
|
||||
extension.getTranslation("inputWhitelistURL")
|
||||
).then(function(choice){
|
||||
if (
|
||||
api &&
|
||||
whitelistingSettings[api]
|
||||
){
|
||||
// eslint-disable-next-line promise/no-nesting
|
||||
return modalChoice(
|
||||
extension.getTranslation("selectWhitelistScope"),
|
||||
[
|
||||
{
|
||||
text: extension.getTranslation("whitelistOnlyAPI")
|
||||
.replace(
|
||||
/\{api\}/g,
|
||||
extension.getTranslation("section_" + api + "-api")
|
||||
),
|
||||
value: api
|
||||
},
|
||||
{
|
||||
text: extension.getTranslation("whitelistAllAPIs"),
|
||||
value: "all"
|
||||
}
|
||||
]
|
||||
).then(function(selection){
|
||||
return {choice, setting: whitelistingSettings[selection]};
|
||||
});
|
||||
}
|
||||
else {
|
||||
return {choice, setting: whitelistingSettings.all};
|
||||
}
|
||||
}).then(function({choice, setting}){
|
||||
if (choice){
|
||||
return settings.set(setting.name, setting.value, choice);
|
||||
}
|
||||
return;
|
||||
}).then(function(){
|
||||
return window.close();
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "whitelistTemporarily",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls}){
|
||||
return domainOrUrlPicker(
|
||||
domain,
|
||||
urls,
|
||||
extension.getTranslation("selectSessionWhitelist"),
|
||||
extension.getTranslation("inputSessionWhitelistURL")
|
||||
).then(function(choice){
|
||||
if (choice){
|
||||
return lists.appendTo("sessionWhite", choice);
|
||||
}
|
||||
return;
|
||||
}).then(function(){
|
||||
return window.close();
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "inspectWhitelist",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls}){
|
||||
window.open(
|
||||
browser.extension.getURL(
|
||||
"options/whitelist.html?domain=" +
|
||||
encodeURIComponent(domain) +
|
||||
"&urls=" +
|
||||
encodeURIComponent(JSON.stringify(Array.from(urls.values())))
|
||||
),
|
||||
"_blank"
|
||||
);
|
||||
}
|
||||
return modalChoice(
|
||||
selectText,
|
||||
choices
|
||||
).then(function(choice){
|
||||
if (choice.startsWith("^")){
|
||||
return modalPrompt(
|
||||
urlInputText,
|
||||
choice
|
||||
);
|
||||
}
|
||||
else {
|
||||
return choice;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
verbose("registering domain actions");
|
||||
[
|
||||
{
|
||||
name: "ignorelist",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls}){
|
||||
domainOrUrlPicker(
|
||||
domain,
|
||||
urls,
|
||||
extension.getTranslation("selectIgnore"),
|
||||
extension.getTranslation("inputIgnoreURL")
|
||||
).then(function(choice){
|
||||
if (choice){
|
||||
settings.set("showNotifications", false, choice).then(function(){
|
||||
window.close();
|
||||
});
|
||||
}
|
||||
else {
|
||||
window.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "whitelist",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls, api}){
|
||||
const whitelistingSettings = {
|
||||
all: {name: "blockMode", value: "allow"},
|
||||
canvas: {name: "protectedCanvasPart", value: "nothing"},
|
||||
audio: {name: "protectAudio", value: false},
|
||||
domRect: {name: "protectDOMRect", value: false},
|
||||
history: {name: "historyLengthThreshold", value: 10000},
|
||||
navigator: {name: "protectNavigator", value: false},
|
||||
windows: {name: "protectWindow", value: false}
|
||||
|
||||
};
|
||||
domainOrUrlPicker(
|
||||
domain,
|
||||
urls,
|
||||
extension.getTranslation("selectWhitelist"),
|
||||
extension.getTranslation("inputWhitelistURL")
|
||||
).then(function(choice){
|
||||
if (
|
||||
api &&
|
||||
whitelistingSettings[api]
|
||||
){
|
||||
return modalChoice(
|
||||
extension.getTranslation("selectWhitelistScope"),
|
||||
[
|
||||
{
|
||||
text: extension.getTranslation("whitelistOnlyAPI")
|
||||
.replace(
|
||||
/\{api\}/g,
|
||||
extension.getTranslation("section_" + api + "-api")
|
||||
),
|
||||
value: api
|
||||
},
|
||||
{
|
||||
text: extension.getTranslation("whitelistAllAPIs"),
|
||||
value: "all"
|
||||
}
|
||||
]
|
||||
).then(function(selection){
|
||||
return {choice, setting: whitelistingSettings[selection]};
|
||||
});
|
||||
}
|
||||
else {
|
||||
return {choice, setting: whitelistingSettings.all};
|
||||
}
|
||||
}).then(function({choice, setting}){
|
||||
if (choice){
|
||||
settings.set(setting.name, setting.value, choice).then(function(){
|
||||
window.close();
|
||||
});
|
||||
}
|
||||
else {
|
||||
window.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "whitelistTemporarily",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls}){
|
||||
domainOrUrlPicker(
|
||||
domain,
|
||||
urls,
|
||||
extension.getTranslation("selectSessionWhitelist"),
|
||||
extension.getTranslation("inputSessionWhitelistURL")
|
||||
).then(function(choice){
|
||||
if (choice){
|
||||
lists.appendTo("sessionWhite", choice).then(function(){
|
||||
window.close();
|
||||
});
|
||||
}
|
||||
else {
|
||||
window.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "inspectWhitelist",
|
||||
isIcon: true,
|
||||
callback: function({domain, urls}){
|
||||
window.open(
|
||||
browser.extension.getURL(
|
||||
"options/whitelist.html?domain=" +
|
||||
encodeURIComponent(domain) +
|
||||
"&urls=" +
|
||||
encodeURIComponent(JSON.stringify(Array.from(urls.values())))
|
||||
),
|
||||
"_blank"
|
||||
);
|
||||
}
|
||||
}
|
||||
].forEach(function(domainAction){
|
||||
];
|
||||
function registerDomainActions(){
|
||||
logging.verbose("registering domain actions");
|
||||
domainActions.forEach(function(domainAction){
|
||||
domainNotification.addAction(domainAction);
|
||||
});
|
||||
|
||||
verbose("registering notification actions");
|
||||
}
|
||||
|
||||
function registerNotificationActions(){
|
||||
logging.verbose("registering notification actions");
|
||||
[
|
||||
{
|
||||
name: "displayFullURL",
|
||||
|
@ -228,13 +191,60 @@
|
|||
].forEach(function(action){
|
||||
Notification.addAction(action);
|
||||
});
|
||||
}
|
||||
|
||||
function domainOrUrlPicker(domain, urls, selectText, urlInputText){
|
||||
const choices = Array.from(urls).map(function(url){
|
||||
return {
|
||||
text: url,
|
||||
value: "^" + url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "$"
|
||||
};
|
||||
});
|
||||
if (domain){
|
||||
choices.unshift(domain);
|
||||
}
|
||||
return modalChoice(
|
||||
selectText,
|
||||
choices
|
||||
).then(function(choice){
|
||||
if (choice.startsWith("^")){
|
||||
return modalPrompt(
|
||||
urlInputText,
|
||||
choice
|
||||
);
|
||||
}
|
||||
else {
|
||||
return choice;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Promise.all([
|
||||
browser.tabs.query({active: true, currentWindow: true}),
|
||||
settings.loaded
|
||||
]).then(function(values){
|
||||
const tabs = values[0];
|
||||
|
||||
var tab = tabs[0];
|
||||
if (!tabs.length){
|
||||
throw new Error("noTabsFound");
|
||||
}
|
||||
else if (tabs.length > 1){
|
||||
logging.error(tabs);
|
||||
throw new Error("tooManyTabsFound");
|
||||
}
|
||||
|
||||
registerActionButtons();
|
||||
|
||||
registerDomainActions();
|
||||
|
||||
registerNotificationActions();
|
||||
|
||||
const tab = tabs[0];
|
||||
extension.message.on(function(data){
|
||||
if (data["canvasBlocker-notificationCounter"]){
|
||||
const url = new URL(data.url);
|
||||
Object.keys(data["canvasBlocker-notificationCounter"]).forEach(function(key){
|
||||
const notification = domainNotification(
|
||||
domainNotification(
|
||||
url,
|
||||
key,
|
||||
data["canvasBlocker-notificationCounter"][key].count,
|
||||
|
@ -246,7 +256,7 @@
|
|||
Array.isArray(data["canvasBlocker-notifications"]) &&
|
||||
data["canvasBlocker-notifications"].length
|
||||
){
|
||||
message("got notifications");
|
||||
logging.message("got notifications");
|
||||
const notifications = data["canvasBlocker-notifications"];
|
||||
let i = 0;
|
||||
const length = notifications.length;
|
||||
|
@ -255,13 +265,14 @@
|
|||
window.clearInterval(tick);
|
||||
}
|
||||
else {
|
||||
for (var delta = 0; delta < 20 && i + delta < length; delta += 1){
|
||||
let delta = 0;
|
||||
for (; delta < 20 && i + delta < length; delta += 1){
|
||||
let notification = notifications[i + delta];
|
||||
verbose(notification);
|
||||
logging.verbose(notification);
|
||||
if (settings.ignoredAPIs[notification.api]){
|
||||
continue;
|
||||
}
|
||||
verbose(notification);
|
||||
logging.verbose(notification);
|
||||
notification.url = new URL(notification.url);
|
||||
domainNotification(
|
||||
notification.url,
|
||||
|
@ -275,15 +286,16 @@
|
|||
}, 1);
|
||||
}
|
||||
});
|
||||
message("request notifications from tab", tab.id);
|
||||
logging.message("request notifications from tab", tab.id);
|
||||
browser.tabs.sendMessage(
|
||||
tab.id,
|
||||
{
|
||||
"canvasBlocker-sendNotifications": tab.id
|
||||
}
|
||||
);
|
||||
notice("waiting for notifications");
|
||||
}).catch(function(e){
|
||||
error(e);
|
||||
logging.notice("waiting for notifications");
|
||||
return;
|
||||
}).catch(function(error){
|
||||
error(error);
|
||||
});
|
||||
}());
|
Loading…
Add table
Add a link
Reference in a new issue