-
Notifications
You must be signed in to change notification settings - Fork 3
/
headlessDetect.js
97 lines (77 loc) · 2.67 KB
/
headlessDetect.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class HeadlessDetect {
allTestFunctions = ['testUserAgent', 'testChromeWindow', 'testPlugins', 'testAppVersion', 'testConnectionRtt'];
constructor() {
}
//* All Tests *//
// User Agent
testUserAgent() {
if (/Headless/.test(window.navigator.userAgent)) {
// Headless
return 1;
} else {
// Not Headless
return 0;
}
}
// Window.Chrome
testChromeWindow() {
if (eval.toString().length == 33 && !window.chrome) {
// Headless
return 1;
} else {
// Not Headless
return 0;
}
}
// Notification Permissions
testNotificationPermissions(callback) {
navigator.permissions.query({name:'notifications'}).then(function(permissionStatus) {
if(Notification.permission === 'denied' && permissionStatus.state === 'prompt') {
// Headless
callback(1);
} else {
// Not Headless
callback(0);
}
});
}
// No Plugins
testPlugins() {
let length = navigator.plugins.length;
return length === 0 ? 1 : 0;
}
// App Version
testAppVersion() {
let appVersion = navigator.appVersion;
return /headless/i.test(appVersion) ? 1 : 0;
}
// Connection Rtt
testConnectionRtt() {
let connection = navigator.connection;
let connectionRtt = connection ? connection.rtt : undefined;
if (connectionRtt === undefined) {
return 0; // Flag doesn't even exists so just return NOT HEADLESS
} else {
return connectionRtt === 0 ? 1 : 0;
}
}
//* Main Functions *//
getHeadlessScore() {
let score = 0;
let testsRun = 0;
// Notification Permissions test has to be done using Callbacks
// That's why it's done separately from all the other tests.
this.testNotificationPermissions(function(v){
score += v;
testsRun++;
//document.write("<p>testNotificationPermissions: " + v + "</p>"); // This is only used for debugging
});
// Loop through all functions and add their results together
for(let i = 0; i < this.allTestFunctions.length; i++){
score += this[this.allTestFunctions[i]].apply();
testsRun++;
//document.write("<p>" + this.allTestFunctions[i] + ": " + this[this.allTestFunctions[i]].apply()+ "</p>"); // This is only used for debugging
}
return score / testsRun;
}
}