From 6d7a9fc22a55d1cebe71fdd6475ba3af2e9a1a5a Mon Sep 17 00:00:00 2001 From: kkapsner Date: Mon, 26 Dec 2016 14:36:01 +0100 Subject: [PATCH] Removed bug with faking webgl and added readPixels for webgl2. Fixes #96 and #97. --- lib/intercept.js | 92 +++++++++++++++++++++++----------------------- lib/modifiedAPI.js | 25 ++++++++----- 2 files changed, 63 insertions(+), 54 deletions(-) diff --git a/lib/intercept.js b/lib/intercept.js index b78bb08..26268d9 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -26,56 +26,58 @@ if (siteStatus.mode !== "allow"){ apiNames.forEach(function(name){ var changedFunction = changedFunctions[name]; - var original = window.wrappedJSObject[changedFunction.object].prototype[name]; - if (changedFunction.getStatus(undefined, siteStatus).active){ - Object.defineProperty( - window.wrappedJSObject[changedFunction.object].prototype, - name, - { - enumerable: true, - configureable: false, - get: function(){ - if (!window.location.href){ - return undef; - } - var error = new Error(); - if (checkStack(error.stack)){ - return original; - } - var funcStatus = changedFunction.getStatus(this, siteStatus); - - if (funcStatus.active){ - if (funcStatus.mode === "ask"){ - funcStatus.mode = ask({window: window, type: changedFunction.type, canvas: this, errorStack: error.stack}); + (Array.isArray(changedFunction.object)? changedFunction.object: [changedFunction.object]).forEach(function(object){ + var original = window.wrappedJSObject[object].prototype[name]; + + Object.defineProperty( + window.wrappedJSObject[object].prototype, + name, + { + enumerable: true, + configureable: false, + get: function(){ + if (!window.location.href){ + return undef; } - switch (funcStatus.mode){ - case "allow": - return original; - case "fake": - setRandomSupplyByType(prefs("rng")); - var fake = changedFunction.fakeGenerator(prefs, function(messageId){ - notify({url: window.location.href, errorStack: error.stack, messageId}); - }, original); - switch (fake){ - case true: - return original; - case false: - return undef; - default: - return exportFunction(fake, window.wrappedJSObject); - } - //case "block": - default: - return undef; + var error = new Error(); + if (checkStack(error.stack)){ + return original; + } + var funcStatus = changedFunction.getStatus(this, siteStatus); + + if (funcStatus.active){ + if (funcStatus.mode === "ask"){ + funcStatus.mode = ask({window: window, type: changedFunction.type, canvas: this, errorStack: error.stack}); + } + switch (funcStatus.mode){ + case "allow": + return original; + case "fake": + setRandomSupplyByType(prefs("rng")); + var fake = changedFunction.fakeGenerator(prefs, function(messageId){ + notify({url: window.location.href, errorStack: error.stack, messageId}); + }, original); + switch (fake){ + case true: + return original; + case false: + return undef; + default: + return exportFunction(fake, window.wrappedJSObject); + } + //case "block": + default: + return undef; + } + } + else { + return original; } - } - else { - return original; } } - } - ); + ); + }); } }); } diff --git a/lib/modifiedAPI.js b/lib/modifiedAPI.js index f95821f..3f74878 100644 --- a/lib/modifiedAPI.js +++ b/lib/modifiedAPI.js @@ -17,30 +17,37 @@ window.HTMLCanvasElement.prototype.getContext.call(canvas, "experimental-webgl2"); } function getImageData(window, context){ + var imageData; + var source; if (context instanceof window.CanvasRenderingContext2D){ - return window.CanvasRenderingContext2D.prototype.getImageData.call(context, 0, 0, context.canvas.width, context.canvas.height); + imageData = window.CanvasRenderingContext2D.prototype.getImageData.call(context, 0, 0, context.canvas.width, context.canvas.height); + source = imageData.data; } else { var imageData = new window.wrappedJSObject.ImageData(context.canvas.width, context.canvas.height); + var source = new Uint8Array(imageData.data.length); window.WebGLRenderingContext.prototype.readPixels.call( context, 0, 0, context.canvas.width, context.canvas.height, context.RGBA, context.UNSIGNED_BYTE, - imageData.data + source ); - return imageData; } + return { + imageData, + source + }; } function getFakeCanvas(window, original){ var context = getContext(window, original); - var imageData = getImageData(window, context); - var data = imageData.data; - var l = data.length; + var {imageData, source} = getImageData(window, context); + var desc = imageData.data; + var l = desc.length; var rng = randomSupply.getRng(l, window); - for (var i = 0; i < l; i += 1){ - data[i] = rng(data[i], i); + for (var i = 0; i < l; i += 10){ + desc[i] = rng(source[i], i); } var canvas = original.cloneNode(true); context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d"); @@ -249,7 +256,7 @@ status.active = hasType(status, "readout") || hasType(status, "input"); return status; }, - object: "WebGLRenderingContext", + object: ["WebGLRenderingContext", "WebGL2RenderingContext"], fakeGenerator: function(prefs, notify){ return function readPixels(x, y, width, height, format, type, pixels){ // not able to use the getFakeCanvas function because the context type is wrong...