1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01: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(){ function block(){
// consoe.log("block");
delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName];
unsafeWindow.HTMLCanvasElement.prototype.getContext = null; 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(){ function unblock(){
// console.log("unblock");
unsafeWindow.HTMLCanvasElement.prototype.getContext = getContext; unsafeWindow.HTMLCanvasElement.prototype.getContext = getContext;
} }
ask();
self.port.on("block", block); self.port.on("block", block);
self.port.on("ask", ask);
self.port.on("unblock", unblock); self.port.on("unblock", unblock);
self.port.on("detach", 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 self = require("sdk/self");
var pageMod = require("sdk/page-mod"); var pageMod = require("sdk/page-mod");
var preferences = require("sdk/simple-prefs"); var preferences = require("sdk/simple-prefs");
var prefs = preferences.prefs; var prefs = preferences.prefs;
var {URL} = require("sdk/url"); var {URL} = require("sdk/url");
var workers = [];
var whiteList; var whiteList;
updateWhiteList();
function updateWhiteList(){ function updateWhiteList(){
whiteList = prefs.whiteList.split(",").map(function(entry){ whiteList = getDomainRegExpList(prefs.whiteList);
return new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i");
});
} }
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) { function detachWorker(worker, workerArray) {
var index = workerArray.indexOf(worker); var index = workerArray.indexOf(worker);
if (index != -1){ if (index != -1){
@ -20,22 +46,46 @@ function detachWorker(worker, workerArray) {
} }
} }
function checkWorker(worker){ function checkWorker(worker){
var url = new URL(worker.url); if (prefs.blockAll){
if (prefs.blockAll || !whiteList.some(function(entry){
return url.hostname.match(entry);
})){
worker.port.emit("block"); worker.port.emit("block");
} }
else { 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(){ preferences.on("blockAll", function(){
updateWhiteList(); if (prefs.blockAll){
prefs.askPermission = false;
}
workers.forEach(checkWorker); workers.forEach(checkWorker);
}); });
preferences.on("blockAll", function(){ preferences.on("askPermission", function(){
if (prefs.askPermission){
prefs.blockAll = false;
}
workers.forEach(checkWorker); workers.forEach(checkWorker);
}); });

View File

@ -17,6 +17,20 @@
"description": "If you want to block everything (ignore the white list).", "description": "If you want to block everything (ignore the white list).",
"type": "bool", "type": "bool",
"value": false "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", "author": "Korbinian Kapsner",