1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-06-18 09:49:35 +02:00

Added black list and ask.

This commit is contained in:
kkapsner 2014-08-01 12:03:23 +02:00
parent 0ab4f1b7d3
commit ebe1d66ad5
4 changed files with 113 additions and 15 deletions

Binary file not shown.

View File

@ -1,12 +1,46 @@
var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext
var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext;
var askFunctionName = Math.random().toString(16);
function block(){
// consoe.log("block");
delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName];
unsafeWindow.HTMLCanvasElement.prototype.getContext = null;
}
function ask(){
// console.log("ask");
Object.defineProperty(
unsafeWindow.HTMLCanvasElement.prototype,
askFunctionName,
{
value: getContext,
enumerabe: false
}
);
unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function(function(){
var oldBorder = this.style.border;
this.style.border = "2px dashed red";
var confirmText =
this.parentNode?
"Do you want to allow the red bordered <canvas>?":
"Do you want to allow an invisibe <canvas>?";
var allow = confirm(confirmText);
this.style.border = oldBorder;
if (allow){
return this["askFunctionName"].apply(this, arguments);
}
else {
return null;
}
}.toString().replace(/^function\s*\(\)\s*\{|\}\s*$/g, "").replace("askFunctionName", askFunctionName));
}
function unblock(){
// console.log("unblock");
unsafeWindow.HTMLCanvasElement.prototype.getContext = getContext;
}
ask();
self.port.on("block", block);
self.port.on("ask", ask);
self.port.on("unblock", unblock);
self.port.on("detach", unblock);

View File

@ -1,18 +1,44 @@
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");
});
}
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");
var workers = [];
var whiteList;
updateWhiteList();
function updateWhiteList(){
whiteList = prefs.whiteList.split(",").map(function(entry){
return new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i");
});
whiteList = getDomainRegExpList(prefs.whiteList);
}
updateWhiteList();
preferences.on("whiteList", function(){
updateWhiteList();
workers.forEach(checkWorker);
});
var blackList;
function updateBlackList(){
blackList = getDomainRegExpList(prefs.blackList);
}
updateBlackList();
preferences.on("blackList", function(){
updateBackList();
workers.forEach(checkWorker);
});
var workers = [];
function detachWorker(worker, workerArray) {
var index = workerArray.indexOf(worker);
if (index != -1){
@ -20,22 +46,46 @@ function detachWorker(worker, workerArray) {
}
}
function checkWorker(worker){
var url = new URL(worker.url);
if (prefs.blockAll || !whiteList.some(function(entry){
return url.hostname.match(entry);
})){
if (prefs.blockAll){
worker.port.emit("block");
}
else {
worker.port.emit("unblock");
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");
}
}
}
}
}
preferences.on("whiteList", function(){
updateWhiteList();
preferences.on("blockAll", function(){
if (prefs.blockAll){
prefs.askPermission = false;
}
workers.forEach(checkWorker);
});
preferences.on("blockAll", function(){
preferences.on("askPermission", function(){
if (prefs.askPermission){
prefs.blockAll = false;
}
workers.forEach(checkWorker);
});

View File

@ -17,6 +17,20 @@
"description": "If you want to block everything (ignore the white list).",
"type": "bool",
"value": false
},
{
"name": "blackList",
"title": "Black list",
"description": "Domains where the <canvas>-API should always be blocked. To add multiple domains seperate them by comma.",
"type": "string",
"value": ""
},
{
"name": "askPermission",
"title": "Ask for permission",
"description": "If you want to be asked if you want to block a canvas element if the domain is neither in the white or black list.",
"type": "bool",
"value": true
}
],
"author": "Korbinian Kapsner",