diff --git a/canvasblocker.xpi b/canvasblocker.xpi index 37d4286..c0a4034 100644 Binary files a/canvasblocker.xpi and b/canvasblocker.xpi differ diff --git a/data/inject.js b/data/inject.js index d934baf..02ba040 100644 --- a/data/inject.js +++ b/data/inject.js @@ -1,38 +1,51 @@ 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 checkPDF(blocking){ + if (unsafeWindow.document.contentType.match(/\/pdf$/i)){ + self.port.emit("isPDF", blocking); + return false; + } + return true; } -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 ?": - "Do you want to allow an invisibe ?"; - 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 block(force){ + if (force || !checkPDF("block")){ + // consoe.log("block"); + delete unsafeWindow.HTMLCanvasElement.prototype[askFunctionName]; + unsafeWindow.HTMLCanvasElement.prototype.getContext = null; + } +} +function ask(force){ + if (force || !checkPDF("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 ?": + "Do you want to allow an invisibe ?"; + 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(){ // console.log("unblock"); diff --git a/lib/main.js b/lib/main.js index 8dea053..11ead3e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -88,6 +88,10 @@ preferences.on("askPermission", function(){ } workers.forEach(checkWorker); }); +preferences.on("allowPDFCanvas", function(){ + workers.forEach(checkWorker); +}); + pageMod.PageMod({ include: "*", @@ -100,5 +104,13 @@ pageMod.PageMod({ worker.on("detach", function(){ detachWorker(this, workers); }); + worker.port.on("isPDF", function(blocking){ + if (prefs.allowPDFCanvas){ + worker.port.emit("unblock"); + } + else { + worker.port.emit(blocking, true); + } + }); }, }); \ No newline at end of file diff --git a/package.json b/package.json index 59ebd2b..a01241f 100644 --- a/package.json +++ b/package.json @@ -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.", "type": "bool", "value": true + }, + { + "name": "allowPDFCanvas", + "title": "Allow canvas in PDFs.", + "description": "The native pdf.js uses to display the PDF content. If this is unchecked there will lots of annoying ask dialogs.", + "type": "bool", + "value": true } ], "author": "Korbinian Kapsner",