1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-01-09 05:04:46 +01:00

PDF canvas support

This commit is contained in:
kkapsner 2014-08-04 17:02:42 +02:00
parent 3eed0d67d3
commit f185ad443b
4 changed files with 63 additions and 31 deletions

Binary file not shown.

View File

@ -1,38 +1,51 @@
var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext; var getContext = unsafeWindow.HTMLCanvasElement.prototype.getContext;
var askFunctionName = Math.random().toString(16); var askFunctionName = Math.random().toString(16);
function block(){ function checkPDF(blocking){
// consoe.log("block"); if (unsafeWindow.document.contentType.match(/\/pdf$/i)){
delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName]; self.port.emit("isPDF", blocking);
unsafeWindow.HTMLCanvasElement.prototype.getContext = null; return false;
}
return true;
} }
function ask(){
// console.log("ask");
Object.defineProperty( function block(force){
unsafeWindow.HTMLCanvasElement.prototype, if (force || !checkPDF("block")){
askFunctionName, // consoe.log("block");
{ delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName];
value: getContext, unsafeWindow.HTMLCanvasElement.prototype.getContext = null;
enumerabe: false }
} }
); function ask(force){
unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function(function(){ if (force || !checkPDF("ask")){
var oldBorder = this.style.border; // console.log("ask");
this.style.border = "2px dashed red";
var confirmText = Object.defineProperty(
this.parentNode? unsafeWindow.HTMLCanvasElement.prototype,
"Do you want to allow the red bordered <canvas>?": askFunctionName,
"Do you want to allow an invisibe <canvas>?"; {
var allow = confirm(confirmText); value: getContext,
this.style.border = oldBorder; enumerabe: false
if (allow){ }
return this["askFunctionName"].apply(this, arguments); );
} unsafeWindow.HTMLCanvasElement.prototype.getContext = new unsafeWindow.Function(function(){
else { var oldBorder = this.style.border;
return null; this.style.border = "2px dashed red";
} var confirmText =
}.toString().replace(/^function\s*\(\)\s*\{|\}\s*$/g, "").replace("askFunctionName", askFunctionName)); 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){
this.getContext = this["askFunctionName"];
return this["askFunctionName"].apply(this, arguments);
}
else {
return null;
}
}.toString().replace(/^function\s*\(\)\s*\{|\}\s*$/g, "").replace(/askFunctionName/g, askFunctionName));
}
} }
function unblock(){ function unblock(){
// console.log("unblock"); // console.log("unblock");

View File

@ -88,6 +88,10 @@ preferences.on("askPermission", function(){
} }
workers.forEach(checkWorker); workers.forEach(checkWorker);
}); });
preferences.on("allowPDFCanvas", function(){
workers.forEach(checkWorker);
});
pageMod.PageMod({ pageMod.PageMod({
include: "*", include: "*",
@ -100,5 +104,13 @@ pageMod.PageMod({
worker.on("detach", function(){ worker.on("detach", function(){
detachWorker(this, workers); detachWorker(this, workers);
}); });
worker.port.on("isPDF", function(blocking){
if (prefs.allowPDFCanvas){
worker.port.emit("unblock");
}
else {
worker.port.emit(blocking, true);
}
});
}, },
}); });

View File

@ -31,6 +31,13 @@
"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.", "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", "type": "bool",
"value": true "value": true
},
{
"name": "allowPDFCanvas",
"title": "Allow canvas in PDFs.",
"description": "The native pdf.js uses <canvas> to display the PDF content. If this is unchecked there will lots of annoying ask dialogs.",
"type": "bool",
"value": true
} }
], ],
"author": "Korbinian Kapsner", "author": "Korbinian Kapsner",