1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 12:36:37 +02:00

Added minimal fake size and respected the fake sizes in all fakeable functions.

This commit is contained in:
kkapsner 2017-07-18 16:11:12 +02:00
parent 0d7e8bdd8b
commit 5575c50a03
6 changed files with 118 additions and 45 deletions

View file

@ -4,6 +4,7 @@ var settings = {
whiteList: "",
blackList: "",
blockMode: "fakeReadout",
minFakeSize: 1,
maxFakeSize: 0,
rng: "nonPersistent",
persistentRndStorage: "",

View file

@ -92,6 +92,18 @@
return imageData2;
}
function canvasSizeShouldBeFaked(canvas, prefs){
if (canvas){
var size = canvas.height * canvas.width;
var maxSize = prefs("maxFakeSize") || Number.POSITIVE_INFINITY;
var minSize = prefs("minFakeSize") || 0;
return size > minSize & size <= maxSize;
}
else {
return true
}
}
function hasType(status, type){
return status.type.indexOf(type) !== -1;
}
@ -149,8 +161,13 @@
object: "HTMLCanvasElement",
fakeGenerator: function(prefs, notify, window, original){
return function toDataURL(){
notify.call(this, "fakedReadout");
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
if (canvasSizeShouldBeFaked(this, prefs)){
notify.call(this, "fakedReadout");
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
}
else {
return original.apply(this, window.Array.from(arguments));
}
};
}
},
@ -170,8 +187,13 @@
object: "HTMLCanvasElement",
fakeGenerator: function(prefs, notify, window, original){
return function toBlob(callback){
notify.call(this, "fakedReadout");
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
if (canvasSizeShouldBeFaked(this, prefs)){
notify.call(this, "fakedReadout");
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
}
else {
return original.apply(this, window.Array.from(arguments));
}
};
},
exportOptions: {allowCallbacks: true}
@ -192,8 +214,13 @@
object: "HTMLCanvasElement",
fakeGenerator: function(prefs, notify, window, original){
return function mozGetAsFile(callback){
notify.call(this, "fakedReadout");
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
if (canvasSizeShouldBeFaked(this, prefs)){
notify.call(this, "fakedReadout");
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
}
else {
return original.apply(this, window.Array.from(arguments));
}
};
}
},
@ -212,12 +239,8 @@
},
object: "CanvasRenderingContext2D",
fakeGenerator: function(prefs, notify, window, original){
var maxSize = prefs("maxFakeSize") || Number.POSITIVE_INFINITY;
return function getImageData(sx, sy, sw, sh){
if (sw * sh > maxSize){
return original.apply(this, window.Array.from(arguments));
}
else {
if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){
notify.call(this, "fakedReadout");
var fakeCanvas;
var context = this;
@ -232,6 +255,9 @@
}
return original.apply(context, window.Array.from(arguments));
}
else {
return original.apply(this, window.Array.from(arguments));
}
};
}
},
@ -245,18 +271,23 @@
object: "CanvasRenderingContext2D",
fakeGenerator: function(prefs, notify, window, original){
return function fillText(str, x, y){
notify.call(this, "fakedInput");
var oldImageData;
try {
// "this" is not trustable - it may be not a context
oldImageData = getImageData(window, this).imageData;
if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){
notify.call(this, "fakedInput");
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));
var newImageData = getImageData(window, this).imageData;
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
return ret;
}
else {
return original.apply(this, window.Array.from(arguments));
}
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));
var newImageData = getImageData(window, this).imageData;
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
return ret;
};
}
},
@ -270,18 +301,23 @@
object: "CanvasRenderingContext2D",
fakeGenerator: function(prefs, notify, window, original){
return function strokeText(str, x, y){
notify.call(this, "fakedInput");
var oldImageData;
try {
// "this" is not trustable - it may be not a context
oldImageData = getImageData(window, this).imageData;
if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){
notify.call(this, "fakedInput");
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));
var newImageData = getImageData(window, this).imageData;
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
return ret;
}
else {
return original.apply(this, window.Array.from(arguments));
}
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));
var newImageData = getImageData(window, this).imageData;
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
return ret;
};
}
},
@ -295,18 +331,23 @@
object: ["WebGLRenderingContext", "WebGL2RenderingContext"],
fakeGenerator: function(prefs, notify, window, original){
return function readPixels(x, y, width, height, format, type, pixels){
// not able to use the getFakeCanvas function because the context type is wrong...
notify.call(this, "fakedReadout");
var xPixels = pixels;
var ret = original.apply(this, window.Array.from(arguments));
var l = xPixels.length;
var rng = randomSupply.getRng(l, window);
for (var i = 0; i < l; i += 1){
xPixels[i] = rng(xPixels[i], i);
if (!this || canvasSizeShouldBeFaked(this.canvas, prefs)){
// not able to use the getFakeCanvas function because the context type is wrong...
notify.call(this, "fakedReadout");
var xPixels = pixels;
var ret = original.apply(this, window.Array.from(arguments));
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;
}
else {
return original.apply(this, window.Array.from(arguments));
}
return ret;
};
}
}