2014-08-01 12:03:23 +02:00
|
|
|
function getDomainRegExpList(domainList){
|
|
|
|
return domainList
|
|
|
|
.split(",")
|
|
|
|
.map(function(entry){
|
|
|
|
return entry.replace(/^\s+|\s+$/g, "");
|
|
|
|
})
|
|
|
|
.filter(function(entry){
|
|
|
|
return !!entry.length;
|
|
|
|
})
|
|
|
|
.map(function(entry){
|
|
|
|
return new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-31 03:05:51 +02:00
|
|
|
var self = require("sdk/self");
|
|
|
|
var pageMod = require("sdk/page-mod");
|
|
|
|
var preferences = require("sdk/simple-prefs");
|
|
|
|
var prefs = preferences.prefs;
|
|
|
|
var {URL} = require("sdk/url");
|
2014-08-01 12:03:23 +02:00
|
|
|
|
2014-07-31 03:05:51 +02:00
|
|
|
var whiteList;
|
2014-08-01 12:03:23 +02:00
|
|
|
function updateWhiteList(){
|
|
|
|
whiteList = getDomainRegExpList(prefs.whiteList);
|
|
|
|
}
|
2014-07-31 03:05:51 +02:00
|
|
|
updateWhiteList();
|
2014-08-01 12:03:23 +02:00
|
|
|
preferences.on("whiteList", function(){
|
|
|
|
updateWhiteList();
|
|
|
|
workers.forEach(checkWorker);
|
|
|
|
});
|
2014-07-31 03:05:51 +02:00
|
|
|
|
2014-08-01 12:03:23 +02:00
|
|
|
var blackList;
|
|
|
|
function updateBlackList(){
|
|
|
|
blackList = getDomainRegExpList(prefs.blackList);
|
2014-07-31 03:05:51 +02:00
|
|
|
}
|
2014-08-01 12:03:23 +02:00
|
|
|
updateBlackList();
|
|
|
|
preferences.on("blackList", function(){
|
|
|
|
updateBackList();
|
|
|
|
workers.forEach(checkWorker);
|
|
|
|
});
|
2014-07-31 03:05:51 +02:00
|
|
|
|
2014-08-01 12:03:23 +02:00
|
|
|
var workers = [];
|
2014-07-31 03:05:51 +02:00
|
|
|
function detachWorker(worker, workerArray) {
|
|
|
|
var index = workerArray.indexOf(worker);
|
|
|
|
if (index != -1){
|
|
|
|
workerArray.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function checkWorker(worker){
|
2014-08-01 12:03:23 +02:00
|
|
|
if (prefs.blockAll){
|
2014-07-31 03:05:51 +02:00
|
|
|
worker.port.emit("block");
|
|
|
|
}
|
|
|
|
else {
|
2014-08-01 12:03:23 +02:00
|
|
|
var url = new URL(worker.url);
|
|
|
|
var inBlackList = blackList.some(function(entry){
|
|
|
|
return url.hostname.match(entry);
|
|
|
|
});
|
|
|
|
if (inBlackList){
|
|
|
|
worker.port.emit("block");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var inWhiteList = whiteList.some(function(entry){
|
|
|
|
return url.hostname.match(entry);
|
|
|
|
});
|
|
|
|
if (inWhiteList){
|
|
|
|
worker.port.emit("unblock");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (prefs.askPermission){
|
|
|
|
worker.port.emit("ask");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
worker.port.emit("block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-31 03:05:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-01 12:03:23 +02:00
|
|
|
preferences.on("blockAll", function(){
|
|
|
|
if (prefs.blockAll){
|
|
|
|
prefs.askPermission = false;
|
|
|
|
}
|
2014-07-31 03:05:51 +02:00
|
|
|
workers.forEach(checkWorker);
|
|
|
|
});
|
2014-08-01 12:03:23 +02:00
|
|
|
preferences.on("askPermission", function(){
|
|
|
|
if (prefs.askPermission){
|
|
|
|
prefs.blockAll = false;
|
|
|
|
}
|
2014-07-31 03:05:51 +02:00
|
|
|
workers.forEach(checkWorker);
|
|
|
|
});
|
2014-08-04 17:02:42 +02:00
|
|
|
preferences.on("allowPDFCanvas", function(){
|
|
|
|
workers.forEach(checkWorker);
|
|
|
|
});
|
|
|
|
|
2014-07-31 03:05:51 +02:00
|
|
|
|
|
|
|
pageMod.PageMod({
|
|
|
|
include: "*",
|
|
|
|
contentScriptWhen: "start",
|
|
|
|
contentScriptFile: self.data.url("inject.js"),
|
|
|
|
onAttach: function(worker){
|
|
|
|
checkWorker(worker);
|
|
|
|
|
|
|
|
workers.push(worker);
|
|
|
|
worker.on("detach", function(){
|
|
|
|
detachWorker(this, workers);
|
|
|
|
});
|
2014-08-04 17:02:42 +02:00
|
|
|
worker.port.on("isPDF", function(blocking){
|
|
|
|
if (prefs.allowPDFCanvas){
|
|
|
|
worker.port.emit("unblock");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
worker.port.emit(blocking, true);
|
|
|
|
}
|
|
|
|
});
|
2014-07-31 03:05:51 +02:00
|
|
|
},
|
|
|
|
});
|