1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-11 07:38:58 +01:00
CanvasBlocker/lib/main.js

238 lines
7.0 KiB
JavaScript
Raw Normal View History

2014-10-14 01:06:11 +02:00
/* global console */
2015-01-16 13:01:01 +01: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/. */
2014-08-20 10:21:38 +02:00
(function(){
2014-10-14 01:06:11 +02:00
"use strict";
require("./stylePreferencePane");
require("./disableWithoutDocumentElement");
2014-08-20 10:21:38 +02:00
var self = require("sdk/self");
var pageMod = require("sdk/page-mod");
var array = require("sdk/util/array");
2014-08-20 10:21:38 +02:00
var preferences = require("sdk/simple-prefs");
var prefService = require("sdk/preferences/service");
2014-08-20 10:21:38 +02:00
var prefs = preferences.prefs;
2014-10-14 01:06:11 +02:00
var URL = require("sdk/url").URL;
2014-08-20 10:21:38 +02:00
var _ = require("sdk/l10n").get;
var tabUtils = require("sdk/tabs/utils");
2014-08-01 12:03:23 +02:00
var sharedFunctions = require("./sharedFunctions");
var getDomainRegExpList = sharedFunctions.getDomainRegExpList;
2014-08-20 10:21:38 +02:00
// preferences
Object.keys(prefs).forEach(function(pref){
preferences.on(pref, function(){
workers.forEach(checkWorker);
});
});
2014-08-20 10:21:38 +02:00
var whiteList;
function updateWhiteList(){
whiteList = getDomainRegExpList(prefs.whiteList);
}
2014-08-01 12:03:23 +02:00
updateWhiteList();
2014-08-20 10:21:38 +02:00
preferences.on("whiteList", function(){
updateWhiteList();
});
2014-07-31 03:05:51 +02:00
2014-08-20 10:21:38 +02:00
var blackList;
function updateBlackList(){
blackList = getDomainRegExpList(prefs.blackList);
}
2014-08-11 18:05:56 +02:00
updateBlackList();
2014-08-20 10:21:38 +02:00
preferences.on("blackList", function(){
updateBlackList();
});
2015-04-23 10:04:20 +02:00
var ignoreList;
function updateIgnoreList(){
ignoreList = getDomainRegExpList(prefs.ignoreList);
}
updateIgnoreList();
preferences.on("ignoreList", function(){
updateIgnoreList();
});
2014-10-11 00:07:51 +02:00
2014-12-15 18:43:47 +01:00
// preferences for injected file
var preferencesForInjected = ["showCallingFile", "showCompleteCallingStack"];
preferencesForInjected.forEach(function(name){
preferences.on(name, function(){
workers.forEach(function(worker){
worker.port.emit("set", name, prefs[name]);
});
});
});
2014-10-11 00:07:51 +02:00
function checkURL(url){
return sharedFunctions.checkURL(url, prefs.blockMode, whiteList, blackList);
2014-10-11 00:07:51 +02:00
}
function checkWorker(worker){
try {
2015-04-15 11:39:35 +02:00
var mode;
var url = new URL(worker.url);
2015-04-15 11:39:35 +02:00
if (
(url.protocol === "about:") ||
(prefs.allowPDFCanvas && worker.tab && worker.tab.contentType.match(/\/pdf$/i))
2015-04-15 11:39:35 +02:00
){
mode = "unblock";
}
else {
mode = checkURL(url);
2015-04-15 11:39:35 +02:00
}
worker.port.emit(mode, prefs.askOnlyOnce);
}
catch (e){
2015-09-03 00:05:43 +02:00
// console.log("Error updating " + worker.url + ": " + e.message);
2014-08-01 12:03:23 +02:00
}
2014-07-31 03:05:51 +02:00
}
var workers = [];
2015-04-24 01:04:14 +02:00
var workerTranslations = {
sourceOutput: _("sourceOutput"),
stackEntryOutput: _("stackEntryOutput")
};
["", "Readout"].forEach(function(type){
["", "Visible", "Invisible"].forEach(function(visibility){
var text = "askFor" + visibility + type + "Permission";
workerTranslations[text] = _(text);
});
});
var workerOptions = {
blockMode: checkURL(),
whiteList: prefs.whiteList,
blackList: prefs.blackList,
2015-04-24 01:04:14 +02:00
askOnce: prefs.askOnce,
translations: workerTranslations
};
preferences.on("blockMode", function(){
workerOptions.blockMode = checkURL();
});
["whiteList", "blackList", "askOnce"].forEach(function(prefName){
preferences.on(prefName, function(){
workerOptions[prefName] = prefs[prefName];
});
});
2014-08-20 10:21:38 +02:00
pageMod.PageMod({
include: "*",
contentScriptWhen: "start",
contentScriptFile: [
self.data.url("sharedFunctions.js").replace("/data/", "/lib/"),
self.data.url("inject.js"),
],
contentScriptOptions: workerOptions,
2014-08-20 10:21:38 +02:00
onAttach: function(worker){
array.add(workers, worker);
worker.on("pageshow", function(){
array.add(workers, this);
});
worker.on("pagehide", function(){
array.remove(workers, this);
});
worker.on("detach", function(){
array.remove(workers, this);
});
2014-12-15 18:43:47 +01:00
preferencesForInjected.forEach(function(name){
worker.port.emit("set", name, prefs[name]);
});
2014-12-04 23:34:41 +01:00
2014-08-20 10:21:38 +02:00
checkWorker(worker);
// display notifications
2015-04-15 12:02:21 +02:00
worker.port.on("accessed readAPI", function(status, callingStackMsg){
switch (status){
case "fake":
var contentURL = new URL(worker.contentURL);
if (prefs.showNotifications && !ignoreList.match(contentURL)){
2015-04-23 10:04:20 +02:00
var url = contentURL.href;
var domain = contentURL.hostname;
var message = _("fakedReadout").replace(/\{url\}/g, domain);
2015-04-23 10:04:20 +02:00
var tab = tabUtils.getTabForId(worker.tab.id);
var tabBrowser = tabUtils.getTabBrowserForTab(tab);
var browser = tabUtils.getBrowserForTab(tab);
var notifyBox = tabBrowser.getNotificationBox(browser);
var notification = notifyBox.getNotificationWithValue("fake-readout");
if (notification){
notification.label = message;
}
else {
var buttons = [
{
label: _("displayFullURL"),
accessKey: "",
callback: function(){
browser.contentWindow.alert(url);
// only way to prevent closing... see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Method/appendNotification#Notification_box_events
throw new Error("Do not close notification.");
}
},
2015-04-23 10:04:20 +02:00
{
label: _("displayCallingStack"),
accessKey: "",
callback: function(){
browser.contentWindow.alert(callingStackMsg);
2015-04-24 01:04:14 +02:00
// only way to prevent closing... see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Method/appendNotification#Notification_box_events
throw new Error("Do not close notification.");
2015-04-23 10:04:20 +02:00
}
},
{
label: _("ignorelistDomain"),
accessKey: "",
callback: function(){
prefs.ignoreList += (prefs.ignoreList? ",": "") + domain;
2015-04-23 10:04:20 +02:00
prefService.set("extensions.CanvasBlocker@kkapsner.de.ignoreList", prefs.ignoreList);
updateIgnoreList();
}
},
{
label: _("whitelistURL"),
accessKey: "",
callback: function(){
prefs.whiteList += (prefs.whiteList? ",": "") + "^" + url.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "$";
prefService.set("extensions.CanvasBlocker@kkapsner.de.whiteList", prefs.whiteList);
updateWhiteList();
workers.forEach(checkWorker);
}
},
2015-04-23 10:04:20 +02:00
{
label: _("whitelistDomain"),
accessKey: "",
callback: function(){
prefs.whiteList += (prefs.whiteList? ",": "") + domain;
2015-04-23 10:04:20 +02:00
prefService.set("extensions.CanvasBlocker@kkapsner.de.whiteList", prefs.whiteList);
updateWhiteList();
workers.forEach(checkWorker);
}
},
{
label: _("disableNotifications"),
accessKey: "",
callback: function(){
prefs.showNotifications = false;
prefService.set("extensions.CanvasBlocker@kkapsner.de.showNotifications", prefs.showNotifications);
}
}
2015-04-23 10:04:20 +02:00
];
2015-04-23 10:04:20 +02:00
var priority = notifyBox.PRIORITY_WARNING_MEDIUM;
2015-04-24 01:04:14 +02:00
notification = notifyBox.appendNotification(
2015-04-23 10:04:20 +02:00
message,
"fake-readout",
"chrome://browser/skin/Info.png",
priority,
buttons
);
2015-04-24 01:04:14 +02:00
}
}
break;
}
});
2014-08-20 10:21:38 +02:00
},
});
2014-07-31 03:05:51 +02:00
2014-08-20 10:21:38 +02:00
}());