-
Notifications
You must be signed in to change notification settings - Fork 2
/
NoCanvasFingerprinting.js
57 lines (49 loc) · 1.92 KB
/
NoCanvasFingerprinting.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// ==UserScript==
// @name NoCanvasFingerprinting
// @namespace NCF
// @description Inspects every script element before it gets executed to determine if it is bad. If so, replaces it.
// @include *
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
document.orig_createelement = document.createElement;
document.createElement = function(x) {
var ret_canvas = document.orig_createelement(x);
//Here we can override canvas functions
ret_canvas.orig_todataurl = ret_canvas.toDataURL;
ret_canvas.toDataURL = function() {
var randomID = Math.random().toString(10);
var saved_style = ret_canvas.getContext('2d').fillStyle;
var r = 255*Math.random()|0,
g = 255*Math.random()|0,
b = 255*Math.random()|0
a = .01;
ret_canvas.getContext('2d').fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
ret_canvas.getContext('2d').fillRect(0,0,ret_canvas.width,ret_canvas.height);
ret_canvas.getContext('2d').fillStyle = saved_style;
return ret_canvas.orig_todataurl();
}
ret_canvas.orig_getcontext = ret_canvas.getContext;
ret_canvas.getContext = function(x) {
var ret_ctx = ret_canvas.orig_getcontext(x);
//Here we can override ctx functions
ret_ctx.orig_getimagedata = ret_ctx.getImageData;
ret_ctx.getImageData = function(w,x,y,z) {
var imagedata = ret_ctx.orig_getimagedata(w,x,y,z);
for (var i = 0; i < imagedata.data.length; i+=4) {
//alpha blend the pixel to emulate ctx fill
var r = 255*Math.random()|0,
g = 255*Math.random()|0,
b = 255*Math.random()|0
imagedata[i] = r + (imagedata[i] * .99);
imagedata[i+1] = g + (imagedata[i+1] * .99);
imagedata[i+2] = b + (imagedata[i+2] * .99);
imagedata[i+3] = a + (imagedata[i+3] * .99);
}
return imagedata;
}
return ret_ctx;
}
return ret_canvas;
}