-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezfluent.js
350 lines (287 loc) · 11.1 KB
/
ezfluent.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
console.log('Starting ezfluent ...');
var articles = [];
var quiz;
// Message listener
browser.runtime.onMessage.addListener((msg) => {
msg = JSON.parse(msg);
console.log('got network data')
const data =JSON.parse(msg.data)
if (msg.type == 'quiz') initQuiz(data);
if (msg.type == 'articles') initArticles(data);
});
function initBanner() {
const div = document.createElement('div');
div.innerHTML = `
<button id="GF_show_btn">SHOW</button>
<button id="GF_auto_btn">AUTO QUIZ</button>
<button id="GF_full_auto_btn">FULL AUTO</button>
<button id="GF_full_auto_retake_btn">FULL AUTO AND RETAKE</button>`;
div.style = "position: absolute; top: 0; left: 50%;"
document.body.appendChild(div);
document.getElementById('GF_show_btn').addEventListener('click', inputAns);
document.getElementById('GF_auto_btn').addEventListener('click', auto_quiz);
document.getElementById('GF_full_auto_btn').addEventListener('click', auto_articles);
document.getElementById('GF_full_auto_retake_btn').addEventListener('click', auto_articles_retake);
}
function initArticles(data) {
// console.log(data);
function addArticle(groupId, articleId, type) {
function isIn(articleId) {
for (let i = 0; i < articles.length; i++) {
if (articles[i].articleId == articleId) return true;
}
return false;
}
if (isIn(articleId)) return;
articles.push({
articleId,
groupId,
type,
done: false,
})
}
const previousArticlesLenght = articles.length
data.forEach((d) => {
addArticle(d.groupId, d.articleId, d.type);
})
console.log(`Got ${articles.length - previousArticlesLenght} new articles`)
// document.querySelectorAll("div[href='/app/dashboard/training-path/321037076/howto/81862/88711']");
}
function auto_articles_retake(e) {
auto_articles(e, true)
}
async function auto_articles(e, retake) {
const do_article_result = await do_article(retake)
if (do_article_result) {
console.log('1 article done !')
auto_articles(e, retake);
}
}
async function do_article(retake) {
console.log('auto_articles')
console.log(articles)
let article;
let article_index = 0;
for (let i = 0; i < articles.length; i++) {
if (!articles[i].done) {
article = articles[i];
article_index = i;
break;
}
}
if(!article) {
console.log('No undone article found')
return 0
}
console.log(`Doing article ${article.groupId}/${article.articleId}`)
const article_div = document.querySelectorAll(`div[href='/app/dashboard/training-path/321037076/${article.type.replace('-', '')}/${article.groupId}/${article.articleId}']`)[0];
if (!article_div) {
console.error('Unable to find article div')
return 0
}
article_div.scrollIntoView()
article_div.click()
quiz = null; // Reset quiz
let wait_time = 0
let quiz_btn;
while (wait_time <= 300) {
quiz_btn = document.getElementById('practice');
if (quiz_btn) break
wait_time += 1
await delay(100);
}
if (!quiz_btn) {
console.error('Article loading timeout')
}
console.log('Article loaded')
quiz_btn.click()
wait_time = 0
while (wait_time <= 300) {
if (quiz) break
wait_time += 1
await delay(100);
}
if (!quiz) {
console.error('Quiz loading timeout')
}
console.log('Quiz loaded, launching auto quiz')
await auto_quiz(null, retake);
console.log('Auto quiz completed')
articles[article_index].done = true;
return 1
}
function initQuiz(data) {
function questionDecode(q) {
let title = q.q;
let type = q.qt;
let options;
if (q.o3) options = [q.o1, q.o2, ... q.o3];
else options = [q.o1, q.o2];
let answers = q.ans;
if (q.qt != 'sl') {
if (!Array.isArray(q.ans)) {
answers = q.ans.split(',');
}
}
return {title, type, answers, options, optionType: q.ot};
}
console.log('Initializing new quiz...');
const questionList = [];
data.q.forEach((q) => {
questionList.push(questionDecode(q));
});
quiz = {questionList};
console.log('New quiz initialized');
}
async function auto_quiz(e, retake) {
if (document.getElementsByClassName('QuizResults__retake')[0]) {
if (retake) {
document.getElementsByClassName('QuizResults__retake')[0].click()
console.log('Retaking quiz')
} else {
console.log('Quiz already done')
return;
}
}
for (let i = 0; i < quiz.questionList.length; i++) {
inputAns(e, true);
await delay(500 + Math.random(2)*500);
// await delay(1000 + Math.random(4)*1000);
if (document.getElementsByClassName('Question__submit')) document.getElementsByClassName('Question__submit')[0].click()
if (document.getElementsByClassName('Question__next')) document.getElementsByClassName('Question__next')[0].click()
}
}
function inputAns(_, autoClick) {
function getQuestion() {
function htmlDecode(input) {
const doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
const qWrapper = document.getElementsByClassName('Question')[0];
const qTitleWrapper = qWrapper.getElementsByClassName('Stem__answer-block')[0];
const qTitle = htmlDecode(qTitleWrapper.innerHTML.replace(/<\/?[^>]+(>|$)/g, "").trim());
console.log(`Getting question data for question : "${qTitle}"`);
let i;
for (i = 0; i < quiz.questionList.length; i++) {
const question = quiz.questionList[i];
// console.log(questionList)
// console.log(htmlDecode(q.title.replace(/<\/?[^>]+(>|$)/g, "").replace(/[\r\n]/gm, '')))
if (htmlDecode(question.title.replace(/<\/?[^>]+(>|$)/g, "").replace(/[\r\n]/gm, '')) == qTitle) {
break;
}
}
return {question : quiz.questionList[i], qWrapper};
}
function showCorrectOption(optionNode, i, text) {
optionNode.textContent = '';
const index = document.createElement('strong');
index.textContent = i;
index.style.color = 'green';
optionNode.appendChild(index);
const optionText = document.createElement('span');
optionText.textContent = ' ' + text;
optionNode.appendChild(optionText);
}
const {question, qWrapper} = getQuestion();
console.info(question);
// TYPE : fill-in-the-gaps
if (question.type == 'fg') {
if (question.optionType == 'oT') { // output-text
for (let j = 0; j < qWrapper.querySelectorAll('input.Stem__answer_non-arabic').length; j++) {
const input = qWrapper.querySelectorAll('input.Stem__answer_non-arabic')[j];
input.value = question.answers[j].ans[0];
reactTriggerChange(input);
}
} else if (question.optionType == 'oTB') { // output-text-button
const optionsWrapper = qWrapper.getElementsByClassName('Question__fill-button');
for (let j = 0; j < question.answers.length; j++) {
const ans = question.answers[j];
const ansText = question.options[ans-1];
for (let k = 0; k < optionsWrapper.length; k++) {
const option = optionsWrapper[k];
if (option.innerText == ansText) {
showCorrectOption(option, j+1, ansText)
option.id = 'GF_click-'+j;
if (autoClick) option.click();
}
}
}
} else {
console.error(`Unkown option type : ${question.optionType}`);
}
// TYPE : multi-choice
} else if (question.type == 'mc') {
const optionsWrapper = qWrapper.getElementsByClassName('Question__options')[0];
optionsWrapper.childNodes.forEach(option => {
question.answers.forEach((ans) => {
const ansText = question.options[ans-1];
if (option.innerText == ansText) {
option.style.border = '2px solid green';
option.id = 'GF_click';
if (autoClick) option.click();
}
});
});
// TYPE : scrambled sentence
} else if (question.type == 'ss') {
const optionsWrapper = qWrapper.getElementsByClassName('Question_type_scrambled-sentence__unselected-box')[0];
for (let j = 0; j < question.answers.length; j++) {
const ans = question.answers[j];
const ansText = question.options[ans-1];
optionsWrapper.childNodes.forEach((option) => {
if (option.innerText == ansText) {
showCorrectOption(option, j+1, ansText)
option.id = 'GF_click-'+j;
if (autoClick) option.click();
return;
}
});
}
// TYPE : scrambled letters
} else if (question.type == 'sl') {
const optionsWrapper = qWrapper.getElementsByClassName('Question_type_scrambled-letters__unselected-box')[0];
for (let j = 0; j < question.answers.length; j++) {
const ans = question.answers[j];
for (let k = 0; k < optionsWrapper.childNodes.length; k++) {
const option = optionsWrapper.childNodes[k];
if (option.innerText == ans) {
showCorrectOption(option, j+1, ansText)
option.id = 'GF_click-'+j;
if (autoClick) option.click();
break;
}
}
}
// TYPE : mt
} else if (question.type == 'mt' || question.type == 'tf') {
const optionsWrapper = qWrapper.getElementsByClassName('Question__options')[0];
for (let j = 0; j < question.answers.length; j++) {
const ans = question.answers[j];
const ansText = question.options[ans-1];
optionsWrapper.childNodes.forEach((option) => {
if (option.innerText == ansText) {
showCorrectOption(option, j+1, ansText)
option.id = 'GF_click-'+j;
if (autoClick) option.click();
return;
}
});
}
// TYPE : short answer
} else if (question.type == 'sa') {
const input = document.querySelectorAll('textarea.Stem__answer_non-arabic')[0];
input.value = question.answers[0];
reactTriggerChange(input);
} else {
console.error(`Unkown question type : ${question.type}`);
}
}
initBanner();
// Auto idle click
setInterval(() => {
if (document.getElementsByClassName('idle-screen__continue')[0]) {
console.log('Idle click');
document.getElementsByClassName('idle-screen__continue')[0].click();
}
}, 5000);
const delay = (time) => new Promise(resolve => setTimeout(resolve, time));