Created
January 26, 2020 13:07
-
-
Save nicoandmee/743315e66ecba7db970ff4ca4992cf9f to your computer and use it in GitHub Desktop.
[canvas-webgl-puppeteer-spoof]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
() => { | |
function hookPrototypeMethods(prefix, object) { | |
// TODO: also hook getters | |
if (!object) return; | |
const originals = {}; | |
const prototype = Object.getPrototypeOf(object); | |
Object | |
.getOwnPropertyNames(prototype) | |
.filter((n) => { | |
try { | |
return typeof prototype[n] === 'function'; | |
} catch (error) { | |
return false; | |
} | |
}) | |
.forEach((n) => { | |
originals[n] = prototype[n]; | |
// eslint-disable-next-line func-names | |
prototype[n] = function fn(...args) { | |
if (prefix === '2d' && (n === 'strokeText' || n === 'fillText')) { | |
const temp = Array.from(args); | |
temp[0] = fingerprint.BUID; | |
temp[1] = Math.max(0, temp[1] - 2); | |
temp[2] = Math.max(0, temp[2] - 2); | |
originals[n].call(this, ...temp); | |
} | |
const result = originals[n].call(this, ...args); | |
if (LO) { | |
let jsonResult; | |
try { | |
jsonResult = JSON.stringify(result); | |
// eslint-disable-next-line no-empty | |
} catch (e) {} | |
// eslint-disable-next-line no-console | |
console.log('function called', prefix, n, JSON.stringify(args), 'result:', result, jsonResult, `${result}`); | |
} | |
return result; | |
}; | |
}); | |
} | |
const gls = []; | |
try { | |
gls.push(document.createElement('canvas').getContext('webgl')); | |
gls.push(document.createElement('canvas').getContext('experimental-webgl')); | |
// eslint-disable-next-line no-empty | |
} catch (e) {} | |
gls.forEach((gl) => { | |
const glProto = Object.getPrototypeOf(gl); | |
const origGetParameter = glProto.getParameter; | |
const debugInfo = gl.getExtension('WEBGL_debug_renderer_info'); | |
if (gl) { | |
glProto.getParameter = function getParameter(...args) { | |
if (args[0] === debugInfo.UNMASKED_VENDOR_WEBGL) return logOverride('gl.getParameter.UNMASKED_VENDOR_WEBGL', fingerprint.GL_PARAMETER.VENDOR); // REPLACE WITH RANDOM VENDOR | |
if (args[0] === debugInfo.UNMASKED_RENDERER_WEBGL) return logOverride('gl.getParameter.UNMASKED_RENDERER_WEBGL', fingerprint.GL_PARAMETER.RENDERER); // REPLACE WITH RANDOM RENDERER | |
if (args[0] === 33901) return new Float32Array([1, 8191]); | |
if (args[0] === 3386) return new Int32Array([16384, 16384]); | |
if (args[0] === 35661) return 80; | |
if (args[0] === 34076) return 16384; | |
if (args[0] === 36349) return 1024; | |
if (args[0] === 34024) return 16384; | |
if (args[0] === 3379) return 16384; | |
if (args[0] === 34921) return 16; | |
if (args[0] === 36347) return 1024; | |
return origGetParameter.call(this, ...args); | |
}; | |
} | |
}); | |
hookPrototypeMethods('webgl', document.createElement('canvas').getContext('webgl')); | |
hookPrototypeMethods('experimental-webgl', document.createElement('canvas').getContext('experimental-webgl')); | |
hookPrototypeMethods('2d', document.createElement('canvas').getContext('2d')); | |
hookPrototypeMethods('canvas', canvas); | |
hookPrototypeMethods('screen', window.screen); | |
hookPrototypeMethods('navigator', window.navigator); | |
hookPrototypeMethods('history', window.history); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment