1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-01-03 10:31:54 +01:00

Changed settings

This commit is contained in:
kkapsner 2014-08-20 10:21:38 +02:00
parent 430a9226dc
commit 40d059311a
5 changed files with 292 additions and 269 deletions

View File

@ -1,40 +1,82 @@
var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext; (function(){
var askFunctionName = Math.random().toString(16); "use strict";
var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext;
var askFunctionName = Math.random().toString(16);
function checkPDF(blocking){ function checkPDF(blocking){
if (unsafeWindow.document.contentType.match(/\/pdf$/i)){ if (document.contentType.match(/\/pdf$/i)){
self.port.emit("isPDF", blocking); self.port.emit("isPDF", blocking);
return true; return true;
}
return false;
} }
return false;
}
function block(force){ function block(force){
if (force || !checkPDF("block")){ if (force || !checkPDF("block")){
// consoe.log("block"); // consoe.log("block");
delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName]; delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName];
unsafeWindow.HTMLCanvasElement.prototype.getContext = null; unsafeWindow.HTMLCanvasElement.prototype.getContext = null;
}
} }
}
function ask(force){ function askVisible(force){
if (force || !checkPDF("ask")){ if (force || !checkPDF("askVisible")){
// console.log("ask");
Object.defineProperty(
Object.defineProperty( unsafeWindow.HTMLCanvasElement.prototype,
unsafeWindow.HTMLCanvasElement.prototype, askFunctionName,
askFunctionName, {
{ value: getContext,
value: getContext, enumerabe: false
enumerabe: false }
} );
); unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function(
unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function( function(){
function(){ if (this.parentNode){
if (this.parentNode){ var oldBorder = this.style.border;
this.style.border = "2px dashed red";
var confirmText = "askForPermission";
var allow = confirm(confirmText);
this.style.border = oldBorder;
if (allow){
this.getContext = this["askFunctionName"];
return this["askFunctionName"].apply(this, arguments);
}
else {
this.getContext = null;
return null;
}
}
else {
return null;
}
}.toString()
.replace(/^function\s*\(\)\s*\{|\}\s*$/g, "")
.replace(/askFunctionName/g, askFunctionName)
.replace(/askForPermission/g, _("askForPermission"))
);
}
}
function askInvisible(force){
if (force || !checkPDF("askInvisible")){
Object.defineProperty(
unsafeWindow.HTMLCanvasElement.prototype,
askFunctionName,
{
value: getContext,
enumerabe: false
}
);
unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function(
function(){
var oldBorder = this.style.border; var oldBorder = this.style.border;
this.style.border = "2px dashed red"; this.style.border = "2px dashed red";
var confirmText = "askForPermission"; var confirmText =
this.parentNode?
"askForPermission":
"askForInvisiblePermission";
var allow = confirm(confirmText); var allow = confirm(confirmText);
this.style.border = oldBorder; this.style.border = oldBorder;
if (allow){ if (allow){
@ -45,70 +87,30 @@ function ask(force){
this.getContext = null; this.getContext = null;
return null; return null;
} }
} }.toString()
else { .replace(/^function\s*\(\)\s*\{|\}\s*$/g, "")
return null; .replace(/askFunctionName/g, askFunctionName)
} .replace(/askForPermission/g, _("askForPermission"))
}.toString() .replace(/askForInvisiblePermission/g, _("askForInvisiblePermission"))
.replace(/^function\s*\(\)\s*\{|\}\s*$/g, "") );
.replace(/askFunctionName/g, askFunctionName) }
.replace(/askForPermission/g, _("askForPermission"))
);
} }
} function unblock(){
function askInvisible(force){ // console.log("unblock");
if (force || !checkPDF("askInvisible")){ unsafeWindow.HTMLCanvasElement.prototype.getContext = getContext;
// 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?
"askForPermission":
"askForInvisiblePermission";
var allow = confirm(confirmText);
this.style.border = oldBorder;
if (allow){
this.getContext = this["askFunctionName"];
return this["askFunctionName"].apply(this, arguments);
}
else {
this.getContext = null;
return null;
}
}.toString()
.replace(/^function\s*\(\)\s*\{|\}\s*$/g, "")
.replace(/askFunctionName/g, askFunctionName)
.replace(/askForPermission/g, _("askForPermission"))
.replace(/askForInvisiblePermission/g, _("askForInvisiblePermission"))
);
} }
}
function unblock(){
// console.log("unblock");
unsafeWindow.HTMLCanvasElement.prototype.getContext = getContext;
}
var _ = function(name){ var _ = function(name){
return _[name] || name; return _[name] || name;
} }
self.port.on("setTranslation", function(name, translation){ self.port.on("setTranslation", function(name, translation){
_[name] = translation; _[name] = translation;
}); });
block(); block();
self.port.on("block", block); self.port.on("block", block);
self.port.on("ask", ask); self.port.on("askVisible", askVisible);
self.port.on("askInvisible", askInvisible); self.port.on("askInvisible", askInvisible);
self.port.on("unblock", unblock); self.port.on("unblock", unblock);
self.port.on("detach", unblock); self.port.on("detach", unblock);
}());

View File

@ -1,157 +1,162 @@
function getDomainRegExpList(domainList){ (function(){
return domainList "use strict";
.split(",")
.map(function(entry){ function getDomainRegExpList(domainList){
return entry.replace(/^\s+|\s+$/g, ""); var list = domainList
}) .split(",")
.filter(function(entry){ .map(function(entry){
return !!entry.length; return entry.replace(/^\s+|\s+$/g, "");
}) })
.map(function(entry){ .filter(function(entry){
var regExp; return !!entry.length;
var domain = !!entry.match(/^[\w.]+$/); })
if (domain){ .map(function(entry){
regExp = new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i") var regExp;
} var domain = !!entry.match(/^[\w.]+$/);
else { if (domain){
regExp = new RegExp(entry, "i"); regExp = new RegExp("(?:^|\\.)" + entry.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "\\.?$", "i")
}
return {
match: function(url){
if (domain){
return url.hostname.match(regExp);
}
else {
return url.href.match(regExp);
}
} }
}; else {
}); regExp = new RegExp(entry, "i");
} }
return {
var self = require("sdk/self"); match: function(url){
var pageMod = require("sdk/page-mod"); if (domain){
var preferences = require("sdk/simple-prefs"); return url.hostname.match(regExp);
var prefs = preferences.prefs; }
var {URL} = require("sdk/url"); else {
var _ = require("sdk/l10n").get; return url.href.match(regExp);
}
// preferences }
var whiteList; };
function updateWhiteList(){
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(){
updateBlackList();
workers.forEach(checkWorker);
});
var workers = [];
function detachWorker(worker, workerArray) {
var index = workerArray.indexOf(worker);
if (index != -1){
workerArray.splice(index, 1);
}
}
function checkWorker(worker){
if (prefs.blockAll){
worker.port.emit("block");
}
else {
var url = new URL(worker.url);
var inBlackList = blackList.some(function(entry){
return entry.match(url);
});
if (inBlackList){
worker.port.emit("block");
}
else {
var inWhiteList = whiteList.some(function(entry){
return entry.match(url);
}); });
if (inWhiteList){
worker.port.emit("unblock"); list.match = function(url){
return this.some(function(entry){
return entry.match(url);
})
} }
else {
if (prefs.askPermission){ return list;
if (prefs.askInvisiblePermission){ }
worker.port.emit("askInvisible");
} var self = require("sdk/self");
else { var pageMod = require("sdk/page-mod");
worker.port.emit("ask"); var preferences = require("sdk/simple-prefs");
} var prefs = preferences.prefs;
var {URL} = require("sdk/url");
var _ = require("sdk/l10n").get;
// preferences
var whiteList;
function updateWhiteList(){
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(){
updateBlackList();
workers.forEach(checkWorker);
});
preferences.on("blockMode", function(){
workers.forEach(checkWorker);
});preferences.on("allowPDFCanvas", function(){
workers.forEach(checkWorker);
});
var workers = [];
function detachWorker(worker, workerArray) {
var index = workerArray.indexOf(worker);
if (index != -1){
workerArray.splice(index, 1);
}
}
function checkWorker(worker){
var url = new URL(worker.url);
switch (prefs.blockMode){
case "blockEverything":
worker.port.emit("block");
break;
case "allowOnlyWhiteList":
if (whiteList.match(url)){
worker.port.emit("unblock");
} }
else { else {
worker.port.emit("block"); worker.port.emit("block");
} }
} break;
case "askVisible":
if (whiteList.match(url)){
worker.port.emit("unblock");
}
else if (blackList.match(url)){
worker.port.emit("block");
}
else {
worker.port.emit("askVisible");
}
break;
case "askInvisible":
if (whiteList.match(url)){
worker.port.emit("unblock");
}
else if (blackList.match(url)){
worker.port.emit("block");
}
else {
worker.port.emit("askInvisible");
}
break;
case "blockOnlyBlackList":
if (blackList.match(url)){
worker.port.emit("block");
}
else {
worker.port.emit("unblock");
}
break;
case "allowEverything":
worker.port.emit("unblock");
break;
default:
console.log("Unknown blocking mode.");
} }
} }
}
preferences.on("blockAll", function(){
if (prefs.blockAll){
prefs.askPermission = false;
prefs.askInsiviblePermission = false;
}
workers.forEach(checkWorker);
});
preferences.on("askPermission", function(){
if (prefs.askPermission){
prefs.blockAll = false;
}
else {
prefs.askInvisiblePermission = false;
}
workers.forEach(checkWorker);
});
preferences.on("askInvisiblePermission", function(){
if (prefs.askInvisiblePermission){
prefs.askPermission = true;
prefs.blockAll = false;
}
workers.forEach(checkWorker);
});
preferences.on("allowPDFCanvas", function(){
workers.forEach(checkWorker);
});
pageMod.PageMod({ pageMod.PageMod({
include: "*", include: "*",
contentScriptWhen: "start", contentScriptWhen: "start",
contentScriptFile: self.data.url("inject.js"), contentScriptFile: self.data.url("inject.js"),
onAttach: function(worker){ onAttach: function(worker){
workers.push(worker); workers.push(worker);
worker.on("detach", function(){ worker.on("detach", function(){
detachWorker(this, workers); detachWorker(this, workers);
}); });
worker.port.on("getTranslation", function(name){ worker.port.on("isPDF", function(blocking){
worker.port.emit("setTranslation", name, _.apply(null, arguments)); if (prefs.allowPDFCanvas){
}); worker.port.emit("unblock");
worker.port.on("isPDF", function(blocking){ }
if (prefs.allowPDFCanvas){ else {
worker.port.emit("unblock"); worker.port.emit(blocking, true);
} }
else { });
worker.port.emit(blocking, true); worker.port.emit("setTranslation", "askForPermission", _("askForPermission"));
} worker.port.emit("setTranslation", "askForInvisiblePermission", _("askForInvisiblePermission"));
}); checkWorker(worker);
worker.port.emit("setTranslation", "askForPermission", _("askForPermission")); },
worker.port.emit("setTranslation", "askForInvisiblePermission", _("askForInvisiblePermission")); });
checkWorker(worker);
}, }());
});

View File

@ -1,17 +1,18 @@
whilteList_title= Whitelist whilteList_title= Whitelist
whiteList_description= Domänen oder URLs, die die <canvas>-API verwenden dürfen. Mehrere Einträge müssen durch ein Komma getrennt weren. whiteList_description= Domänen oder URLs, die die <canvas>-API verwenden dürfen. Mehrere Einträge müssen durch ein Komma getrennt weren.
blockAll_title= Alles blockieren
blockAll_description= Ob immer alles blockiert werden soll (ignoriert die Whitelist).
blackList_title= Blacklist blackList_title= Blacklist
blackList_description= Domänen oder URLs, die die <canvas>-API niemals verwenden dürfen. Mehrere Einträge müssen durch ein Komma getrennt weren. blackList_description= Domänen oder URLs, die die <canvas>-API niemals verwenden dürfen. Mehrere Einträge müssen durch ein Komma getrennt weren.
askPermission_title= Um Erlaubnis fragen blockMode_title= Blockiermodus
askPermission_description= Ob man gefragt werden soll, ob ein sichtbares <canvas>-Element blockiert werden soll oder nicht, wenn die Domäne oder URL weder auf der Whitelist noch auf der Blacklist steht. blockMode_description=
askInvisiblePermission_title= Bei verstecken Elementen um Erlaubnis fragen blockMode_options.block everything= alles blockieren
askInvisiblePermission_description= Ob man gefragt werden soll, ob ein verstecktes <canvas>-Element blockiert werden soll oder nicht, wenn die Domäne oder URL weder auf der Whitelist noch auf der Blacklist steht. blockMode_options.allow only white list= nur Einträge der Whitelist erlauben
blockMode_options.ask for permission for visible <canvas>= bei sichtbaren <canvas> um Erlaubnis fragen
blockMode_options.ask for permision for invisible <canvas>= bei unsichtbaren <canvas> um Erlaubnis fragen
blockMode_options.block only black list= nur Einträge der Blacklist blockieren
blockMode_options.allow everything= alles erlauben
allowPDFCanvas_title= <canvas> in PDFs erlauben allowPDFCanvas_title= <canvas> in PDFs erlauben
allowPDFCanvas_description= Die native pdf.js verwendet <canvas> um den Inhalt von PDFs anzuzeigen. Wenn dies nicht markiert ist, werden viele Nachfragedialoge erscheinen oder die PDF Ansicht nicht funktionieren. allowPDFCanvas_description= Die native pdf.js verwendet <canvas> um den Inhalt von PDFs anzuzeigen. Wenn dies nicht markiert ist, werden viele Nachfragedialoge erscheinen oder die PDF Ansicht nicht funktionieren.

View File

@ -1,17 +1,18 @@
whilteList_title= White list whilteList_title= White list
whiteList_description= Domains or URLs where the <canvas>-API should not be blocked. To add multiple entries seperate them by comma. whiteList_description= Domains or URLs where the <canvas>-API should not be blocked. To add multiple entries seperate them by comma.
blockAll_title= Block everything
blockAll_description= If you want to block everything (ignore the white list).
blackList_title= Black list blackList_title= Black list
blackList_description= Domains or URLs where the <canvas>-API should always be blocked. To add multiple entries seperate them by comma. blackList_description= Domains or URLs where the <canvas>-API should always be blocked. To add multiple entries seperate them by comma.
askPermission_title= Ask for permission blockMode_title= Block mode
askPermission_description= If you want to be asked if you want to block a visible canvas element if the domain or URL is neither in the white or black list. blockMode_description=
askInvisiblePermission_title= Ask for permission for invisible elements blockMode_options.block everything= block everything
askInvisiblePermission_description= If you want to be asked if you want to block an invisible canvas element if the domain or URL is neither in the white or black list. blockMode_options.allow only white list= allow only white list
blockMode_options.ask for permission for visible <canvas>= ask for permission for visible <canvas>
blockMode_options.ask for permision for invisible <canvas>= ask for permision for invisible <canvas>
blockMode_options.block only black list= block only black list
blockMode_options.allow everything= allow everything
allowPDFCanvas_title= Allow canvas in PDFs allowPDFCanvas_title= Allow canvas in PDFs
allowPDFCanvas_description= The native pdf.js uses <canvas> to display the PDF content. If this is unchecked there will lots of annoying ask dialogs or the PDF display will not work. allowPDFCanvas_description= The native pdf.js uses <canvas> to display the PDF content. If this is unchecked there will lots of annoying ask dialogs or the PDF display will not work.

View File

@ -10,12 +10,6 @@
"type": "string", "type": "string",
"value": "kkapsner.de,^https?://(\\\\w+\\\\.)*google.[a-z]+/maps" "value": "kkapsner.de,^https?://(\\\\w+\\\\.)*google.[a-z]+/maps"
}, },
{
"name": "blockAll",
"title": "Block everything",
"type": "bool",
"value": false
},
{ {
"name": "blackList", "name": "blackList",
"title": "Black list", "title": "Black list",
@ -23,16 +17,36 @@
"value": "" "value": ""
}, },
{ {
"name": "askPermission", "name": "blockMode",
"title": "Ask for permission", "title": "block mode",
"type": "bool", "type": "menulist",
"value": true "value": "askVisible",
}, "options": [
{ {
"name": "askInvisiblePermission", "value": "blockEverything",
"title": "Ask for permission for invisible elements", "label": "block everything"
"type": "bool", },
"value": false {
"value": "allowOnlyWhiteList",
"label": "allow only white list"
},
{
"value": "askVisible",
"label": "ask for permission for visible <canvas>"
},
{
"value": "askInvisible",
"label": "ask for permision for invisible <canvas>"
},
{
"value": "blockOnlyBlackList",
"label": "block only black list"
},
{
"value": "allowEverything",
"label": "allow everything"
}
]
}, },
{ {
"name": "allowPDFCanvas", "name": "allowPDFCanvas",