-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop-with-pause.js
35 lines (31 loc) · 1 KB
/
stop-with-pause.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
// Focusing the typing area
document.getElementById("wordsWrapper")?.click();
const DELAY = 100;
// User needs to enter the space button for the automatic typing to start
// And also user needs to type the space button again when the last active word typed completely
document.addEventListener("keyup", (e) => {
if (e.key == " ") {
const ele = document.querySelector(".word.active");
if (ele) {
const content = ele.textContent;
typeActiveWord(content);
}
}
});
// It will pause the execution for the given time
const pause = async (time) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, time);
});
};
// It will type the active word programmatically with some delay
const typeActiveWord = async (word, i = 0) => {
const length = word.length;
if (i == length) return;
const randomNumber = Math.floor(Math.random() * DELAY);
await pause(randomNumber);
document.execCommand("insertText", false, word[i]);
await typeActiveWord(word, i + 1);
};