-
Notifications
You must be signed in to change notification settings - Fork 5
/
7speaking.user.js
336 lines (251 loc) · 8.26 KB
/
7speaking.user.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
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
// ==UserScript==
// @name 7Speaking Bot
// @namespace https://github.com/quantumsheep
// @version 7.0
// @description 7Speaking is kil
// @author quantumsheep
// @match https://user.7speaking.com/*
// @grant none
// ==/UserScript==
(async () => {
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function isPath(regex) {
return regex.test(location.pathname);
}
function error(message) {
alert(message);
throw new Error(message);
}
async function waitForQuerySelector(selector) {
console.log(`Waiting for querySelector('${selector}')`)
return new Promise(resolve => {
const interval = setInterval(() => {
const e = document.querySelector(selector);
if (e) {
clearInterval(interval);
resolve(e);
}
}, 1000);
});
}
function getReactElement(e) {
for (const key in e) {
if (key.startsWith('__reactInternalInstance$')) {
return e[key];
}
}
return null;
}
async function completeQuiz() {
async function findAnswer() {
const e = await waitForQuerySelector('.question-container');
let container = getReactElement(e);
while (container) {
if (container.memoizedProps) {
return container.memoizedProps.children[5].props.children[0].props.children.props.answerOptions.answer[0].value;
}
container = container.return;
}
return null;
}
function getInputElement(answer) {
const e = document.querySelector('.question__form input');
if (e) {
return {
element: getReactElement(e),
type: 'input'
};
}
const buttons = document.querySelectorAll('.answer-container button');
for (const button of buttons) {
if (button.querySelector('.question__customLabel').innerText.trim() === answer.trim()) {
return {
element: button,
type: 'button'
};
}
}
return null;
}
function getSubmitButton() {
const e = document.querySelector('.question__form button[type=submit]');
return e;
}
console.log('Searching for the answer...');
const answer = await findAnswer();
if (answer === null || answer === undefined) {
return error("Can't find answer");
}
console.log(`Answer is "${answer}"`);
const input = getInputElement(answer);
if (!input) {
return error("Can't find input");
}
console.log(`Question type is "${input.type}"`);
if (input.type === 'input') {
input.element.memoizedProps.onChange({
currentTarget: {
value: answer
}
});
} else if (input.type === 'button') {
input.element.click();
}
await wait(200);
const button = getSubmitButton();
if (!button) {
return error("Can't find submit button");
}
console.log(`Clicking "Validate" button`);
button.click();
await wait(500);
console.log(`Clicking "Next" button`);
button.click();
await wait(500);
}
async function completeExam() {
async function findAnswer() {
const e = await waitForQuerySelector('.question_content');
let container = getReactElement(e);
while (container) {
if (container.memoizedProps && container.memoizedProps.questions) {
const [question] = container.memoizedProps.questions;
if (question.needorder) {
const options = {};
for (const k in question.answer) {
options[k] = question.answer[k].sort((a, b) => a - b);
}
return options;
}
return question.answer;
}
container = container.return;
}
return null;
}
const answer = await findAnswer();
if (answer === null || answer === undefined) {
const submitButton = document.querySelector('.buttons_container button:last-child');
if (!submitButton) {
return error("Can't find answer");
} else {
submitButton.click();
await wait(1000);
}
} else {
if (typeof answer === 'object') {
const optionsAreTypeof = (type) => Object.values(answer).every(options => options.every(option => typeof option === type))
if (optionsAreTypeof('boolean')) {
console.log(`Options are booleans`);
const lines = [...document.querySelectorAll('.question_variant tbody tr')];
for (const i in lines) {
const inputs = lines[i].querySelectorAll('td input');
for (const j in answer) {
const input = inputs[+j - 1];
if (answer[j][i]) {
input.click();
}
}
}
} else if (optionsAreTypeof('string') || optionsAreTypeof('number')) {
console.log(`Options are strings/numbers`);
const columns = [...document.querySelectorAll('.question_variant tbody tr td')];
for (const i in answer) {
const inputs = columns[+i - 1].querySelectorAll('input');
for (const j in answer[i]) {
const input = getReactElement(inputs[j]);
input.memoizedProps.onChange({
target: {
value: answer[i][j].toString(),
},
});
}
}
} else {
return error(`Can't understand this type of options`);
}
await wait(1000);
} else {
const inputs = document.querySelectorAll('.question_variant label');
if (isNaN(answer)) {
const options = answer.split(',');
for (const option of options) {
inputs[option.charCodeAt(0) - 'A'.charCodeAt(0)].click();
}
} else {
inputs[+answer - 1].click();
}
}
const submitButton = await waitForQuerySelector('.buttons_container button:last-child');
submitButton.click();
await wait(1000);
submitButton.click();
await wait(1000);
}
}
async function routes() {
console.log(`Analysing current route`);
if (isPath(/^\/home/)) {
console.log(`Current route is /home`);
console.log(`Selecting the first content...`);
const e = await waitForQuerySelector('.scrollableList .scrollableList__content .MuiButtonBase-root');
e.click();
routes();
} else if (isPath(/^\/workshop\/exams-tests/)) {
const search = new URLSearchParams(location.search);
if (search.has('id')) {
await completeExam();
routes();
} else {
const nextExam = await waitForQuerySelector('.lists .list__items.active');
nextExam.click();
await wait(300);
const modalConfirmButton = document.querySelector('.confirmCloseDialog__buttons button:last-child');
if (modalConfirmButton) {
modalConfirmButton.click();
}
await wait(1000);
routes();
}
} else if (isPath(/^\/workshop/)) {
console.log(`Current route is /workshop`);
await waitForQuerySelector('.banner');
const buttons = document.querySelectorAll('.bottom-pagination .pagination button');
if (buttons.length > 0) {
buttons[buttons.length - 1].click();
}
let quizButton = document.querySelector('.category-action-bottom button');
if (!quizButton) {
quizButton = document.querySelector('button.cardMode__goToQuiz:not(.finalCard__btns button)');
}
if (!quizButton) {
console.log("Can't find quiz button, returning to /home");
location.href = '/home';
throw new Error();
}
quizButton.click();
routes();
} else if (isPath(/^\/document\/\d+/)) {
console.log(`Current route is /document`);
const e = await waitForQuerySelector('.appBarTabs__testTab');
e.click();
routes();
} else if (isPath(/^\/quiz/)) {
console.log(`Current route is /quiz`);
await waitForQuerySelector('.quiz__container');
if (document.querySelector('.result-container')) {
location.href = '/home';
} else {
await completeQuiz();
routes();
}
}
}
if (document.readyState === 'complete') {
routes();
} else {
window.addEventListener('load', async () => {
routes();
});
}
})();