2017-07-27 19:14:04 +02:00
|
|
|
/* 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(){
|
2017-07-27 19:14:04 +02:00
|
|
|
"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");
|
2017-11-07 00:36:44 +01:00
|
|
|
|
2017-10-03 12:17:14 +02:00
|
|
|
const domainNotification = require("./domainNotification");
|
|
|
|
const Notification = require("./Notification");
|
2018-09-11 23:54:59 +02:00
|
|
|
const {createActionButtons, modalPrompt, modalChoice} = require("./gui");
|
2019-04-08 00:02:29 +02:00
|
|
|
const lists = require("../lib/lists");
|
2019-05-02 00:30:30 +02:00
|
|
|
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"),
|
2017-12-23 23:49:05 +01:00
|
|
|
[
|
|
|
|
{
|
|
|
|
name: "showOptions",
|
|
|
|
isIcon: true,
|
|
|
|
callback: function(){
|
|
|
|
if (browser.runtime && browser.runtime.openOptionsPage){
|
|
|
|
browser.runtime.openOptionsPage();
|
|
|
|
}
|
|
|
|
else {
|
2021-11-01 13:18:38 +01:00
|
|
|
window.open(extension.getURL("options/options.html"), "_blank");
|
2017-12-23 23:49:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "disableNotifications",
|
|
|
|
isIcon: true,
|
2019-12-28 23:23:55 +01:00
|
|
|
callback: async function(){
|
|
|
|
await settings.set("showNotifications", false);
|
|
|
|
window.close();
|
2017-12-23 23:49:05 +01:00
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
2017-12-23 23:49:05 +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 = {
|
2021-06-08 23:42:19 +02:00
|
|
|
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},
|
|
|
|
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},
|
2018-09-11 23:54:59 +02:00
|
|
|
};
|
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){
|
2021-06-08 23:42:19 +02:00
|
|
|
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-06-25 23:22:55 +02:00
|
|
|
}
|
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}){
|
|
|
|
window.open(
|
2021-11-01 13:18:38 +01:00
|
|
|
extension.getURL(
|
2019-11-28 01:26:35 +01:00
|
|
|
"options/whitelist.html?domain=" +
|
|
|
|
encodeURIComponent(domain) +
|
|
|
|
"&urls=" +
|
|
|
|
encodeURIComponent(JSON.stringify(Array.from(urls.values())))
|
|
|
|
),
|
|
|
|
"_blank"
|
|
|
|
);
|
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;
|
2018-08-21 21:37:21 +02:00
|
|
|
}
|
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));
|
2018-08-21 21:37:21 +02:00
|
|
|
}
|
2019-12-28 23:23:55 +01:00
|
|
|
i += delta;
|
|
|
|
}
|
|
|
|
}, 1);
|
|
|
|
}
|
2017-06-29 07:21:36 +02:00
|
|
|
});
|
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
|
|
|
}());
|