-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
436 lines (382 loc) · 12.8 KB
/
script.js
File metadata and controls
436 lines (382 loc) · 12.8 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
// const timeElement = document.getElementById("time");
const timeElement = document.getElementById("time-options");
const capslockElement = document.getElementById("capslock");
// const mobileKeyboard = document.getElementById('mobile-keyboard')
//const dataPara = document.getElementById("data-para");
// Getting datatype
const dataTypeElement = document.getElementById("dataType-options");
let dataTypeValue = dataTypeElement.value;
// Loading Details from Local Stoage
if (
localStorage.length > 1 &&
localStorage.getItem("arrayType") &&
localStorage.getItem("time")
) {
dataTypeElement.value = localStorage.getItem("arrayType");
timeElement.value = localStorage.getItem("time");
dataTypeValue = localStorage.getItem("arrayType");
} else {
localStorage.setItem("arrayType", "words");
localStorage.setItem("time", 3);
}
const dataTypeChanged = () => {
localStorage.setItem("arrayType", dataTypeElement.value);
pageReload();
};
const timeChanged = () => {
localStorage.setItem("time", timeElement.value);
};
dataTypeElement.addEventListener("change", dataTypeChanged);
timeElement.addEventListener("change", timeChanged);
// dataTypeElement.value = localStorage.getItem("arrayType");
// timeElement.value = localStorage.getItem("time");
// dataTypeValue = localStorage.getItem("arrayType");
// For random dataTypeElement choosing random datatype
let dataTypes = ["words", "paragraph", "lorem"];
let random = dataTypes[Math.floor(Math.random(4) * 3)];
let array = "words";
function getArray(arrayType) {
switch (arrayType) {
case "words":
array = words;
break;
case "paragraph":
array = paragraph;
break;
case "lorem":
array = lorem;
break;
case "random":
array = window[random];
break;
default:
array = words;
break;
}
return array;
}
array = getArray(dataTypeValue);
// Suffling wordlist
function suffleWordlist(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * i);
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
array = suffleWordlist(array);
//dataPara.innerText = array.join(' ')
const data = array.join(" ").trim().replace("<br>", " ");
function createTable(table, data) {
function createNode(ele, cls, text = null, id = null) {
let element = document.createElement(ele);
element.classList.add(cls);
if (text) {
element.innerText = text;
}
if (id) {
element.id = id;
}
return element;
}
function createWord(word) {
// Word Element
let screenBasicWord = createNode("div", "screenBasic-word");
let y = 0;
// For letter in list create element and add into screenBasicWord
while (y < word.length) {
// Letter Element
let screenBasicLetter = createNode(
"div",
"screenBasic-letter",
word[y],
y
);
// Adding Space at end of word
screenBasicWord.appendChild(screenBasicLetter.cloneNode(true));
y++;
}
// Adding SpaceBar to element
let screenBasicLetter = createNode("div", "screenBasic-letter", "_", y);
screenBasicWord.appendChild(screenBasicLetter.cloneNode(true));
table.append(screenBasicWord);
table.childNodes[0].childNodes[0].classList.add("is-current");
}
// Converting Words into word
function createWords(words) {
console.log(words);
words.map((word) => {
if (word != "") createWord(word.trim());
});
// createWord(words)
}
// Converting Data into Array
function convertDataIntoArray(data) {
return data.trim().split(" ");
}
let dataArray = convertDataIntoArray(data);
createWords(dataArray);
}
const table = document.getElementById("table");
createTable(table, data);
function getCurrentNode(table, currentWord, currentLetter) {
return table.childNodes[currentWord].childNodes[currentLetter];
}
function scrollAll(element) {
// Getting value of translateY
function getValue(str) {
let value = str
.replace("translateY", "")
.replace("(", "")
.replace(")", "")
.replace("px", "");
return value;
}
let previousTranlateValue = element.style.transform;
if (previousTranlateValue == "") {
element.style.transform = "translateY(-58px)";
} else {
let value = getValue(previousTranlateValue);
element.style.transform = `translateY(${value - 58}px)`;
}
console.log(element.style.transform);
}
function checkAndScrollHeightCurrent(element) {
let currentElementScrollY = element.parentElement.getBoundingClientRect().y;
let previous = element.parentElement.previousSibling;
// console.log('previous', previous, element)
let previousElementScrollY = previous.getBoundingClientRect().y;
// console.log(currentElementScrollY, previousElementScrollY)
let sameHeight =
Math.floor(currentElementScrollY) == Math.floor(previousElementScrollY);
// if ()
if (!sameHeight) {
console.log("not-same");
scrollAll(element.parentElement.parentElement);
}
}
function getWpm(correct, wrong, minutes) {
let grossWpm = (correct + wrong) / 5 / minutes;
return grossWpm;
// Commeting net value cause want to use only gross wpm
// let netWpm = grossWpm - (wrong / minutes)
// return netWpm
}
// For capslock check
function checkCapslock(event) {
if (event.getModifierState("CapsLock")) {
capslockElement.style.display = "block";
// console.log('on')
} else {
// console.log('off')
capslockElement.style.display = "none";
}
console.log("pressed");
}
function startGame(event) {
// Check if pressed key is space or not
function checkifspace(event, currentText) {
if (currentText == " ") {
if (event.key == "_" || event.keyCode == 13) {
return true;
}
}
return false;
}
// Stopping space-bar scroll
event.preventDefault();
// Check capslock
checkCapslock(event);
let currentNodeElement = getCurrentNode(table, currentNode, currentLetter);
currentText = currentNodeElement.innerText;
currentNodeElement.classList.add("is-current");
if (
currentText == ("" || "_" || " " || "<br>" || "/\n/") ||
currentText.charCodeAt(0) == 10
) {
currentText = " ";
event.preventDefault();
}
if (event.key == currentText || checkifspace(event, currentText)) {
currentNodeElement.classList.remove("is-current");
if (!currentNodeElement.classList.contains("is-wrong")) {
currentNodeElement.classList.add("is-correct");
}
// For Timeup and timechange
if (first) {
first = false;
let minutes = 1;
// Setting minutes to option value
// minutes = document.getElementById("time-options").value;
minutes = timeElement.value;
time.innerText = minutes * 60;
let timeInterval = setInterval(() => (time.innerText -= 1), 1000);
setTimeout(() => {
// alert("timeOut");
let correctWordNumber = document.querySelectorAll(".is-right").length;
let correctNumber = document.querySelectorAll(".is-correct").length;
let wrongNumber = document.querySelectorAll(".is-wrong").length;
// Net commented out cause only want to use grossWpm
let grossWpm = getWpm(correctNumber, wrongNumber, minutes);
// let netWpm = getWpm(correctNumber, wrongNumber, minutes)
document.getElementById("text-inner").innerText = `
Correct : ${correctNumber}
Wrong : ${wrongNumber}
WPM : ${Math.round(grossWpm)}wpm
Accuracy : ${Math.round(
100 - (correctNumber + wrongNumber) / correctNumber
)}%
`;
document.getElementById("overlay").style.display = "block";
// Removing Game
clearInterval(timeInterval);
document.removeEventListener("keypress", startGame);
}, minutes * 60 * 1000);
}
// Calling next Word
if (
currentNodeElement.id ==
currentNodeElement.parentElement.childElementCount - 1
) {
currentNode++;
currentLetter = 0;
currentNodeElement.parentElement.classList.add("is-right");
// Checking if have more words ( current word space )
if (
currentNode !=
currentNodeElement.parentElement.parentElement.childElementCount
) {
let current = getCurrentNode(table, currentNode, currentLetter);
current.classList.add("is-current");
checkAndScrollHeightCurrent(current);
} else {
// When completed para
console.log("Done");
// document.removeEventListener("keypress", startGame);
}
} else {
currentNodeElement.nextElementSibling.classList.add("is-current");
currentLetter++;
}
} else {
currentNodeElement.classList.add("is-wrong");
}
}
function startGameMobile(event) {
// console.log(event.target.value)
let value = event.target.value;
// Check if pressed key is space or not
function checkifspace(event, currentText) {
if (currentText == " ") {
if (value[value.length - 1] == "_" || event.keyCode == 13) {
return true;
}
}
return false;
}
// Stopping space-bar scroll
event.preventDefault();
// Check capslock
// checkCapslock(event)
let currentNodeElement = getCurrentNode(table, currentNode, currentLetter);
currentText = currentNodeElement.innerText;
currentNodeElement.classList.add("is-current");
if (
currentText == ("" || "_" || " " || "<br>" || "/\n/") ||
currentText.charCodeAt(0) == 10
) {
currentText = " ";
event.preventDefault();
}
if (
value[value.length - 1] == currentText ||
checkifspace(event, currentText)
) {
currentNodeElement.classList.remove("is-current");
if (!currentNodeElement.classList.contains("is-wrong")) {
currentNodeElement.classList.add("is-correct");
}
// For Timeup and timechange
if (first) {
first = false;
let minutes = 1;
// Setting minutes to option value
minutes = document.getElementById("time-options").value;
time.innerText = minutes * 60;
let timeInterval = setInterval(() => (time.innerText -= 1), 1000);
setTimeout(() => {
// alert("timeOut");
let correctWordNumber = document.querySelectorAll(".is-right").length;
let correctNumber = document.querySelectorAll(".is-correct").length;
let wrongNumber = document.querySelectorAll(".is-wrong").length;
// Net commented out cause only want to use grossWpm
let grossWpm = getWpm(correctNumber, wrongNumber, minutes);
// let netWpm = getWpm(correctNumber, wrongNumber, minutes)
document.getElementById("text-inner").innerText = `
Correct : ${correctNumber}
Wrong : ${wrongNumber}
WPM : ${Math.round(grossWpm)}wpm
Accuracy : ${Math.round(
100 - (correctNumber + wrongNumber) / correctNumber
)}%
`;
document.getElementById("overlay").style.display = "block";
// Removing Game
clearInterval(timeInterval);
document.removeEventListener("keypress", startGame);
}, minutes * 60 * 1000);
}
// Calling next Word
if (
currentNodeElement.id ==
currentNodeElement.parentElement.childElementCount - 1
) {
currentNode++;
currentLetter = 0;
currentNodeElement.parentElement.classList.add("is-right");
// Checking if have more words ( current word space )
if (
currentNode !=
currentNodeElement.parentElement.parentElement.childElementCount
) {
let current = getCurrentNode(table, currentNode, currentLetter);
current.classList.add("is-current");
checkAndScrollHeightCurrent(current);
} else {
// When completed para
console.log("Done");
// document.removeEventListener("keypress", startGame);
}
} else {
currentNodeElement.nextElementSibling.classList.add("is-current");
currentLetter++;
}
} else {
currentNodeElement.classList.add("is-wrong");
}
// event.target.value = ""
}
let currentNode = 0;
let currentLetter = 0;
let keypressed = 0;
let first = true;
let keypressEvent = document.addEventListener("keypress", startGame, false);
let mobileKeyboardEvent = document.addEventListener(
"input",
startGameMobile,
false
);
// console.log(table.childNodes[0].childNodes[0].innerText)
// Adding Caches
// Checking if service worker supported by browser for caching site
if ("serviceWorker" in navigator) {
// console.log('Service Worker Support');
window.addEventListener("load", () => {
navigator.serviceWorker
.register("sw_cached_pages.js") // Create any file name and create file with that name
.then((reg) => console.log("Service Worker: Registered"))
.catch((err) => console.log(`Service Worker: Error: ${err}`));
});
}