mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-01-03 18:42:00 +01:00
Prevented error when wrong object is passed to the faked functions.
Solves the fourth problem in #114.
This commit is contained in:
parent
231e33ffb7
commit
a9c7d97708
@ -73,7 +73,7 @@
|
|||||||
setRandomSupplyByType(prefs("rng"));
|
setRandomSupplyByType(prefs("rng"));
|
||||||
var fake = changedFunction.fakeGenerator(prefs, function(messageId){
|
var fake = changedFunction.fakeGenerator(prefs, function(messageId){
|
||||||
notify({url, errorStack: error.stack, messageId});
|
notify({url, errorStack: error.stack, messageId});
|
||||||
}, original);
|
}, window, original);
|
||||||
switch (fake){
|
switch (fake){
|
||||||
case true:
|
case true:
|
||||||
return original;
|
return original;
|
||||||
|
@ -44,19 +44,25 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getFakeCanvas(window, original){
|
function getFakeCanvas(window, original){
|
||||||
var context = getContext(window, original);
|
try {
|
||||||
var {imageData, source} = getImageData(window, context);
|
// original may not be a canvas -> we must not leak an error
|
||||||
var desc = imageData.data;
|
var context = getContext(window, original);
|
||||||
var l = desc.length;
|
var {imageData, source} = getImageData(window, context);
|
||||||
var rng = randomSupply.getRng(l, window);
|
var desc = imageData.data;
|
||||||
|
var l = desc.length;
|
||||||
|
var rng = randomSupply.getRng(l, window);
|
||||||
|
|
||||||
for (var i = 0; i < l; i += 1){
|
for (var i = 0; i < l; i += 1){
|
||||||
desc[i] = rng(source[i], i);
|
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;
|
||||||
}
|
}
|
||||||
var canvas = original.cloneNode(true);
|
|
||||||
context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d");
|
|
||||||
context.putImageData(imageData, 0, 0);
|
|
||||||
return canvas;
|
|
||||||
}
|
}
|
||||||
function randomMixImageData(window, imageData1, imageData2){
|
function randomMixImageData(window, imageData1, imageData2){
|
||||||
var data1 = imageData1.data;
|
var data1 = imageData1.data;
|
||||||
@ -77,9 +83,6 @@
|
|||||||
return imageData2;
|
return imageData2;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWindow(canvas){
|
|
||||||
return canvas.ownerDocument.defaultView;
|
|
||||||
}
|
|
||||||
function hasType(status, type){
|
function hasType(status, type){
|
||||||
return status.type.indexOf(type) !== -1;
|
return status.type.indexOf(type) !== -1;
|
||||||
}
|
}
|
||||||
@ -114,11 +117,10 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
object: "HTMLCanvasElement",
|
object: "HTMLCanvasElement",
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function(context, contextAttributes){
|
return function(context, contextAttributes){
|
||||||
var window = getWindow(this);
|
|
||||||
canvasContextType.set(this, context);
|
canvasContextType.set(this, context);
|
||||||
return window.HTMLCanvasElement.prototype.getContext.apply(this, arguments);
|
return original.apply(this, window.Array.from(arguments));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -136,11 +138,10 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: "HTMLCanvasElement",
|
object: "HTMLCanvasElement",
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function toDataURL(){
|
return function toDataURL(){
|
||||||
notify("fakedReadout");
|
notify("fakedReadout");
|
||||||
var window = getWindow(this);
|
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
|
||||||
return window.HTMLCanvasElement.prototype.toDataURL.apply(getFakeCanvas(window, this), arguments);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -158,11 +159,10 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: "HTMLCanvasElement",
|
object: "HTMLCanvasElement",
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function toBlob(callback){
|
return function toBlob(callback){
|
||||||
notify("fakedReadout");
|
notify("fakedReadout");
|
||||||
var window = getWindow(this);
|
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
|
||||||
return window.HTMLCanvasElement.prototype.toBlob.apply(getFakeCanvas(window, this), arguments);
|
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
exportOptions: {allowCallbacks: true}
|
exportOptions: {allowCallbacks: true}
|
||||||
@ -181,11 +181,10 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: "HTMLCanvasElement",
|
object: "HTMLCanvasElement",
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function mozGetAsFile(callback){
|
return function mozGetAsFile(callback){
|
||||||
notify("fakedReadout");
|
notify("fakedReadout");
|
||||||
var window = getWindow(this);
|
return original.apply(getFakeCanvas(window, this), window.Array.from(arguments));
|
||||||
return window.HTMLCanvasElement.prototype.mozGetAsFile.apply(getFakeCanvas(window, this), arguments);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -203,19 +202,25 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: "CanvasRenderingContext2D",
|
object: "CanvasRenderingContext2D",
|
||||||
fakeGenerator: function(prefs, notify, original){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
var maxSize = prefs("maxFakeSize") || Number.POSITIVE_INFINITY;
|
var maxSize = prefs("maxFakeSize") || Number.POSITIVE_INFINITY;
|
||||||
return function getImageData(sx, sy, sw, sh){
|
return function getImageData(sx, sy, sw, sh){
|
||||||
var window = getWindow(this.canvas);
|
|
||||||
if (sw * sh > maxSize){
|
if (sw * sh > maxSize){
|
||||||
return original.apply(this, window.Array.from(arguments));
|
return original.apply(this, window.Array.from(arguments));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
notify("fakedReadout");
|
notify("fakedReadout");
|
||||||
var context = window.HTMLCanvasElement.prototype.getContext.call(
|
var fakeCanvas;
|
||||||
getFakeCanvas(window, this.canvas),
|
var context = this;
|
||||||
"2d"
|
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"
|
||||||
|
);
|
||||||
|
}
|
||||||
return original.apply(context, window.Array.from(arguments));
|
return original.apply(context, window.Array.from(arguments));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -229,12 +234,17 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: "CanvasRenderingContext2D",
|
object: "CanvasRenderingContext2D",
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function fillText(str, x, y){
|
return function fillText(str, x, y){
|
||||||
notify("fakedInput");
|
notify("fakedInput");
|
||||||
var window = getWindow(this.canvas);
|
var oldImageData;
|
||||||
var oldImageData = getImageData(window, this).imageData;
|
try {
|
||||||
var ret = window.CanvasRenderingContext2D.prototype.fillText.apply(this, arguments);
|
// "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;
|
var newImageData = getImageData(window, this).imageData;
|
||||||
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
|
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
|
||||||
return ret;
|
return ret;
|
||||||
@ -249,12 +259,17 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: "CanvasRenderingContext2D",
|
object: "CanvasRenderingContext2D",
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function strokeText(str, x, y){
|
return function strokeText(str, x, y){
|
||||||
notify("fakedInput");
|
notify("fakedInput");
|
||||||
var window = getWindow(this.canvas);
|
var oldImageData;
|
||||||
var oldImageData = getImageData(window, this).imageData;
|
try {
|
||||||
var ret = window.CanvasRenderingContext2D.prototype.strokeText.apply(this, arguments);
|
// "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;
|
var newImageData = getImageData(window, this).imageData;
|
||||||
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
|
this.putImageData(randomMixImageData(window, oldImageData, newImageData), 0, 0);
|
||||||
return ret;
|
return ret;
|
||||||
@ -269,13 +284,12 @@
|
|||||||
return status;
|
return status;
|
||||||
},
|
},
|
||||||
object: ["WebGLRenderingContext", "WebGL2RenderingContext"],
|
object: ["WebGLRenderingContext", "WebGL2RenderingContext"],
|
||||||
fakeGenerator: function(prefs, notify){
|
fakeGenerator: function(prefs, notify, window, original){
|
||||||
return function readPixels(x, y, width, height, format, type, pixels){
|
return function readPixels(x, y, width, height, format, type, pixels){
|
||||||
// not able to use the getFakeCanvas function because the context type is wrong...
|
// not able to use the getFakeCanvas function because the context type is wrong...
|
||||||
notify("fakedReadout");
|
notify("fakedReadout");
|
||||||
var xPixels = Cu.waiveXrays(pixels);
|
var xPixels = Cu.waiveXrays(pixels);
|
||||||
var window = getWindow(this.canvas);
|
var ret = original.apply(this, window.Array.from(arguments));
|
||||||
var ret = window.WebGLRenderingContext.prototype.readPixels.apply(this, arguments);
|
|
||||||
var l = xPixels.length;
|
var l = xPixels.length;
|
||||||
var rng = randomSupply.getRng(l, window);
|
var rng = randomSupply.getRng(l, window);
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ Version 0.3.8:
|
|||||||
fixes:
|
fixes:
|
||||||
- prevented error when canvas has size zero
|
- prevented error when canvas has size zero
|
||||||
- frame script was not loaded in the first tab that was restored from the last session
|
- frame script was not loaded in the first tab that was restored from the last session
|
||||||
|
- prevented error when wrong object is passed to the faked functions
|
||||||
|
|
||||||
Version 0.3.7:
|
Version 0.3.7:
|
||||||
new features:
|
new features:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user