2017-07-27 19:14:04 +02:00
|
|
|
/* jslint moz: true */
|
|
|
|
/* 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-07-18 16:19:32 +02:00
|
|
|
|
2017-07-02 12:21:16 +02:00
|
|
|
Promise.all([
|
|
|
|
browser.tabs.query({active: true, currentWindow: true}),
|
|
|
|
browser.storage.local.get().then(function(data){
|
|
|
|
Object.keys(data).forEach(function(key){
|
|
|
|
settings[key] = data[key];
|
|
|
|
});
|
|
|
|
return settings;
|
|
|
|
})
|
2017-07-27 19:14:04 +02:00
|
|
|
]).then(function(values){
|
|
|
|
"use strict";
|
|
|
|
const [tabs, settings] = values;
|
|
|
|
|
|
|
|
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = require("./logging");
|
|
|
|
setLogPrefix("page action script");
|
|
|
|
|
|
|
|
function modalPrompt(message, defaultValue){
|
|
|
|
message("open modal prompt");
|
|
|
|
return new Promise(function(resolve, reject){
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
document.body.appendChild(document.createTextNode(message));
|
|
|
|
var input = document.createElement("input");
|
|
|
|
input.value = defaultValue;
|
|
|
|
document.body.appendChild(input);
|
|
|
|
var button = document.createElement("button");
|
|
|
|
button.textContent = "OK";
|
|
|
|
button.addEventListener("click", function(){
|
|
|
|
resolve(input.value);
|
|
|
|
message("modal prompt closed with value", input.value);
|
|
|
|
});
|
|
|
|
document.body.appendChild(button);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-29 07:21:36 +02:00
|
|
|
if (!tabs.length){
|
|
|
|
throw new Error("noTabsFound");
|
|
|
|
}
|
2017-07-02 12:21:16 +02:00
|
|
|
else if (tabs.length > 1){
|
|
|
|
console.error(tabs);
|
|
|
|
throw new Error("tooManyTabsFound");
|
|
|
|
}
|
|
|
|
|
|
|
|
const lists = require("./lists");
|
2017-07-07 08:46:58 +02:00
|
|
|
lists.updateAll();
|
2017-07-02 12:21:16 +02:00
|
|
|
const {parseErrorStack} = require("./callingStack");
|
|
|
|
var actionsCallbacks = {
|
|
|
|
displayFullURL: function({url}){
|
|
|
|
alert(url.href);
|
|
|
|
},
|
|
|
|
displayCallingStack: function({errorStack}){
|
|
|
|
alert(parseErrorStack(errorStack));
|
|
|
|
},
|
2017-07-18 16:19:32 +02:00
|
|
|
inspectImage: function({dataURL}){
|
|
|
|
document.body.innerHTML = "<img src=" + dataURL + ">";
|
|
|
|
},
|
2017-07-02 12:21:16 +02:00
|
|
|
ignorelistDomain: function({url}){
|
|
|
|
var domain = url.host;
|
|
|
|
modalPrompt(
|
|
|
|
browser.i18n.getMessage("inputIgnoreDomain"),
|
|
|
|
url.host
|
|
|
|
).then(function(domain){
|
|
|
|
if (domain){
|
|
|
|
lists.appendTo("ignore", domain);
|
|
|
|
}
|
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
whitelistURL: function({url}){
|
|
|
|
modalPrompt(
|
|
|
|
browser.i18n.getMessage("inputWhitelistDomain"),
|
|
|
|
"^" + url.href.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "$"
|
|
|
|
).then(function(url){
|
|
|
|
if (url){
|
|
|
|
lists.appendTo("white", url);
|
|
|
|
}
|
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
whitelistDomain: function({url}){
|
|
|
|
modalPrompt(
|
|
|
|
browser.i18n.getMessage("inputWhitelistURL"),
|
|
|
|
url.host
|
|
|
|
).then(function(domain){
|
|
|
|
if (domain){
|
|
|
|
lists.appendTo("white", domain);
|
|
|
|
}
|
|
|
|
window.close();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
disableNotifications: function(notification){
|
|
|
|
browser.storage.local.set({showNotifications: false});
|
|
|
|
window.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-06-29 07:21:36 +02:00
|
|
|
var tab = tabs[0];
|
|
|
|
browser.runtime.onMessage.addListener(function(data){
|
|
|
|
if (Array.isArray(data["canvasBlocker-notifications"])){
|
2017-07-27 19:14:04 +02:00
|
|
|
message("got notifications");
|
2017-06-29 07:21:36 +02:00
|
|
|
var ul = document.getElementById("prints");
|
|
|
|
data["canvasBlocker-notifications"].forEach(function(notification){
|
2017-07-27 19:14:04 +02:00
|
|
|
verbose(notification);
|
2017-07-02 12:21:16 +02:00
|
|
|
|
|
|
|
var url = new URL(notification.url);
|
2017-06-29 07:21:36 +02:00
|
|
|
var li = document.createElement("li");
|
|
|
|
li.className = "print";
|
2017-07-02 12:21:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
li.appendChild(document.createTextNode(" "));
|
|
|
|
|
|
|
|
var messageSpan = document.createElement("span");
|
|
|
|
var messageParts = browser.i18n.getMessage(notification.messageId).split(/\{url\}/g);
|
|
|
|
messageSpan.appendChild(document.createTextNode(messageParts.shift()));
|
|
|
|
while (messageParts.length){
|
|
|
|
var urlSpan = document.createElement("span");
|
|
|
|
urlSpan.textContent = url.hostname;
|
|
|
|
urlSpan.title = url.href;
|
|
|
|
messageSpan.appendChild(urlSpan);
|
|
|
|
messageSpan.appendChild(document.createTextNode(messageParts.shift()));
|
|
|
|
}
|
|
|
|
messageSpan.title = notification.timestamp + ": " + notification.functionName;
|
|
|
|
li.appendChild(messageSpan);
|
|
|
|
|
|
|
|
li.appendChild(document.createTextNode(" "));
|
|
|
|
|
|
|
|
var actions = document.createElement("span");
|
|
|
|
actions.className = "actions";
|
2017-07-18 16:19:32 +02:00
|
|
|
var data = {url, errorStack: notification.errorStack, notification, dataURL: notification.dataURL};
|
|
|
|
Object.keys(actionsCallbacks).forEach(function(key, i){
|
2017-07-02 12:21:16 +02:00
|
|
|
var button = document.createElement("button");
|
2017-07-18 16:19:32 +02:00
|
|
|
button.className = key;
|
2017-07-02 12:21:16 +02:00
|
|
|
button.textContent = browser.i18n.getMessage(key);
|
|
|
|
button.addEventListener("click", function(){actionsCallbacks[key](data);});
|
|
|
|
actions.appendChild(button);
|
2017-07-18 16:19:32 +02:00
|
|
|
if (i % 3 === 2){
|
|
|
|
actions.appendChild(document.createElement("br"));
|
|
|
|
}
|
2017-07-02 12:21:16 +02:00
|
|
|
});
|
2017-07-08 22:40:38 +02:00
|
|
|
if (notification.dataURL){
|
2017-07-18 16:19:32 +02:00
|
|
|
actions.classList.add("imageAvailable");
|
2017-07-08 22:40:38 +02:00
|
|
|
}
|
2017-07-02 12:21:16 +02:00
|
|
|
li.appendChild(actions);
|
|
|
|
|
2017-06-29 07:21:36 +02:00
|
|
|
ul.appendChild(li);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("clearing the display");
|
2017-07-18 16:19:32 +02:00
|
|
|
var ul = document.getElementById("prints");
|
|
|
|
ul.innerHTML = "";
|
2017-07-27 19:14:04 +02:00
|
|
|
message("request notifications from tab", tab.id);
|
2017-06-29 07:21:36 +02:00
|
|
|
browser.tabs.sendMessage(
|
|
|
|
tab.id,
|
|
|
|
{
|
|
|
|
"canvasBlocker-sendNotifications": tab.id
|
|
|
|
}
|
|
|
|
);
|
2017-07-27 19:14:04 +02:00
|
|
|
notice("waiting for notifications");
|
2017-06-29 07:21:36 +02:00
|
|
|
}).catch(function(e){
|
|
|
|
console.error(e);
|
|
|
|
});
|