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

142 lines
3.4 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/. */
2017-11-07 00:36:44 +01:00
(function(){
"use strict";
2017-11-07 00:36:44 +01:00
const settings = require("./settings");
const {parseErrorStack} = require("./callingStack");
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = require("./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");
const {createActionButtons, modalPrompt} = require("./gui");
2017-11-07 00:36:44 +01:00
Promise.all([
browser.tabs.query({active: true, currentWindow: true}),
settings.loaded
]).then(function(values){
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"),
[{
name: "disableNotifications",
isIcon: true,
callback: function(){
settings.showNotifications = false;
2017-10-03 12:17:14 +02:00
window.close();
2017-11-07 00:36:44 +01:00
}
}],
undefined
);
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");
}
2017-10-03 12:17:14 +02:00
2017-11-07 00:36:44 +01:00
verbose("registering domain actions");
[
{
name: "ignorelistDomain",
isIcon: true,
callback: function(domain){
modalPrompt(
browser.i18n.getMessage("inputIgnoreDomain"),
domain
).then(function(domain){
if (domain){
2017-12-03 23:58:54 +01:00
settings.set("showNotifications", false, domain);
2017-11-07 00:36:44 +01:00
}
window.close();
});
}
},
{
name: "whitelistDomain",
isIcon: true,
callback: function(domain){
modalPrompt(
browser.i18n.getMessage("inputWhitelistURL"),
domain
).then(function(domain){
if (domain){
2017-12-03 23:58:54 +01:00
settings.set("blockMode", "allow", domain);
2017-11-07 00:36:44 +01:00
}
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));
}
},
{
name: "whitelistURL",
isIcon: true,
callback: function({url}){
modalPrompt(
browser.i18n.getMessage("inputWhitelistDomain"),
"^" + url.href.replace(/([\\+*?[^\]$(){}=!|.])/g, "\\$1") + "$"
).then(function(url){
if (url){
2017-12-03 23:58:54 +01:00
settings.set("blockMode", "allow", url);
2017-11-07 00:36:44 +01:00
}
window.close();
});
}
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];
browser.runtime.onMessage.addListener(function(data){
if (Array.isArray(data["canvasBlocker-notifications"])){
message("got notifications");
data["canvasBlocker-notifications"].forEach(function(notification){
verbose(notification);
notification.url = new URL(notification.url);
domainNotification(
notification.url.hostname,
notification.messageId
).addNotification(new Notification(notification));
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-11-07 00:36:44 +01:00
}());