1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-06-01 02:48:06 +02:00
CanvasBlocker/lib/main.js

183 lines
4.1 KiB
JavaScript
Raw Normal View History

2014-10-14 01:06:11 +02:00
/* global console */
2014-08-20 10:21:38 +02:00
(function(){
2014-10-14 01:06:11 +02:00
"use strict";
2014-08-20 10:21:38 +02:00
function getDomainRegExpList(domainList){
var list = domainList
.split(",")
.map(function(entry){
return entry.replace(/^\s+|\s+$/g, "");
})
.filter(function(entry){
return !!entry.length;
})
.map(function(entry){
var regExp;
var domain = !!entry.match(/^[\w.]+$/);
if (domain){
2014-10-14 01:06:11 +02:00
regExp = new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i");
2014-08-20 10:21:38 +02:00
}
else {
regExp = new RegExp(entry, "i");
}
2014-08-20 10:21:38 +02:00
return {
match: function(url){
if (domain){
return url.hostname.match(regExp);
}
else {
return url.href.match(regExp);
}
}
};
});
list.match = function(url){
return this.some(function(entry){
return entry.match(url);
2014-10-14 01:06:11 +02:00
});
};
2014-08-20 10:21:38 +02:00
return list;
}
2014-08-01 12:03:23 +02:00
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 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;
2014-08-01 12:03:23 +02:00
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();
});
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){
var url = new URL(url);
var mode = "block";
switch (prefs.blockMode){
case "blockEverything":
mode = "block";
break;
case "allowOnlyWhiteList":
if (whiteList.match(url)){
mode = "unblock";
}
else {
mode = "block";
2014-10-11 00:07:51 +02:00
}
break;
case "ask":
case "blockReadout":
2014-10-13 01:37:13 +02:00
case "fakeReadout":
2014-10-11 01:46:47 +02:00
case "askReadout":
if (whiteList.match(url)){
mode = "unblock";
}
else if (blackList.match(url)){
mode = "block";
}
else {
2014-10-14 01:06:11 +02:00
mode = prefs.blockMode;
2014-10-11 01:46:47 +02:00
}
break;
2014-10-11 00:07:51 +02:00
case "blockOnlyBlackList":
if (blackList.match(url)){
mode = "block";
}
else {
mode = "unblock";
}
break;
case "allowEverything":
mode = "unblock";
break;
default:
console.log("Unknown blocking mode. Default to block everything.");
}
return mode;
}
function checkWorker(worker){
try {
var mode = checkURL(worker.url);
worker.port.emit(mode, false, prefs.askOnlyOnce);
}
catch (e){
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 = [];
2014-08-20 10:21:38 +02:00
pageMod.PageMod({
include: "*",
contentScriptWhen: "start",
contentScriptFile: self.data.url("inject.js"),
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-08-20 10:21:38 +02:00
worker.port.on("isPDF", function(blocking){
if (prefs.allowPDFCanvas){
2014-10-05 15:08:01 +02:00
this.emit("unblock");
2014-08-20 10:21:38 +02:00
}
else {
this.emit(blocking, true, prefs.askOnlyOnce);
2014-08-20 10:21:38 +02:00
}
});
2014-12-04 23:34:41 +01:00
["", "Readout"].forEach(function(type){
["", "Visible", "Invisible"].forEach(function(visibility){
var text = "askFor" + visibility + type + "Permission";
worker.port.emit("setTranslation", text, _(text));
});
});
2014-12-15 18:43:47 +01:00
worker.port.emit("setTranslation", "sourceOutput", _("sourceOutput"));
worker.port.emit("setTranslation", "stackEntryOutput", _("stackEntryOutput"));
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);
},
});
2014-07-31 03:05:51 +02:00
2014-08-20 10:21:38 +02:00
}());