-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.js
97 lines (83 loc) · 2.77 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
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
(function() {
var socket = io( 'http://localhost:3334' );
socket.on( 'connection', function() {
console.log( 'socket::connection' );
socket.on( 'disconnect', function() {
console.log( 'socket::disconnect' );
} );
} );
socket.on( 'sync', function( html ) {
console.log( 'socket::sync' );
window.document.body.outerHTML = html;
} );
var tmpdiv = null;
socket.on( 'update', function( update ) {
console.log( 'socket::update' );
for( var i = 0; i < update.length; i++ ) {
var nit = window.document.body;
for( var j = 0; j < update[i].path.length; j++ ) {
if( nit.childNodes[update[i].path[j]] ) {
nit = nit.childNodes[update[i].path[j]];
}
}
if( update[i].type == 'DOMNodeInserted' ) {
tmpdiv = tmpdiv || document.createElement( 'div' );
tmpdiv.innerHTML = update[i].diff;
nit.appendChild( tmpdiv.firstChild );
} else if( update[i].type == 'DOMCharacterDataModified' ) {
nit.nodeValue = update[i].diff;
}
}
} );
function getPath( element ) {
if( element.parentNode == window.document || element.parentNode == window.document.documentElement ) {
return nit != window.document.body ? null : [];
}
var path = [], nit = element;
while( nit != window.document.body || nit == null ) {
for( var index = 0; nit.parentNode.childNodes[index] != nit; index++ ) {
if( index > 100 ) {
console.log( '?' );
}
}
path.push( index );
nit = nit.parentNode;
}
path.reverse();
return path;
}
var queue = [];
function track( event ) {
document.addEventListener( event, function( event ) {
queue.push( {
path: getPath( event.toElement ),
type: event.type,
screenX: event.screenX,
screenY: event.screenY,
clientX: event.clientX,
clientY: event.clientY,
ctrl: event.ctrl,
alt: event.alt,
shift: event.shift,
meta: event.meta,
button: event.button,
x: event.x,
y: event.y
} );
} );
}
track( 'click' );
track( 'mousemove' );
track( 'mousedown' );
track( 'mouseup' );
setInterval( function() {
if( !socket.connected ) {
return;
}
if( queue.length == 0 ) {
return;
}
socket.emit( 'mouse', queue );
queue = [];
}, 25 )
})();