-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript.js
More file actions
440 lines (365 loc) · 14.7 KB
/
script.js
File metadata and controls
440 lines (365 loc) · 14.7 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// ==UserScript==
// @name 10FF Live WPM
// @namespace https://github.com/wRadion/10FFLiveWPMScript
// @version 4.14
// @description Live WPM for 10FF tests
// @author wRadion
// @match *://10fastfingers.com/typing-test/*
// @match *://10fastfingers.com/widget*
// @match *://10fastfingers.com/competition/*
// @match *://10fastfingers.com/advanced-typing-test/*
// @grant none
// ==/UserScript==
/*******************
** CUSTOMISATION **
*******************/
const alignment = 'center'; // left | center | right
const speedVisible = true; // true | false
const keystrokesVisible = true; // true | false
const wordsVisible = true; // true | false
const scoreVisible = true; // true | false
/*******************/
const style = window.getComputedStyle(document.getElementById('words'), null);
const divStyle =
'font-size: 22px;' +
'line-height: 18px;' +
'margin-bottom: -1px;';
const commonStyle =
'background: ' + style.getPropertyValue('background-color') + ';' +
'border-radius: 4px 4px 0 0;' +
'border: ' + style.getPropertyValue('border') + ';' +
'margin: 0 4px;' +
'padding: 8px 12px 12px 12px;';
const smallStyle =
'text-align: center;' +
'font-size: 10px;' +
'margin-bottom: 6px;';
(function() {
'use strict';
const html =
'<div align="' + alignment + '" style="' + divStyle + '">' +
'<div style="' + commonStyle + 'display: ' + (speedVisible ? 'inline-block' : 'none') + ';"><div style="' + smallStyle + '">Speed</div><span id="live-wpm"></span> <strong>WPM</strong></div>' +
'<div style="' + commonStyle + 'display: ' + (keystrokesVisible ? 'inline-block' : 'none') + ';"><div style="' + smallStyle + '">Keystrokes</div><span id="live-kc" class="correct"></span> | <span id="live-kw" class="wrong"></span></div>' +
'<div style="' + commonStyle + 'display: ' + (wordsVisible ? 'inline-block' : 'none') + ';"><div style="' + smallStyle + '">Words</div><span id="live-wc" class="correct"></span> | <span id="live-ww" class="wrong"></span></div>' +
'<div style="' + commonStyle + 'display: ' + (scoreVisible ? 'inline-block' : 'none') + ';"><div style="' + smallStyle + '">Score</div><span id="live-raw"></span> <strong>WPM</strong></div>' +
'</div>';
let infoBar = document.createElement('div');
infoBar.innerHTML = html;
document.querySelector('#words').before(infoBar);
/* VARIABLES */
var language,
inter,
timer,
durationRatio,
startTime,
keystrokesCorrect,
keystrokesWrong,
wordsCorrect,
wordsWrong,
index;
/* SETUP */
const languageId = parseInt(document.querySelector('#speedtest-id').attributes.value.value);
language = [
null, 'english', 'german', 'french', 'portuguese', 'spanish', 'indonesian', 'turkish', 'vietnamese', 'polish', 'romanian', 'malaysian', 'norwegian', 'persian', 'hungarian', 'chinese_traditional', 'chinese_simplified',
'danish', 'dutch', 'swedish', 'italian', 'finnish', 'serbian', 'catalan', 'filipino', 'croatian', 'russian', 'arabic', 'bulgarian', 'japanese', 'albanian', 'korean', 'greek', 'czech', 'estonian', 'latvian', 'hebrew',
'urdu', 'galician', 'lithuanian', 'georgian', 'armenian', 'kurdish', 'azerbaijani', 'hindi', 'slovak', 'slovenian', null, 'icelandic', null, 'thai', 'pashto', 'esperanto', 'ukrainian', 'macedonian', 'malagasy', 'bengali'
][languageId];
/* FUNCTIONS */
function getDuration() {
const durParam = window.location.search.match(/dur=(\d+)/);
if (durParam) return parseInt(durParam[1]);
const baseTimer = document.getElementById("timer").innerText.split(':').map((i) => parseInt(i));
return baseTimer[0] * 60 + baseTimer[1];
}
function updateWpm(wpm) { document.getElementById("live-wpm").innerText = wpm; }
function updateKs(kc, kw) { document.getElementById("live-kc").innerText = kc; document.getElementById("live-kw").innerText = kw; }
function updateWs(wc, ww) { document.getElementById("live-wc").innerText = wc; document.getElementById("live-ww").innerText = ww; }
function updateRaw(raw) { document.getElementById("live-raw").innerText = raw; }
function getKeystrokes(word) {
var oneKeystroke = null;
var twoKeystrokes = null;
var threeKeystrokes = null;
var fourKeystrokes = null;
var fiveKeystrokes = null;
switch (language) {
/******************************
** KEYSTROKES CUSTOMISATION **
******************************/
case 'albanian':
oneKeystroke = /[a-zçë]/g;
twoKeystrokes = /[A-ZÇË]/g;
break;
case 'arabic':
oneKeystroke = /[ضصثقفغعهخحجدشسيبلاتنمكطئءؤرلاىةوزظلأ]/g;
twoKeystrokes = /[إ]/g;
break;
case 'armenian':
oneKeystroke = /[աբգդեևզէըթժիլխծկհձղճմյնշոչպջռսվտրցւփքօֆ]/g;
twoKeystrokes = /[ԷԿ]/g;
break;
case 'azerbaijani':
oneKeystroke = /[a-zçəğıöşü]/g;
twoKeystrokes = /[A-ZÇƏĞIİÖŞÜ]/g;
break;
case 'bulgarian':
oneKeystroke = /[абвгдежзийклмнопрстуфхцчшщъюя]/g;
twoKeystrokes = /[Б]/g;
break;
case 'catalan':
oneKeystroke = /[a-zç]/g;
twoKeystrokes = /[A-ZÇàèòéíóú]/g;
threeKeystrokes = /ÀÈÒÉÍÓÚïü/g;
fourKeystrokes = /[ÏÜ]/g;
break;
case 'croatian':
oneKeystroke = /[a-zćčđšž]/g;
twoKeystrokes = /[A-ZĆČĐŠŽ]/g;
break;
case 'czech':
oneKeystroke = /[a-záéěíščřúůýž]/g;
twoKeystrokes = /[A-Zó]/g;
threeKeystrokes = /[ďťňÁÉÍÓÚÝ]/g;
fourKeystrokes = /[ĚŠČŘŽŇŤĎŮ]/g;
break;
case 'danish':
oneKeystroke = /[a-zåæø]/g;
twoKeystrokes = /[A-ZÅÆØ]/g;
break;
case 'dutch':
oneKeystroke = /[a-z']/g;
twoKeystrokes = /[A-Zé]/g;
break;
case 'esperanto':
oneKeystroke = /[a-z]/g;
twoKeystrokes = /[A-Zĉĝĥĵŝŭ]/g;
break;
case 'estonian':
oneKeystroke = /[a-zõäöü]/g;
twoKeystrokes = /[A-ZÕÄÖÜ]/g;
break;
case 'finnish':
oneKeystroke = /[a-zäö]/g;
twoKeystrokes = /[A-ZÄÖ]/g;
break;
case 'french':
oneKeystroke = /[a-zàçéèù'-]/g;
twoKeystrokes = /[A-Zâêîôû]/g;
threeKeystrokes = /[ÂÊÎÔÛäëïüÿÄËÏÜ]/g;
fourKeystrokes = /[ÀÈÙÇÉ]/g;
fiveKeystrokes = /[ŒœŸ]/g;
break;
case 'galician':
oneKeystroke = /[a-zñ]/g;
twoKeystrokes = /[A-ZáéíóúÑ]/g;
threeKeystrokes = /[ÁÉÍÓÚ]/g;
break;
case 'german':
oneKeystroke = /[a-zäöüß]/g;
twoKeystrokes = /[A-ZÄÖÜ]/g;
break;
case 'greek':
oneKeystroke = /[αβγδεζηθικλμνξοπρσςτυφχψω]/g;
twoKeystrokes = /[άήίώόύέΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]/g;
threeKeystrokes = /[ϊϋΐΰΆΉΊΏΌΎΈ]/g;
fourKeystrokes = /[ΪΫ]/g;
break;
case 'hebrew':
oneKeystroke = /[אבגדהוזחטיכלמנסעפצקרשת]/g;
break;
case 'hungarian':
oneKeystroke = /[a-záéíóöőúüű]/g;
twoKeystrokes = /[A-ZÁÉÍÓÖŐÚÜŰ]/g;
break;
case 'icelandic':
oneKeystroke = /[a-zðþæö]/g;
twoKeystrokes = /[A-ZÐÞÆÖáéúíóý]/g;
threeKeystrokes = /[ÁÉÍÓÚÝ]/g;
break;
case 'italian':
oneKeystroke = /[a-zàèìòù]/g;
twoKeystrokes = /[A-Zçé]/g;
break;
case 'korean':
twoKeystrokes = /[가개거고교구그기나내너녀누느니다대더데도두디라래러로르리마머모무바버보부사새서세소수시아야어여오우이자저제주지처크타트퍼하후히]/g;
threeKeystrokes = /[각간갖같것결경계곳과국굴금길께날남년놓눈님돈동되뒤든듣들등따때또람런렇령론롭른만말먹명몇몸못문물뭐받방번법살상생선성센속손술습신실싶쓰씨안알앞얼엇역예왜용운위육음의인일작잘잡장전점정좀좋중직집찾책친통특학한함화회]/g;
fourKeystrokes = /[관떤떻많뿐삶앉않없원읽있활]/g;
break;
case 'latvian':
oneKeystroke = /[a-z]/g;
twoKeystrokes = /[A-Zāčēģīķļņšūž]/g;
threeKeystrokes = /[ĀČĒĢĪĶĻŅŠŪŽ]/g;
break;
case 'lithuanian':
oneKeystroke = /[a-ząčęėįšųūž]/g;
twoKeystrokes = /[A-ZĄČĘĖĮŠŲŪŽ]/g;
break;
case 'macedonian':
oneKeystroke = /[абвгдежзијклмнњопрстуфхцчш]/g;
twoKeystrokes = /[ЈМ]/g;
break;
case 'malagasy':
oneKeystroke = /[a-zà'-]/g;
twoKeystrokes = /[A-Z]/g;
break;
case 'norwegian':
oneKeystroke = /[a-zåæø]/g;
twoKeystrokes = /[A-ZÅÆØ]/g;
break;
case 'persian':
oneKeystroke = /[ا ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی]/g;
twoKeystrokes = /[ژ آ ء ّ َ ُ ِ]/g;
break;
case 'polish':
oneKeystroke = /[a-z]/g;
twoKeystrokes = /[A-Ząćęłńóśźż]/g; // Some should be one
threeKeystrokes = /[ĄĆĘŁŃÓŚŹŻ]/g; // Some should be two
break;
case 'portuguese':
oneKeystroke = /[a-zç]/g;
twoKeystrokes = /[A-ZÇáãéíóõú]/g;
threeKeystrokes = /[ÁÃÀÂÉÊÍÓÕÔÚàâêô]/g;
break;
case 'romanian':
oneKeystroke = /[a-z]/g;
twoKeystrokes = /[A-Zăâîșț]/g; // Should be one
break;
case 'russian':
oneKeystroke = /[явертыуиопшщэючасдфгхйклзьцжбнм\-]/g;
twoKeystrokes = /[ЮёЁъЪЧЯВЕРТЫУИОПШЩЭАСДФГХЙКЛЗЬЦЖБНМ]/g; // phonetic layout
break;
case 'serbian':
oneKeystroke = /[a-zćčđšž]/g;
twoKeystrokes = /[A-ZĆČĐŠŽ]/g;
break;
case 'slovak':
oneKeystroke = /[a-záäčéíľňôšťúýž]/g;
twoKeystrokes = /[A-Z]/g;
threeKeystrokes = /[ďĎÁČÉÍĽŇŠŤÚÝŽ]/g;
fourKeystrokes = /[ÄÔ]/g;
break;
case 'slovenian':
oneKeystroke = /[a-zčšž]/g;
twoKeystrokes = /[A-ZČŠŽ]/g;
break;
case 'spanish':
oneKeystroke = /[a-zñ]/g;
twoKeystrokes = /[A-ZáéíóúÑ]/g;
threeKeystrokes = /[ÁÉÍÓÚü]/g;
fourKeystrokes = /[Ü]/g;
break;
case 'swedish':
oneKeystroke = /[a-zåäö]/g;
twoKeystrokes = /[A-ZÅÄÖ]/g;
break;
case 'turkish':
oneKeystroke = /[a-zçğıöşü]/g;
twoKeystrokes = /[A-ZÇĞIİÖŞÜ]/g;
break;
case 'ukrainian':
oneKeystroke = /[абвгдеєжзиійклмнопрстуфхцчшщьюя]/g;
break;
case 'urdu':
oneKeystroke = /[ء آ ؤ ئ ث چ خ ذ ڈ ز ڑ ژ ض ظ گ ں ۂ ۃ ي ۓ]/g;
twoKeystrokes = /[ا ب پ ت ٹ ج ح د ر س ش ص ط ع غ ف ق ک ل م ن ھ ہ و ی ے]/g;
break;
case 'thai': // temporary fix, count all chars as 1 keystroke
oneKeystroke = /./g;
break;
case 'vietnamese':
oneKeystroke = /[a-z]/g;
twoKeystrokes = /[A-Zăâáàạảãđéèẻẽẹêíìỉĩịôơóòỏõọưúùủũụýỳỷỹỵ]/g;
threeKeystrokes = /[ắằặẳẵấầậẩẫếềểễệốồổỗộớờởỡợứừửữự]/g;
break;
default: // english, indonesian, malaysian, filipino
oneKeystroke = /[a-z'\-\.]/g;
twoKeystrokes = /[A-Z]/g;
break;
/******************************/
}
const one = (word.match(oneKeystroke) || []).length;
const two = (word.match(twoKeystrokes) || []).length * 2;
const three = (word.match(threeKeystrokes) || []).length * 3;
const four = (word.match(fourKeystrokes) || []).length * 4;
const five = (word.match(fiveKeystrokes) || []).length * 5;
let extra = 0;
if (language === "arabic") {
for (const c of word) {
if (c === "أ" || c === "آ") {
extra += 1;
}
if (c === "رّ" || c === "وّ") {
extra += 2;
}
}
}
return one + two + three + four + five + extra;
}
function reset() {
if (inter) clearInterval(inter);
inter = null;
timer = getDuration();
durationRatio = 60 / timer;
startTime = null;
keystrokesCorrect = 0;
keystrokesWrong = 0;
wordsCorrect = 0;
wordsWrong = 0;
index = 0;
updateWpm('?');
updateKs('?', '?');
updateWs('?', '?');
updateRaw('?');
document.getElementById("inputfield").focus();
}
function stop() {
if (inter) clearInterval(inter);
startTime = undefined;
}
function start() {
startTime = Date.now();
updateWpm(0);
updateKs(0, 0);
updateWs(0, 0);
updateRaw(0);
inter = setInterval(() => { if (--timer === 0) stop(); }, 1000);
}
/* EVENT HANDLERS */
// F5
document.onkeydown = function(e) { if (e.keyCode === 116) { reset(); } };
// Reload Button
document.getElementById("reload-btn").onclick = function(e) { reset(); };
// Apply Settings Button (Custom Test)
const applySettingsBtn = document.getElementById("apply-settings");
if (applySettingsBtn) {
applySettingsBtn.onclick = function(e) { reset(); };
}
document.getElementById("inputfield").oninput = function(e) {
if (startTime === null) start();
};
document.getElementById("inputfield").onkeyup = function(e) {
if (startTime === undefined) return;
if (document.getElementById('words').style.display === 'none') {
stop();
return;
}
if (e.keyCode === 32) {
const word = document.querySelectorAll(".correct[wordnr]")[index++];
if (word) {
keystrokesCorrect += getKeystrokes(word.innerText) + 1;
wordsCorrect += 1;
} else {
--index;
const wrongWords = document.querySelectorAll(".wrong[wordnr]");
keystrokesWrong += getKeystrokes(wrongWords[wrongWords.length - 1].innerText) + 1;
wordsWrong += 1;
}
const tmp = keystrokesCorrect / 5;
updateWpm(((tmp * 60 * 1000) / (Date.now() - startTime)).toFixed(2));
updateKs(keystrokesCorrect, keystrokesWrong);
updateWs(wordsCorrect, wordsWrong);
updateRaw((tmp * durationRatio).toFixed(2));
}
};
/* CODE */
reset();
})();