1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-01-08 20:54:49 +01:00

Made toDataURL() and toBlob() return valid data.

This commit is contained in:
kkapsner 2015-09-01 21:28:03 +02:00
parent 513e96f6c5
commit 637f2dd465

View File

@ -41,14 +41,6 @@
}; };
var undef; var undef;
var randomImage = (function(){
var length = Math.floor(20 + Math.random() * 100);
var bytes = "";
for (var i = 0; i < length; i += 1){
bytes += String.fromCharCode(Math.floor(Math.random() * 256));
}
return bytes;
}());
// Stack parsing // Stack parsing
@ -138,6 +130,13 @@
}; };
} }
function getFakeCanvas(original){
var imageData = changedFunctions.getImageData.fake(0, 0, this.width, this.height);
var canvas = this.cloneNode(true);
var context = canvas.getContext("2d");
context.putImageData(imageData, 0, 0);
return canvas;
}
// changed functions and their fakes // changed functions and their fakes
var changedFunctions = { var changedFunctions = {
getContext: { getContext: {
@ -148,17 +147,14 @@
mode: blockMode.readAPI, mode: blockMode.readAPI,
object: unsafeWindow.HTMLCanvasElement, object: unsafeWindow.HTMLCanvasElement,
fake: function toDataURL(){ fake: function toDataURL(){
var type = arguments[0] || "image/png"; return window.HTMLCanvasElement.prototype.toDataURL.apply(getFakeCanvas(this), arguments);
return "data:" + type + ";base64," + btoa(randomImage);
} }
}, },
toBlob: { toBlob: {
mode: blockMode.readAPI, mode: blockMode.readAPI,
object: unsafeWindow.HTMLCanvasElement, object: unsafeWindow.HTMLCanvasElement,
fake: function toBlob(callback){ fake: function toBlob(callback){
var type = arguments[0] || "image/png"; window.HTMLCanvasElement.prototype.toBlob.apply(getFakeCanvas(this), arguments);
var blob = new window.Blob(randomImage, {type: type});
callback(blob);
}, },
exportOptions: {allowCallbacks: true} exportOptions: {allowCallbacks: true}
}, },