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/. */
|
2017-11-07 00:36:44 +01:00
|
|
|
(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");
|
|
|
|
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = require("../lib/logging");
|
2017-11-07 00:36:44 +01:00
|
|
|
setLogPrefix("page action script");
|
|
|
|
|
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");
|
2017-10-03 12:17:14 +02:00
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
Promise.all([
|
|
|
|
browser.tabs.query({active: true, currentWindow: true}),
|
|
|
|
settings.loaded
|
|
|
|
]).then(function(values){
|
2018-08-28 08:29:01 +02:00
|
|
|
// load theme
|
|
|
|
var themeLink = document.createElement("link");
|
|
|
|
themeLink.href = `pageAction-${settings.theme}.css`;
|
|
|
|
themeLink.rel = "stylesheet";
|
|
|
|
themeLink.type = "text/css";
|
|
|
|
document.head.appendChild(themeLink);
|
|
|
|
settings.on("theme", function(){
|
|
|
|
themeLink.href = `pageAction-${settings.theme}.css`;
|
|
|
|
});
|
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
const tabs = values[0];
|
|
|
|
|
|
|
|
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 {
|
|
|
|
window.open(browser.extension.getURL("options/options.html"), "_blank");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "disableNotifications",
|
|
|
|
isIcon: true,
|
|
|
|
callback: function(){
|
2018-07-17 12:58:52 +02:00
|
|
|
settings.set("showNotifications", false).then(function(){
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
if (!tabs.length){
|
|
|
|
throw new Error("noTabsFound");
|
2017-10-03 12:17:14 +02:00
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
else if (tabs.length > 1){
|
|
|
|
error(tabs);
|
|
|
|
throw new Error("tooManyTabsFound");
|
|
|
|
}
|
|
|
|
|
2018-09-11 23:54:59 +02:00
|
|
|
function domainOrUrlPicker(domain, urls, selectText, urlInputText){
|
|
|
|
const choices = Array.from(urls).map(function(url){
|
|
|
|
return {
|
|
|
|
text: url,
|
|
|
|
value: "^" + url.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "$"
|
|
|
|
};
|
|
|
|
});
|
|
|
|
choices.unshift(domain);
|
|
|
|
return modalChoice(
|
|
|
|
selectText,
|
|
|
|
choices
|
|
|
|
).then(function(choice){
|
|
|
|
if (choice.startsWith("^")){
|
|
|
|
return modalPrompt(
|
|
|
|
urlInputText,
|
|
|
|
choice
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
verbose("registering domain actions");
|
|
|
|
[
|
|
|
|
{
|
2018-09-11 23:54:59 +02:00
|
|
|
name: "ignorelist",
|
2017-11-07 00:36:44 +01:00
|
|
|
isIcon: true,
|
2018-09-11 23:54:59 +02:00
|
|
|
callback: function({domain, urls}){
|
|
|
|
domainOrUrlPicker(
|
|
|
|
domain,
|
|
|
|
urls,
|
2019-04-09 08:29:52 +02:00
|
|
|
extension.getTranslation("selectIgnore"),
|
|
|
|
extension.getTranslation("inputIgnoreURL")
|
2018-09-11 23:54:59 +02:00
|
|
|
).then(function(choice){
|
|
|
|
if (choice){
|
|
|
|
settings.set("showNotifications", false, choice).then(function(){
|
2018-07-17 12:58:52 +02:00
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.close();
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2018-09-11 23:54:59 +02:00
|
|
|
name: "whitelist",
|
2017-11-07 00:36:44 +01:00
|
|
|
isIcon: true,
|
2019-04-30 23:42:21 +02:00
|
|
|
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}
|
|
|
|
|
|
|
|
};
|
2018-09-11 23:54:59 +02:00
|
|
|
domainOrUrlPicker(
|
|
|
|
domain,
|
|
|
|
urls,
|
2019-04-09 08:29:52 +02:00
|
|
|
extension.getTranslation("selectWhitelist"),
|
|
|
|
extension.getTranslation("inputWhitelistURL")
|
2018-09-11 23:54:59 +02:00
|
|
|
).then(function(choice){
|
2019-04-30 23:42:21 +02:00
|
|
|
const allAPIs = {
|
|
|
|
choice,
|
|
|
|
setting: "blockMode",
|
|
|
|
settingValue: "allow"
|
|
|
|
};
|
|
|
|
const onlyAPI = {
|
|
|
|
choice,
|
|
|
|
setting: whitelistingSettings[api],
|
|
|
|
settingValue: false
|
|
|
|
};
|
|
|
|
if (
|
|
|
|
api &&
|
|
|
|
whitelistingSettings[api]
|
|
|
|
){
|
|
|
|
return modalChoice(
|
|
|
|
extension.getTranslation("selectWhitelistScope"),
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: extension.getTranslation("whitelistOnlyAPI")
|
|
|
|
.replace(/\{api\}/g, 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}){
|
2018-09-11 23:54:59 +02:00
|
|
|
if (choice){
|
2019-04-30 23:42:21 +02:00
|
|
|
settings.set(setting.name, setting.value, choice).then(function(){
|
2018-07-17 12:58:52 +02:00
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.close();
|
2017-11-07 00:36:44 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-07-17 13:07:50 +02:00
|
|
|
},
|
|
|
|
{
|
2018-09-11 23:54:59 +02:00
|
|
|
name: "whitelistTemporarily",
|
2018-07-17 13:07:50 +02:00
|
|
|
isIcon: true,
|
2018-09-11 23:54:59 +02:00
|
|
|
callback: function({domain, urls}){
|
|
|
|
domainOrUrlPicker(
|
|
|
|
domain,
|
|
|
|
urls,
|
2019-04-09 08:29:52 +02:00
|
|
|
extension.getTranslation("selectSessionWhitelist"),
|
|
|
|
extension.getTranslation("inputSessionWhitelistURL")
|
2018-09-11 23:54:59 +02:00
|
|
|
).then(function(choice){
|
|
|
|
if (choice){
|
|
|
|
lists.appendTo("sessionWhite", choice).then(function(){
|
2018-07-17 13:07:50 +02:00
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-03 12:17:14 +02:00
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
].forEach(function(domainAction){
|
|
|
|
domainNotification.addAction(domainAction);
|
|
|
|
});
|
|
|
|
|
|
|
|
verbose("registering notification actions");
|
|
|
|
[
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
var tab = tabs[0];
|
2019-04-30 23:42:21 +02:00
|
|
|
extension.message.on(function(data){
|
2018-09-11 23:54:59 +02:00
|
|
|
if (data["canvasBlocker-notificationCounter"]){
|
|
|
|
const url = new URL(data.url);
|
|
|
|
Object.keys(data["canvasBlocker-notificationCounter"]).forEach(function(key){
|
|
|
|
const notification = domainNotification(
|
|
|
|
url,
|
|
|
|
key,
|
2019-04-30 23:42:21 +02:00
|
|
|
data["canvasBlocker-notificationCounter"][key].count,
|
|
|
|
data["canvasBlocker-notificationCounter"][key].api
|
2018-09-11 23:54:59 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
Array.isArray(data["canvasBlocker-notifications"]) &&
|
|
|
|
data["canvasBlocker-notifications"].length
|
|
|
|
){
|
2017-11-07 00:36:44 +01:00
|
|
|
message("got notifications");
|
2018-08-21 21:37:21 +02:00
|
|
|
const notifications = data["canvasBlocker-notifications"];
|
|
|
|
let i = 0;
|
|
|
|
const length = notifications.length;
|
|
|
|
const tick = window.setInterval(function(){
|
|
|
|
if (i >= length){
|
|
|
|
window.clearInterval(tick);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (var delta = 0; delta < 20 && i + delta < length; delta += 1){
|
|
|
|
let notification = notifications[i + delta];
|
2018-09-11 23:54:59 +02:00
|
|
|
verbose(notification);
|
2018-08-24 16:47:27 +02:00
|
|
|
if (settings.ignoredAPIs[notification.api]){
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-21 21:37:21 +02:00
|
|
|
verbose(notification);
|
|
|
|
notification.url = new URL(notification.url);
|
|
|
|
domainNotification(
|
2018-09-11 23:54:59 +02:00
|
|
|
notification.url,
|
2019-04-30 23:42:21 +02:00
|
|
|
notification.messageId,
|
|
|
|
0,
|
|
|
|
notification.api
|
2018-08-21 21:37:21 +02:00
|
|
|
).addNotification(new Notification(notification));
|
|
|
|
}
|
|
|
|
i += delta;
|
|
|
|
}
|
|
|
|
}, 1);
|
2017-10-03 12:17:14 +02:00
|
|
|
}
|
2017-11-07 00:36:44 +01:00
|
|
|
});
|
|
|
|
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);
|
2017-06-29 07:21:36 +02:00
|
|
|
});
|
2017-11-07 00:36:44 +01:00
|
|
|
}());
|