Skip to content

Instantly share code, notes, and snippets.

@chelovekula
Created September 20, 2019 15:24
Show Gist options
  • Save chelovekula/5c3dec0078958af9a6a21471e7619155 to your computer and use it in GitHub Desktop.
Save chelovekula/5c3dec0078958af9a6a21471e7619155 to your computer and use it in GitHub Desktop.
function overrideDefaultMethods(r, g, b, a, scriptId) {
var scriptNode = document.getElementById(scriptId);
function overrideCanvasProto(root) {
function overrideCanvasInternal(name, old) {
root.prototype[name] = old;
Object.defineProperty(root.prototype, name,
{
value: function () {
var width = this.width;
console.log(width);
var height = this.height;
console.log(height);
var context = this.getContext("2d");
if (!context) {
var can = document.createElement('canvas');
var imageData = can.getContext('2d').getImageData(0, 0, width, height);
} else {
var imageData = context.getImageData(0, 0, width, height);
}
console.log(imageData);
console.log(r, g, b, a);
for (var i = 0; i < height; i++) {
for (var j = 0; j < width; j++) {
var index = ((i * (width * 4)) + (j * 4));
imageData.data[index + 0] = imageData.data[index + 0] + r;
imageData.data[index + 1] = imageData.data[index + 1] + g;
imageData.data[index + 2] = imageData.data[index + 2] + b;
imageData.data[index + 3] = imageData.data[index + 3] + a;
}
}
if (!context) {
can.getContext('2d').putImageData(imageData, 0, 0);
return old.apply(can, arguments);
} else {
context.putImageData(imageData, 0, 0);
return old.apply(this, arguments);
}
}
}
);
}
overrideCanvasInternal("toDataURL", root.prototype.toDataURL);
overrideCanvasInternal("toBlob", root.prototype.toBlob);
}
function overrideCanvaRendProto(root) {
const name = "getImageData";
const getImageData = root.prototype.getImageData;
root.prototype[name] = getImageData;
Object.defineProperty(root.prototype, "getImageData",
{
value: function () {
var imageData = getImageData.apply(this, arguments);
var height = imageData.height;
var width = imageData.width;
console.log(width);
console.log(height);
console.log(imageData);
for (var i = 0; i < height; i++) {
for (var j = 0; j < width; j++) {
var index = ((i * (width * 4)) + (j * 4));
imageData.data[index + 0] = imageData.data[index + 0] + r;
imageData.data[index + 1] = imageData.data[index + 1] + g;
imageData.data[index + 2] = imageData.data[index + 2] + b;
imageData.data[index + 3] = imageData.data[index + 3] + a;
}
}
return imageData;
}
}
);
}
function inject(element) {
if (element.tagName.toUpperCase() === "IFRAME" && element.contentWindow) {
try {
var hasAccess = element.contentWindow.HTMLCanvasElement;
} catch (e) {
return;
}
overrideCanvasProto(element.contentWindow.HTMLCanvasElement);
overrideCanvaRendProto(element.contentWindow.CanvasRenderingContext2D);
overrideDocumentProto(element.contentWindow.Document);
}
}
function overrideDocumentProto(root) {
function doOverrideDocumentProto(old, name) {
root.prototype[name] = old;
Object.defineProperty(root.prototype, name,
{
value: function () {
var element = old.apply(this, arguments);
if (element == null) {
return null;
}
if (Object.prototype.toString.call(element) === '[object HTMLCollection]' ||
Object.prototype.toString.call(element) === '[object NodeList]') {
for (var i = 0; i < element.length; ++i) {
var el = element[i];
inject(el);
}
} else {
inject(element);
}
return element;
}
}
);
}
doOverrideDocumentProto(root.prototype.createElement, "createElement");
doOverrideDocumentProto(root.prototype.createElementNS, "createElementNS");
doOverrideDocumentProto(root.prototype.getElementById, "getElementById");
doOverrideDocumentProto(root.prototype.getElementsByName, "getElementsByName");
doOverrideDocumentProto(root.prototype.getElementsByClassName, "getElementsByClassName");
doOverrideDocumentProto(root.prototype.getElementsByTagName, "getElementsByTagName");
doOverrideDocumentProto(root.prototype.getElementsByTagNameNS, "getElementsByTagNameNS");
}
if (canvasNoise == 1) {
overrideCanvasProto(HTMLCanvasElement);
overrideCanvaRendProto(CanvasRenderingContext2D);
overrideDocumentProto(Document);
scriptNode.parentNode.removeChild(scriptNode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment