-
Notifications
You must be signed in to change notification settings - Fork 47
/
eventLogger.html
239 lines (214 loc) · 5.71 KB
/
eventLogger.html
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<!DOCTYPE html>
<html>
<!--
/*!
* VisualEditor keyboard event logger page
*
* @copyright See AUTHORS.txt
*/
-->
<head>
<meta charset="utf-8">
<title>Keyboard Event Logger</title>
<style>
html {
margin: 0;
padding: 0;
height: 100%;
}
body {
margin: 1em;
padding: 0;
height: 100%;
}
.ve-demo-form {
width: 100%;
min-height: 2em;
}
.ve-demo-content {
border: 1px solid gray;
width: 100%;
min-height: 2em;
max-height: 15%;
overflow-y: auto;
}
.ve-demo-log {
margin-top: 1em;
border: 1px solid gray;
width: 100%;
height: 80%;
white-space: pre;
font-family: monospace, monospace; /* Support: Blink, Gecko, Webkit, see T176636 */
padding: 0;
overflow-y: auto;
}
</style>
<link rel=stylesheet href="../../lib/oojs-ui/oojs-ui-apex.css" class="stylesheet-ltr stylesheet-read">
</head>
<body>
<form class="ve-demo-form">
IME identifier (name and version):
<input type="text" class="ve-demo-identifier">
<input type="submit" value="Start Logging Keyboard Events">
</form>
<div class="ve-demo-content" contenteditable="true"></div>
<textarea class="ve-demo-log"></textarea>
<script src="../../lib/jquery/jquery.js"></script>
<script src="../../lib/oojs/oojs.js"></script>
<script src="../../lib/oojs-ui/oojs-ui-core.js"></script>
<script src="../../lib/unicodejs/unicodejs.js"></script>
<script src="../../src/ve.js"></script>
<script src="../../src/ve.utils.js"></script>
<script src="../../src/ve.EventSequencer.js"></script>
<script>
( function () {
/* eslint-disable no-jquery/no-global-selector */
const sel = document.getSelection(),
$form = $( '.ve-demo-form' ),
$content = $( '.ve-demo-content' ).addClass( 'oo-ui-element-hidden' ),
content = $content[ 0 ],
$log = $( '.ve-demo-log' ).val( '' ),
$imeIdentifier = $( '.ve-demo-identifier' ),
/* eslint-enable no-jquery/no-global-selector */
log = [];
let comments,
startTime,
oldHtml = '',
oldAnchorPosition = null,
oldFocusPosition = null;
function getPosition( node, offset ) {
if ( node === null ) {
return null;
}
const position = [ offset ];
while ( node.parentNode !== null && node !== content ) {
position.splice(
0,
0,
Array.prototype.indexOf.call(
node.parentNode.childNodes,
node
)
);
node = node.parentNode;
}
return position;
}
function eventToObject( e ) {
// TODO: flesh this out more
if ( !e || e.keyCode === undefined ) {
return {};
} else {
return { keyCode: e.keyCode };
}
}
function showLog() {
const jsons = [];
jsons.push( JSON.stringify( comments ) );
for ( let i = 0, len = log.length; i < len; i++ ) {
jsons.push( JSON.stringify( log[ i ] ) );
}
$log.val( '[\n\t' + jsons.join( ',\n\t' ) + '\n]' );
$log[ 0 ].scrollTop = $log[ 0 ].scrollHeight;
}
function logWrite( val ) {
log.push( val );
showLog();
}
function logEvent( methodName, eventName, e ) {
const args = [],
msUtc = ( Date.now() - startTime ) / 1000,
newHtml = $content.html(),
newAnchorPosition = getPosition( sel.anchorNode, sel.anchorOffset ),
newFocusPosition = getPosition( sel.focusNode, sel.focusOffset );
if ( oldHtml !== newHtml ) {
logWrite( {
seq: log.length,
time: msUtc,
action: 'changeText',
args: [ newHtml ]
} );
oldHtml = newHtml;
}
if (
JSON.stringify( oldAnchorPosition ) !== JSON.stringify( newAnchorPosition ) ||
JSON.stringify( oldFocusPosition ) !== JSON.stringify( newFocusPosition )
) {
logWrite( {
seq: log.length,
time: msUtc,
action: 'changeSel',
args: [
newAnchorPosition,
newFocusPosition
]
} );
oldAnchorPosition = newAnchorPosition;
oldFocusPosition = newFocusPosition;
}
if ( eventName !== undefined ) {
args.push( eventName );
args.push( eventToObject( e ) );
}
logWrite( {
seq: log.length,
time: msUtc,
action: methodName,
args: args
} );
}
function makeEventHandler( eventName ) {
return function ( e ) {
logEvent( 'sendEvent', eventName, e );
};
}
function makeAfterLoopHandler() {
return function () {
logEvent( 'endLoop' );
};
}
/** @param {jQuery.Event} e Halo form submit event */
function start( e ) {
e.preventDefault();
comments = {
imeIdentifier: $imeIdentifier.val(),
userAgent: navigator.userAgent,
startDom: $content.html()
};
$form.addClass( 'oo-ui-element-hidden' );
$content.removeClass( 'oo-ui-element-hidden' ).trigger( 'focus' );
}
( function () {
const handlers = {},
eventNames = [
'compositionstart',
'compositionend',
'keydown',
'keyup',
'keypress',
'beforeinput',
'input'
];
for ( let i = 0, len = eventNames.length; i < len; i++ ) {
handlers[ eventNames[ i ] ] =
makeEventHandler( eventNames[ i ] );
}
// eslint-disable no-new
new ve.EventSequencer( eventNames )
.attach( $content )
.on( handlers )
.afterLoop( makeAfterLoopHandler() );
$( document ).on( 'selectstart', makeEventHandler( 'selectstart' ) );
$( document ).on( 'selectionchange', makeEventHandler( 'selectionchange' ) );
startTime = Date.now();
$form.on( 'submit', start );
$log.on( 'click', function () {
if ( this.select ) {
this.select();
}
} );
}() );
}() );
</script>
</body>
</html>