-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhumanity-check.js
70 lines (59 loc) · 1.83 KB
/
humanity-check.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
/* eslint-disable class-methods-use-this */
const store = require('./store');
const botList = require('./botlist');
const helpers = require('./helpers');
const storageKey = '_constructorio_is_human';
const humanEvents = [
'scroll',
'resize',
'touchmove',
'mouseover',
'mousemove',
'keydown',
'keypress',
'keyup',
'focus',
];
class HumanityCheck {
constructor() {
// Check if a human event has been performed in the past
this.hasPerformedHumanEvent = this.getIsHumanFromSessionStorage();
// Humanity proved, remove handlers
const remove = () => {
this.hasPerformedHumanEvent = true;
store.session.set(storageKey, true);
humanEvents.forEach((eventType) => {
helpers.removeEventListener(eventType, remove, true);
});
};
// Add handlers to prove humanity
if (!this.hasPerformedHumanEvent) {
humanEvents.forEach((eventType) => {
helpers.addEventListener(eventType, remove, true);
});
}
}
// Helper function to grab the human variable from session storage
getIsHumanFromSessionStorage() {
return !!store.session.get(storageKey) || false;
}
// Return boolean indicating if user is a bot
// ...if it has a bot-like useragent
// ...or uses webdriver
// ...or has not performed a human event
isBot() {
const { userAgent, webdriver } = helpers.getNavigator();
const botRegex = new RegExp(`(${botList.join('|')})`);
// Always check the user agent and webdriver fields first to determine if the user is a bot
if (Boolean(userAgent.match(botRegex)) || Boolean(webdriver)) {
return true;
}
// If the user hasn't performed a human event, it indicates it is a bot
if (!this.getIsHumanFromSessionStorage()) {
return true;
}
// Otherwise, it is a human
return false;
}
}
module.exports = HumanityCheck;