1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/pageAction/pageAction.js

297 lines
7.5 KiB
JavaScript
Raw Normal View History

/* 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/. */
2019-12-28 23:23:55 +01:00
(async function(){
"use strict";
2017-11-07 00:36:44 +01:00
2019-04-09 08:29:52 +02:00
const extension = require("../lib/extension");
2019-04-08 00:02:29 +02:00
const settings = require("../lib/settings");
const {parseErrorStack} = require("../lib/callingStack");
2019-11-28 01:26:35 +01:00
const logging = require("../lib/logging");
logging.setPrefix("page action script");
2024-03-28 14:43:53 +01:00
window.addEventListener("load", async function(){
extension.displayVersion("version", 250);
});
2017-11-07 00:36:44 +01:00
2017-10-03 12:17:14 +02:00
const domainNotification = require("./domainNotification");
const Notification = require("./Notification");
const {createActionButtons, modalPrompt, modalChoice} = require("./gui");
2019-04-08 00:02:29 +02:00
const lists = require("../lib/lists");
require("../lib/theme").init("pageAction");
2019-11-28 01:26:35 +01:00
function registerActionButtons(){
logging.notice("create global action buttons");
2017-10-03 12:17:14 +02:00
2017-11-07 00:36:44 +01:00
createActionButtons(
document.getElementById("globalActions"),
[
{
name: "showOptions",
isIcon: true,
callback: function(){
if (browser.runtime && browser.runtime.openOptionsPage){
browser.runtime.openOptionsPage();
}
else {
browser.tabs.create({url: extension.getURL("options/options.html")});
}
}
},
{
name: "disableNotifications",
isIcon: true,
2019-12-28 23:23:55 +01:00
callback: async function(){
await settings.set("showNotifications", false);
window.close();
}
2017-11-07 00:36:44 +01:00
}
],
undefined,
true
2017-11-07 00:36:44 +01:00
);
2019-11-28 01:26:35 +01:00
}
const domainActions = [
{
name: "ignorelist",
isIcon: true,
2019-12-28 23:23:55 +01:00
callback: async function({domain, urls}){
const choice = await domainOrUrlPicker(
2019-11-28 01:26:35 +01:00
domain,
urls,
extension.getTranslation("selectIgnore"),
extension.getTranslation("inputIgnoreURL")
2019-12-28 23:23:55 +01:00
);
if (choice){
await settings.set("showNotifications", false, choice);
}
window.close();
2019-11-28 01:26:35 +01:00
}
},
{
name: "whitelist",
isIcon: true,
2019-12-28 23:23:55 +01:00
callback: async function({domain, urls, api}){
2019-11-28 01:26:35 +01:00
const whitelistingSettings = {
all: {name: "blockMode", value: "allowEverything", fakeValue: "fake"},
2019-11-28 01:26:35 +01:00
canvas: {name: "protectedCanvasPart", value: "nothing"},
audio: {name: "protectAudio", value: false},
domRect: {name: "protectDOMRect", value: false},
svg: {name: "protectSVG", value: false},
2019-11-28 01:26:35 +01:00
history: {name: "historyLengthThreshold", value: 10000},
navigator: {name: "protectNavigator", value: false},
2019-12-09 00:31:08 +01:00
windows: {name: "protectWindow", value: false},
screen: {name: "protectScreen", value: false},
};
2019-12-28 23:23:55 +01:00
const choice = await domainOrUrlPicker(
2019-11-28 01:26:35 +01:00
domain,
urls,
extension.getTranslation("selectWhitelist"),
extension.getTranslation("inputWhitelistURL")
2019-12-28 23:23:55 +01:00
);
let setting = whitelistingSettings.all;
if (
api &&
whitelistingSettings[api]
){
setting = whitelistingSettings[await modalChoice(
extension.getTranslation("selectWhitelistScope"),
[
{
text: extension.getTranslation("whitelistOnlyAPI")
.replace(
/\{api\}/g,
extension.getTranslation("section_" + api + "-api")
),
value: api
},
{
text: extension.getTranslation("whitelistAllAPIs"),
value: "all"
}
]
)];
}
if (choice){
if (setting === whitelistingSettings.all && settings.get(setting.name, choice).startsWith("block")){
setting.value = await modalChoice(
extension.getTranslation("selectWhitelistType"),
[
{
text: extension.getTranslation("blockMode_options." + setting.value),
value: setting.value
},
{
text: extension.getTranslation("blockMode_options." + setting.fakeValue),
value: setting.fakeValue
}
]
);
}
2019-12-28 23:23:55 +01:00
await settings.set(setting.name, setting.value, choice);
}
window.close();
}
2019-11-28 01:26:35 +01:00
},
{
name: "whitelistTemporarily",
isIcon: true,
2019-12-28 23:23:55 +01:00
callback: async function({domain, urls}){
const choice = await domainOrUrlPicker(
2019-11-28 01:26:35 +01:00
domain,
urls,
extension.getTranslation("selectSessionWhitelist"),
extension.getTranslation("inputSessionWhitelistURL")
2019-12-28 23:23:55 +01:00
);
if (choice){
await lists.appendTo("sessionWhite", choice);
}
window.close();
2019-11-28 01:26:35 +01:00
}
},
{
name: "inspectWhitelist",
isIcon: true,
callback: function({domain, urls}){
browser.tabs.create({url: extension.getURL(
"options/whitelist.html?domain=" +
encodeURIComponent(domain) +
"&urls=" +
encodeURIComponent(JSON.stringify(Array.from(urls.values())))
)});
2017-10-03 12:17:14 +02:00
}
2019-11-28 01:26:35 +01:00
}
];
function registerDomainActions(){
logging.verbose("registering domain actions");
domainActions.forEach(function(domainAction){
2017-11-07 00:36:44 +01:00
domainNotification.addAction(domainAction);
});
2019-11-28 01:26:35 +01:00
}
function registerNotificationActions(){
logging.verbose("registering notification actions");
2017-11-07 00:36:44 +01:00
[
{
name: "displayFullURL",
isIcon: true,
callback: function({url}){
alert(url.href);
}
},
{
name: "displayCallingStack",
isIcon: true,
callback: function({errorStack}){
alert(parseErrorStack(errorStack));
}
2017-10-03 12:17:14 +02:00
}
2017-11-07 00:36:44 +01:00
].forEach(function(action){
Notification.addAction(action);
});
2019-11-28 01:26:35 +01:00
}
2019-12-28 23:23:55 +01:00
async function domainOrUrlPicker(domain, urls, selectText, urlInputText){
2019-11-28 01:26:35 +01:00
const choices = Array.from(urls).map(function(url){
return {
text: url,
value: "^" + url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "$"
};
});
if (domain){
choices.unshift(domain);
}
2019-12-28 23:23:55 +01:00
const choice = await modalChoice(
2019-11-28 01:26:35 +01:00
selectText,
choices
2019-12-28 23:23:55 +01:00
);
if (choice.startsWith("^")){
return modalPrompt(
urlInputText,
choice
);
}
else {
return choice;
}
2019-11-28 01:26:35 +01:00
}
2019-12-28 23:23:55 +01:00
const values = await Promise.all([
2019-11-28 01:26:35 +01:00
browser.tabs.query({active: true, currentWindow: true}),
settings.loaded
2019-12-28 23:23:55 +01:00
]);
const tabs = values[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){
domainNotification(
url,
key,
data["canvasBlocker-notificationCounter"][key].count,
data["canvasBlocker-notificationCounter"][key].api
);
});
2019-11-28 01:26:35 +01:00
}
2019-12-28 23:23:55 +01:00
if (
Array.isArray(data["canvasBlocker-notifications"]) &&
data["canvasBlocker-notifications"].length
){
logging.message("got notifications");
const notifications = data["canvasBlocker-notifications"];
let i = 0;
const length = notifications.length;
const tick = window.setInterval(function(){
if (i >= length){
window.clearInterval(tick);
}
else {
let delta = 0;
for (; delta < 20 && i + delta < length; delta += 1){
let notification = notifications[i + delta];
logging.verbose(notification);
if (settings.ignoredAPIs[notification.api]){
continue;
}
2019-12-28 23:23:55 +01:00
logging.verbose(notification);
notification.url = new URL(notification.url);
domainNotification(
notification.url,
notification.messageId,
0,
notification.api
).addNotification(new Notification(notification));
}
2019-12-28 23:23:55 +01:00
i += delta;
}
}, 1);
}
});
2019-12-28 23:23:55 +01:00
logging.message("request notifications from tab", tab.id);
browser.tabs.sendMessage(
tab.id,
{
"canvasBlocker-sendNotifications": tab.id
}
);
logging.notice("waiting for notifications");
2017-11-07 00:36:44 +01:00
}());