-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnitrotype.js
99 lines (75 loc) · 2.38 KB
/
nitrotype.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
98
99
const puppeteer = require("puppeteer");
const BASE_URL = "http://nitrotype.com/race";
let actualWord = [];
const nitrotype = {
browser: null,
page: null,
initialize: async () => {
nitrotype.browser = await puppeteer.launch({
headless: false
});
nitrotype.page = await nitrotype.browser.newPage();
await nitrotype.page.goto(BASE_URL, { waitUntil: "networkidle2" });
},
login: async (login, password) => {
const sigUpButton = await nitrotype.page.$("a.header-login");
await sigUpButton.click();
await nitrotype.page.waitFor(3000);
await nitrotype.page.type("input[name=username]", login, {
delay: 50
});
await nitrotype.page.type("input[name=password]", password, {
delay: 50
});
await nitrotype.page.waitFor(1000);
await nitrotype.page.click("button[type=submit]");
},
firstRace: async () => {
await nitrotype.page.waitFor(2000);
let startRaceButton = await nitrotype.page.$(
"div.profile-left div.profile-primaryActions a"
);
await startRaceButton.click();
await nitrotype.page.waitFor(2000);
},
play: async WPM => {
await nitrotype.page.waitFor(3000);
await nitrotype.page.waitForFunction(
'document.querySelector(".raceLight-status").innerText === "Type!"'
);
let letters = await nitrotype.page.$$("span.dash-word span.dash-letter");
for (let letter of letters) {
let char = await nitrotype.page.evaluate(
element => element.textContent,
letter
);
if (/\s/.test(char)) {
actualWord.push("Space");
} else actualWord.push(char);
}
for (let i = 0; i < actualWord.length - 1; i++) {
await nitrotype.page.keyboard.press(actualWord[i]);
await nitrotype.page.waitFor(WPM * 1.33333333);
}
actualWord = [];
},
nextRaces: async () => {
await nitrotype.page.waitFor(2000);
let levelUpButton = await nitrotype.page.$("button.levelup-close");
if (levelUpButton) {
await levelUpButton.click();
}
let achievementUnlockedButton = await nitrotype.page.$(
"button.modal-close"
);
if (achievementUnlockedButton) {
await achievementUnlockedButton.click();
}
await nitrotype.page.waitFor(1000);
let startRaceButton = await nitrotype.page.$(
"div.raceResults-footer button"
);
await startRaceButton.click();
}
};
module.exports = nitrotype;