diff --git a/canvasblocker.xpi b/canvasblocker.xpi index 4f41e83..c9c4db2 100644 Binary files a/canvasblocker.xpi and b/canvasblocker.xpi differ diff --git a/data/inject.js b/data/inject.js index 0d4eeda..5601b26 100644 --- a/data/inject.js +++ b/data/inject.js @@ -147,7 +147,7 @@ toDataURL: { mode: blockMode.readAPI, object: unsafeWindow.HTMLCanvasElement, - fake: function(){ + fake: function toDataURL(){ var type = arguments[0] || "image/png"; return "data:" + type + ";base64," + btoa(randomImage); } @@ -155,7 +155,7 @@ toBlob: { mode: blockMode.readAPI, object: unsafeWindow.HTMLCanvasElement, - fake: function(callback){ + fake: function toBlob(callback){ var type = arguments[0] || "image/png"; var blob = new window.Blob(randomImage, {type: type}); callback(blob); @@ -169,7 +169,7 @@ getImageData: { mode: blockMode.readAPI, object: unsafeWindow.CanvasRenderingContext2D, - fake: function(sx, sy, sw, sh){ + fake: function getImageData(sx, sy, sw, sh){ var l = sw * sh * 4; var data = new Uint8ClampedArray(l); for (var i = 0; i < l; i += 1){ @@ -185,7 +185,7 @@ readPixels: { mode: blockMode.readAPI, object: unsafeWindow.WebGLRenderingContext, - fake: function(x, y, width, height, format, type, pixels){ + fake: function readPixels(x, y, width, height, format, type, pixels){ // fake not working due to XRay copy restrictions... // for (var i = 0; i < pixels.length; i += 1){ // pixels[i] = Math.floor( diff --git a/lib/disableWithoutDocumentElement.js b/lib/disableWithoutDocumentElement.js new file mode 100644 index 0000000..9fdedfb --- /dev/null +++ b/lib/disableWithoutDocumentElement.js @@ -0,0 +1,49 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +(function(){ + "use strict"; + + const observers = require("sdk/system/events"); + const { when: unload } = require("sdk/system/unload"); + + var classes = { + HTMLCanvasElement: ["getContext", "toDataURL", "toBlob", "mozGetAsFile"], + CanvasRenderingContext2D: ["getImageData"], + WebGLRenderingContext: ["readPixels"] + }; + + var classNames = Object.keys(classes); + + var originalProperties = new WeakMap(); + + function disable({subject: window}){ + var oldProperties = {}; + classNames.forEach(function(className){ + oldProperties[className] = {}; + classes[className].forEach(function(funcName){ + oldProperties[className][funcName] = window.wrappedJSObject[className].prototype[funcName]; + window.wrappedJSObject[className].prototype[funcName] = function(){}; + }); + }); + originalProperties.set(window, oldProperties); + } + + function reset({subject: document}){ + var window = document.defaultView; + var oldProperties = originalProperties.get(window); + if (oldProperties){ + originalProperties.delete(window); + classNames.forEach(function(className){ + classes[className].forEach(function(funcName){ + window.wrappedJSObject[className].prototype[funcName] = oldProperties[className][funcName]; + }); + }); + } + } + + observers.on("content-document-global-created", disable); + unload(() => observers.off("content-document-global-created", disable)); + observers.on("document-element-inserted", reset); + unload(() => observers.off("document-element-inserted", reset)); +}()); \ No newline at end of file diff --git a/lib/main.js b/lib/main.js index 4102195..47d8931 100644 --- a/lib/main.js +++ b/lib/main.js @@ -5,6 +5,7 @@ (function(){ "use strict"; require("./stylePreferencePane"); + require("./disableWithoutDocumentElement"); var self = require("sdk/self"); var pageMod = require("sdk/page-mod"); @@ -70,7 +71,7 @@ var url = new URL(worker.url); if ( (url.protocol === "about:") || - (prefs.allowPDFCanvas && worker.tab.contentType.match(/\/pdf$/i)) + (prefs.allowPDFCanvas && worker.tab && worker.tab.contentType.match(/\/pdf$/i)) ){ mode = "unblock"; }