2016-08-06 19:17:36 +02:00
|
|
|
/* jslint moz: true */
|
2015-09-06 12:26:50 +02:00
|
|
|
/* 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";
|
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.scope.modifiedAPI = {};
|
|
|
|
scope = window.scope.modifiedAPI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// let Cu = require("chrome").Cu;
|
2016-11-26 17:37:52 +01:00
|
|
|
|
2016-08-06 19:17:36 +02:00
|
|
|
var randomSupply = null;
|
|
|
|
|
2016-10-23 22:12:12 +02:00
|
|
|
function getContext(window, canvas){
|
|
|
|
return window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d") ||
|
|
|
|
window.HTMLCanvasElement.prototype.getContext.call(canvas, "webgl") ||
|
|
|
|
window.HTMLCanvasElement.prototype.getContext.call(canvas, "experimental-webgl") ||
|
|
|
|
window.HTMLCanvasElement.prototype.getContext.call(canvas, "webgl2") ||
|
|
|
|
window.HTMLCanvasElement.prototype.getContext.call(canvas, "experimental-webgl2");
|
|
|
|
}
|
|
|
|
function getImageData(window, context){
|
2016-12-26 14:36:01 +01:00
|
|
|
var imageData;
|
|
|
|
var source;
|
2017-04-19 19:32:37 +02:00
|
|
|
if ((context.canvas.width || 0) * (context.canvas.height || 0) === 0){
|
|
|
|
imageData = new window.wrappedJSObject.ImageData(0, 0);
|
|
|
|
source = new window.wrappedJSObject.ImageData(0, 0);
|
|
|
|
}
|
|
|
|
else if (context instanceof window.CanvasRenderingContext2D){
|
2016-12-26 14:36:01 +01:00
|
|
|
imageData = window.CanvasRenderingContext2D.prototype.getImageData.call(context, 0, 0, context.canvas.width, context.canvas.height);
|
|
|
|
source = imageData.data;
|
2015-09-10 01:35:49 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-04-19 19:32:37 +02:00
|
|
|
imageData = new window.wrappedJSObject.ImageData(context.canvas.width, context.canvas.height);
|
|
|
|
source = new Uint8Array(imageData.data.length);
|
2015-09-10 01:35:49 +02:00
|
|
|
window.WebGLRenderingContext.prototype.readPixels.call(
|
|
|
|
context,
|
2016-10-23 22:12:12 +02:00
|
|
|
0, 0, context.canvas.width, context.canvas.height,
|
2015-09-10 01:35:49 +02:00
|
|
|
context.RGBA, context.UNSIGNED_BYTE,
|
2016-12-26 14:36:01 +01:00
|
|
|
source
|
2015-09-10 01:35:49 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-26 14:36:01 +01:00
|
|
|
return {
|
|
|
|
imageData,
|
|
|
|
source
|
|
|
|
};
|
2016-10-23 22:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getFakeCanvas(window, original){
|
2017-05-05 09:18:11 +02:00
|
|
|
try {
|
|
|
|
// original may not be a canvas -> we must not leak an error
|
|
|
|
var context = getContext(window, original);
|
|
|
|
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){
|
|
|
|
desc[i] = rng(source[i], i);
|
|
|
|
}
|
|
|
|
var canvas = original.cloneNode(true);
|
|
|
|
context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d");
|
|
|
|
context.putImageData(imageData, 0, 0);
|
|
|
|
return canvas;
|
|
|
|
}
|
|
|
|
catch (e){
|
|
|
|
return original;
|
2015-09-06 12:26:50 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-23 22:12:12 +02:00
|
|
|
function randomMixImageData(window, imageData1, imageData2){
|
|
|
|
var data1 = imageData1.data;
|
|
|
|
var data2 = imageData2.data;
|
|
|
|
var l = data1.length;
|
|
|
|
if (l === data2.length){
|
|
|
|
var rng = randomSupply.getRng(l, window);
|
|
|
|
|
|
|
|
for (var i = 0; i < l; i += 1){
|
|
|
|
if (data1[i] > data2[i]){
|
|
|
|
data2[i] = data1[i] - rng(data1[i] - data2[i], i);
|
|
|
|
}
|
|
|
|
else if (data1[i] < data2[i]){
|
|
|
|
data2[i] = data1[i] + rng(data2[i] - data1[i], i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return imageData2;
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasType(status, type){
|
|
|
|
return status.type.indexOf(type) !== -1;
|
|
|
|
}
|
2016-08-06 19:17:36 +02:00
|
|
|
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.setRandomSupply = function(supply){
|
2016-08-06 19:17:36 +02:00
|
|
|
randomSupply = supply;
|
|
|
|
};
|
2016-10-23 22:12:12 +02:00
|
|
|
var canvasContextType = new WeakMap();
|
2015-09-06 12:26:50 +02:00
|
|
|
// changed functions and their fakes
|
2017-06-25 22:33:12 +02:00
|
|
|
scope.changedFunctions = {
|
2015-09-06 12:26:50 +02:00
|
|
|
getContext: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "context",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-13 14:55:42 +01:00
|
|
|
if (hasType(status, "internal")){
|
|
|
|
return {
|
|
|
|
mode: "allow",
|
|
|
|
type: status.type,
|
|
|
|
active: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (hasType(status, "context") || hasType(status, "input")){
|
|
|
|
return {
|
|
|
|
mode: (status.mode === "block")? "block": "fake",
|
|
|
|
type: status.type,
|
|
|
|
active: true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-11-13 14:55:42 +01:00
|
|
|
status.active = false;
|
|
|
|
return status;
|
|
|
|
}
|
2016-10-23 22:12:12 +02:00
|
|
|
},
|
|
|
|
object: "HTMLCanvasElement",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function(context, contextAttributes){
|
|
|
|
canvasContextType.set(this, context);
|
2017-05-05 09:18:11 +02:00
|
|
|
return original.apply(this, window.Array.from(arguments));
|
2016-10-23 22:12:12 +02:00
|
|
|
};
|
|
|
|
}
|
2015-09-06 12:26:50 +02:00
|
|
|
},
|
|
|
|
toDataURL: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "readout",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
if (hasType(status, "input")){
|
|
|
|
var contextType = canvasContextType.get(obj);
|
2016-11-13 15:09:03 +01:00
|
|
|
status.active = contextType !== "2d";
|
2016-10-23 22:12:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.active = hasType(status, "readout");
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
},
|
2015-09-06 12:26:50 +02:00
|
|
|
object: "HTMLCanvasElement",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function toDataURL(){
|
|
|
|
notify("fakedReadout");
|
2017-05-05 09:18:11 +02:00
|
|
|
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
|
2016-10-23 22:12:12 +02:00
|
|
|
};
|
2015-09-06 12:26:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
toBlob: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "readout",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
if (hasType(status, "input")){
|
|
|
|
var contextType = canvasContextType.get(obj);
|
2016-11-13 15:09:03 +01:00
|
|
|
status.active = contextType !== "2d";
|
2016-10-23 22:12:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.active = hasType(status, "readout");
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
},
|
2015-09-06 12:26:50 +02:00
|
|
|
object: "HTMLCanvasElement",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function toBlob(callback){
|
|
|
|
notify("fakedReadout");
|
2017-05-05 09:18:11 +02:00
|
|
|
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
|
2016-10-23 22:12:12 +02:00
|
|
|
};
|
2015-09-06 12:26:50 +02:00
|
|
|
},
|
|
|
|
exportOptions: {allowCallbacks: true}
|
|
|
|
},
|
|
|
|
mozGetAsFile: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "readout",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
if (hasType(status, "input")){
|
|
|
|
var contextType = canvasContextType.get(obj);
|
2016-11-13 15:09:03 +01:00
|
|
|
status.active = contextType !== "2d";
|
2016-10-23 22:12:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.active = hasType(status, "readout");
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
},
|
2015-09-06 12:26:50 +02:00
|
|
|
object: "HTMLCanvasElement",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function mozGetAsFile(callback){
|
|
|
|
notify("fakedReadout");
|
2017-05-05 09:18:11 +02:00
|
|
|
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
|
2016-10-23 22:12:12 +02:00
|
|
|
};
|
2015-09-06 12:26:50 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
getImageData: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "readout",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
if (hasType(status, "input")){
|
|
|
|
var contextType = canvasContextType.get(obj);
|
2016-11-13 15:09:03 +01:00
|
|
|
status.active = contextType !== "2d";
|
2016-10-23 22:12:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
status.active = hasType(status, "readout");
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
},
|
2015-09-06 12:26:50 +02:00
|
|
|
object: "CanvasRenderingContext2D",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-05-10 08:01:21 +02:00
|
|
|
var maxSize = prefs("maxFakeSize") || Number.POSITIVE_INFINITY;
|
|
|
|
return function getImageData(sx, sy, sw, sh){
|
2016-05-11 08:58:45 +02:00
|
|
|
if (sw * sh > maxSize){
|
2016-12-03 17:29:30 +01:00
|
|
|
return original.apply(this, window.Array.from(arguments));
|
2016-05-11 08:58:45 +02:00
|
|
|
}
|
|
|
|
else {
|
2016-10-23 22:12:12 +02:00
|
|
|
notify("fakedReadout");
|
2017-05-05 09:18:11 +02:00
|
|
|
var fakeCanvas;
|
|
|
|
var context = this;
|
|
|
|
if (this && this.canvas) {
|
|
|
|
fakeCanvas = getFakeCanvas(window, this.canvas);
|
|
|
|
}
|
|
|
|
if (fakeCanvas && fakeCanvas !== this.canvas){
|
|
|
|
context = window.HTMLCanvasElement.prototype.getContext.call(
|
|
|
|
getFakeCanvas(window, this.canvas),
|
|
|
|
"2d"
|
|
|
|
);
|
|
|
|
}
|
2016-12-03 17:29:30 +01:00
|
|
|
return original.apply(context, window.Array.from(arguments));
|
2016-05-11 08:58:45 +02:00
|
|
|
}
|
2016-08-06 19:17:36 +02:00
|
|
|
};
|
2015-09-06 12:26:50 +02:00
|
|
|
}
|
|
|
|
},
|
2016-10-23 22:12:12 +02:00
|
|
|
fillText: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "input",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
status.active = hasType(status, "input");
|
|
|
|
return status;
|
|
|
|
},
|
|
|
|
object: "CanvasRenderingContext2D",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function fillText(str, x, y){
|
|
|
|
notify("fakedInput");
|
2017-05-05 09:18:11 +02:00
|
|
|
var oldImageData;
|
|
|
|
try {
|
|
|
|
// "this" is not trustable - it may be not a context
|
|
|
|
oldImageData = getImageData(window, this).imageData;
|
|
|
|
}
|
|
|
|
catch (e){}
|
|
|
|
// if "this" is not a correct context the next line will throw an error
|
|
|
|
var ret = original.apply(this, window.Array.from(arguments));
|
2017-02-10 16:30:11 +01:00
|
|
|
var newImageData = getImageData(window, this).imageData;
|
2016-10-23 22:12:12 +02:00
|
|
|
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
strokeText: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "input",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
status.active = hasType(status, "input");
|
|
|
|
return status;
|
|
|
|
},
|
|
|
|
object: "CanvasRenderingContext2D",
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function strokeText(str, x, y){
|
|
|
|
notify("fakedInput");
|
2017-05-05 09:18:11 +02:00
|
|
|
var oldImageData;
|
|
|
|
try {
|
|
|
|
// "this" is not trustable - it may be not a context
|
|
|
|
oldImageData = getImageData(window, this).imageData;
|
|
|
|
}
|
|
|
|
catch (e){}
|
|
|
|
// if "this" is not a correct context the next line will throw an error
|
|
|
|
var ret = original.apply(this, window.Array.from(arguments));
|
2017-02-10 16:30:11 +01:00
|
|
|
var newImageData = getImageData(window, this).imageData;
|
2016-10-23 22:12:12 +02:00
|
|
|
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
2015-09-06 12:26:50 +02:00
|
|
|
readPixels: {
|
2017-03-03 00:55:32 +01:00
|
|
|
type: "readout",
|
2016-10-23 22:12:12 +02:00
|
|
|
getStatus: function(obj, status){
|
2016-11-19 15:35:00 +01:00
|
|
|
var status = Object.create(status);
|
2016-10-23 22:12:12 +02:00
|
|
|
status.active = hasType(status, "readout") || hasType(status, "input");
|
|
|
|
return status;
|
|
|
|
},
|
2016-12-26 14:36:01 +01:00
|
|
|
object: ["WebGLRenderingContext", "WebGL2RenderingContext"],
|
2017-05-05 09:18:11 +02:00
|
|
|
fakeGenerator: function(prefs, notify, window, original){
|
2016-10-23 22:12:12 +02:00
|
|
|
return function readPixels(x, y, width, height, format, type, pixels){
|
2016-11-26 17:37:52 +01:00
|
|
|
// not able to use the getFakeCanvas function because the context type is wrong...
|
2016-10-23 22:12:12 +02:00
|
|
|
notify("fakedReadout");
|
2017-06-25 22:33:12 +02:00
|
|
|
var xPixels = pixels//Cu.waiveXrays(pixels);
|
2017-05-05 09:18:11 +02:00
|
|
|
var ret = original.apply(this, window.Array.from(arguments));
|
2016-11-26 17:37:52 +01:00
|
|
|
var l = xPixels.length;
|
|
|
|
var rng = randomSupply.getRng(l, window);
|
|
|
|
|
|
|
|
for (var i = 0; i < l; i += 1){
|
|
|
|
xPixels[i] = rng(xPixels[i], i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-10-23 22:12:12 +02:00
|
|
|
};
|
2015-09-06 12:26:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|