This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathuserscript.js
467 lines (433 loc) · 15 KB
/
userscript.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// ==UserScript==
// @name Duohacker
// @namespace https://www.duolingo.com/
// @homepageURL https://github.com/smintf/duohacker
// @supportURL https://github.com/smintf/duohacker/issues
// @version 1.0.5
// @description An autoanswer script for Duolingo.
// @author Smint <[email protected]>
// @copyright Smint
// @match https://www.duolingo.com/skill*
// @match https://www.duolingo.com/alphabet*
// @match https://www.duolingo.com/checkpoint*
// @match https://www.duolingo.com/stories*
// @match https://www.duolingo.com/practice*
// @license MIT
// @grant none
// @run-at document-end
// ==/UserScript==
// ==OpenUserJS==
// @author smintf
// ==/OpenUserJS==
const DEBUG = true;
let mainInterval;
const dataTestComponentClassName = "e4VJZ";
const TIME_OUT = 1000;
// Challenge types
const CHARACTER_SELECT_TYPE = "characterSelect";
const CHARACTER_MATCH_TYPE = "characterMatch";
const TRANSLATE_TYPE = "translate";
const LISTEN_TAP_TYPE = "listenTap";
const NAME_TYPE = "name";
const COMPLETE_REVERSE_TRANSLATION_TYPE = "completeReverseTranslation";
const LISTEN_TYPE = "listen";
const SELECT_TYPE = "select";
const JUDGE_TYPE = "judge";
const FORM_TYPE = "form";
const LISTEN_COMPREHENSION_TYPE = "listenComprehension";
const READ_COMPREHENSION_TYPE = "readComprehension";
const CHARACTER_INTRO_TYPE = "characterIntro";
const DIALOGUE_TYPE = "dialogue";
const SELECT_TRANSCRIPTION_TYPE = "selectTranscription";
const SPEAK_TYPE = "speak";
const SELECT_PRONUNCIATION_TYPE = "selectPronunciation";
const LISTEN_ISOLATION_TYPE = "listenIsolation";
// W.I.P
const ASSIST_TYPE = "assist";
const TAP_COMPLETE_TYPE = "tapComplete";
const GAP_FILL_TYPE = "gapFill";
const CHARACTER_TRACE_TYPE = "characterTrace";
const CHALLENGE_PUZZLE_TYPE = "characterPuzzle";
const DEFINITION_TYPE = "definition";
const MATCH_TYPE = "match";
const TAP_DESCRIBE_TYPE = "tapDescribe";
const FREE_RESPONSE_TYPE = "freeResponse";
// Query DOM keys
const CHALLENGE_CHOICE_CARD = '[data-test="challenge-choice-card"]';
const CHALLENGE_CHOICE = '[data-test="challenge-choice"]';
const CHALLENGE_TRANSLATE_INPUT = '[data-test="challenge-translate-input"]';
const CHALLENGE_LISTEN_TAP = '[data-test="challenge-listenTap"]';
const CHALLENGE_JUDGE_TEXT = '[data-test="challenge-judge-text"]';
const CHALLENGE_TEXT_INPUT = '[data-test="challenge-text-input"]';
const CHALLENGE_TAP_TOKEN = '[data-test="challenge-tap-token"]';
const CHALLENGE_TAP_TOKEN_TEXT = '[data-test="challenge-tap-token-text"]';
const PLAYER_NEXT = '[data-test="player-next"]';
const PLAYER_SKIP = '[data-test="player-skip"]';
const AUDIO_BUTTON = '[data-test="audio-button"]';
const WORD_BANK = '[data-test="word-bank"]';
const BLAME_INCORRECT = '[data-test="blame-incorrect"]';
const CHARACTER_MATCH = '[data-test="challenge challenge-characterMatch"]';
const STORIES_PLAYER_NEXT = '[data-test="stories-player-continue"]';
const STORIES_CHOICE = '[data-test="stories-choice"]';
const STORIES_ELEMENT = '[data-test="stories-element"]';
// Mouse Click
const clickEvent = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true,
});
// Gets Challenge Object
function getChallengeObj(theObject) {
let result = null;
if (theObject instanceof Array) {
for (let i = 0; i < theObject.length; i++) {
result = getChallengeObj(theObject[i]);
if (result) {
break;
}
}
} else {
for (let prop in theObject) {
if (prop == "challenge") {
if (typeof theObject[prop] == "object") {
return theObject;
}
}
if (
theObject[prop] instanceof Object ||
theObject[prop] instanceof Array
) {
result = getChallengeObj(theObject[prop]);
if (result) {
break;
}
}
}
}
return result;
}
// Gets the Challenge
function getChallenge() {
const dataTestDOM = document.getElementsByClassName(
dataTestComponentClassName
)[0];
if (!dataTestDOM) {
document.querySelectorAll(PLAYER_NEXT)[0].dispatchEvent(clickEvent);
return null;
} else {
const dataTestAtrr = Object.keys(dataTestDOM).filter((att) =>
/^__reactProps/g.test(att)
)[0];
const childDataTestProps = dataTestDOM[dataTestAtrr];
const { challenge } = getChallengeObj(childDataTestProps);
return challenge;
}
}
// pressEnter() function
function pressEnter() {
const clickEvent = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: false,
});
document
.querySelector('button[data-test="player-next"]')
.dispatchEvent(clickEvent);
}
// pressEnter() function but for stories
function pressEnterStories() {
const clickEvent = new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: false,
});
document
.querySelector('button[data-test="stories-player-continue"]')
.dispatchEvent(clickEvent);
}
// dynamicInput() function
function dynamicInput(element, msg) {
let input = element;
let lastValue = input.value;
input.value = msg;
let event = new Event("input", { bubbles: true });
event.simulated = true;
let tracker = input._valueTracker;
if (tracker) {
tracker.setValue(lastValue);
}
input.dispatchEvent(event);
}
// Solves the Challenge
function classify() {
const challenge = getChallenge();
if (!challenge) return;
if (DEBUG) console.log(`${challenge.type}`, challenge);
switch (challenge.type) {
case GAP_FILL_TYPE:
case SELECT_TYPE:
case SELECT_PRONUNCIATION_TYPE:
case READ_COMPREHENSION_TYPE:
case LISTEN_COMPREHENSION_TYPE:
case CHARACTER_SELECT_TYPE:
case FORM_TYPE: {
const { choices, correctIndex } = challenge;
if (DEBUG) {
console.log(
"READ_COMPREHENSION, LISTEN_COMPREHENSION, CHARACTER_SELECT_TYPE, GAP_FILL_TYPE, SELECT_PRONUNCIATION_TYPE",
{ choices, correctIndex }
);}
document
.querySelectorAll(CHALLENGE_CHOICE)
[correctIndex].dispatchEvent(clickEvent);
return { choices, correctIndex };
}
case TAP_COMPLETE_TYPE: {
const { choices, correctIndices } = challenge;
const tokens = document.querySelectorAll(WORD_BANK);
if (DEBUG) {console.log("TAP_COMPLETE_TYPE", { choices, correctIndices, tokens });}
return { choices, correctIndices };
}
case LISTEN_ISOLATION_TYPE: {
const { correctIndex } = challenge;
const tokens = document.querySelectorAll(CHALLENGE_CHOICE);
if (DEBUG) {console.log("LISTEN_ISOLATION_TYPE", { correctIndex, tokens });}
tokens.forEach((e, i) => {
if(i == correctIndex) {
e.dispatchEvent(clickEvent);
}
});
return { correctIndex };
}
case MATCH_TYPE: {
const { pairs } = challenge;
const tokens = document.querySelectorAll(CHALLENGE_TAP_TOKEN_TEXT);
if (DEBUG) {console.log("CHARACTER_MATCH_TYPE", { tokens, pairs });}
pairs.forEach((pair) => {
for (let i = 0; i < tokens.length; i++) {
if (
tokens[i].innerText === pair.fromToken ||
tokens[i].innerText === pair.learningToken
) {
tokens[i].dispatchEvent(clickEvent);
}
}
});
return { pairs };
}
case CHARACTER_MATCH_TYPE: {
const { pairs } = challenge;
const tokens = document.querySelectorAll(CHALLENGE_TAP_TOKEN);
if (DEBUG) {console.log("CHARACTER_MATCH_TYPE", { tokens, pairs });}
pairs.forEach((pair) => {
for (let i = 0; i < tokens.length; i++) {
if (
tokens[i].innerText === pair.transliteration ||
tokens[i].innerText === pair.character
) {
tokens[i].dispatchEvent(clickEvent);
}
}
});
return { pairs };
}
case TRANSLATE_TYPE: {
const { correctTokens, correctSolutions } = challenge;
if (DEBUG) {console.log("TRANSLATE_TYPE", { correctTokens, correctSolutions });}
if (correctTokens) {
const tokens = document.querySelectorAll(CHALLENGE_TAP_TOKEN);
let ignoreTokeIndexes = [];
for (let correctTokenIndex in correctTokens) {
for (let tokenIndex in tokens) {
const token = tokens[tokenIndex];
if (ignoreTokeIndexes.includes(tokenIndex)) continue;
if (token.innerText === correctTokens[correctTokenIndex]) {
token.dispatchEvent(clickEvent);
ignoreTokeIndexes.push(tokenIndex);
if (DEBUG) {
console.log(
`correctTokenIndex [${correctTokens[correctTokenIndex]}] - tokenIndex [${token.innerText}]`
);
}
break;
}
}
}
} else if (correctSolutions) {
let textInputElement = document.querySelectorAll(
CHALLENGE_TRANSLATE_INPUT
)[0];
dynamicInput(textInputElement, correctSolutions[0]);
}
return { correctTokens };
}
case NAME_TYPE: {
const { correctSolutions, articles } = challenge;
if (DEBUG) {console.log("NAME_TYPE", { correctSolutions, articles });}
const regexFrom = (articles, flags) =>
new RegExp(
articles
.map((s) => s.replace(/[()[\]{}*+?^$|#.,\/\\\s-]/g, "\\$&"))
.sort((a, b) => b.length - a.length)
.join("|"),
flags
);
let textInputElement = document.querySelectorAll(CHALLENGE_TEXT_INPUT)[0];
let articleChoiceElement =
document.querySelectorAll(CHALLENGE_JUDGE_TEXT);
let string = correctSolutions[0];
const pattern = regexFrom(articles, "i");
console.log(string);
let result;
while ((result = pattern.exec(string))) {
let x = string.replace(result[0], "");
let correctSolution = x.trim();
console.log(correctSolution);
dynamicInput(textInputElement, correctSolution);
for (let i = 0; i < articles.length; i++) {
if (articles[i] === result) {
console.log(result);
articleChoiceElement[i].dispatchEvent(clickEvent);
document
.querySelectorAll(CHALLENGE_CHOICE)
[i].dispatchEvent(clickEvent);
document
.querySelectorAll(CHALLENGE_CHOICE)
[result].dispatchEvent(clickEvent);
}
}
return { correctSolutions, articles };
}
break;
}
case COMPLETE_REVERSE_TRANSLATION_TYPE: {
const { displayTokens } = challenge;
if (DEBUG) {console.log("COMPLETE_REVERSE_TRANLATION_TYPE", { displayTokens });}
const { text } = displayTokens.filter((token) => token.isBlank)[0];
let textInputElement = document.querySelectorAll(CHALLENGE_TEXT_INPUT)[0];
dynamicInput(textInputElement, text);
return { displayTokens };
}
case LISTEN_TAP_TYPE: {
const { correctTokens } = challenge;
if (DEBUG) {console.log("LISTEN_TAP_TYPE", { correctTokens });}
const tokens = document.querySelectorAll(CHALLENGE_TAP_TOKEN);
for (let wordIndex in correctTokens) {
tokens.forEach((token) => {
if (token.innerText === correctTokens[wordIndex]) {
token.dispatchEvent(clickEvent);
}
});
}
return { correctTokens };
}
case LISTEN_TYPE: {
const { prompt } = challenge;
if (DEBUG) {console.log("LISTEN_TYPE", { prompt });}
let textInputElement = document.querySelectorAll(
CHALLENGE_TRANSLATE_INPUT
)[0];
dynamicInput(textInputElement, prompt);
return { prompt };
}
case JUDGE_TYPE: {
const { correctIndices } = challenge;
if (DEBUG) {console.log("JUDGE_TYPE", { correctIndices });}
document
.querySelectorAll(CHALLENGE_JUDGE_TEXT)
[correctIndices[0]].dispatchEvent(clickEvent);
return { correctIndices };
}
case DIALOGUE_TYPE:
case CHARACTER_INTRO_TYPE: {
const { choices, correctIndex } = challenge;
if (DEBUG)
{console.log("CHARACTER_INTRO_TYPE, DIALOGUE_TYPE", {
choices,
correctIndex,
});}
document
.querySelectorAll(CHALLENGE_JUDGE_TEXT)
[correctIndex].dispatchEvent(clickEvent);
return { choices, correctIndex };
}
case SELECT_TRANSCRIPTION_TYPE: {
const { choices, correctIndex } = challenge;
if (DEBUG) {console.log("SELECT_TRANSCRIPTION_TYPE", { choices, correctIndex });}
document
.querySelectorAll(CHALLENGE_JUDGE_TEXT)
[correctIndex].dispatchEvent(clickEvent);
return { choices, correctIndex };
}
case SPEAK_TYPE: {
const { prompt } = challenge;
if (DEBUG) {console.log("SPEAK_TYPE", { prompt });}
document.querySelectorAll(PLAYER_SKIP)[0].dispatchEvent(clickEvent);
return { prompt };
}
case ASSIST_TYPE: {
const { choices, correctIndex } = challenge;
if (DEBUG) {console.log("ASSIST_TYPE", { choices, correctIndex });}
document
.querySelectorAll(CHALLENGE_JUDGE_TEXT)
[correctIndex].dispatchEvent(clickEvent);
return { choices, correctIndex };
}
default:
break;
}
}
// Stops when an answer is incorrect
function breakWhenIncorrect() {
const isBreak = document.querySelectorAll(BLAME_INCORRECT).length > 0;
if (isBreak) {
console.log("Incorrect, stopped");
clearInterval(mainInterval);
}
}
// Main Function
function main() {
try {
let isPlayerNext = document
.querySelectorAll(PLAYER_NEXT)[0]
.textContent.toUpperCase();
if (isPlayerNext.valueOf() !== "CONTINUE") {
classify();
breakWhenIncorrect();
pressEnter();
}
setTimeout(pressEnter, 150);
} catch (e) {
console.log(e);
}
}
// Stories Function
function stories() {
try {
let isPlayerNext = document
.querySelectorAll(STORIES_PLAYER_NEXT)[0]
.textContent.toUpperCase();
if (isPlayerNext.valueOf() !== "CONTINUE") {
classify();
pressEnterStories();
}
setTimeout(pressEnterStories, 50);
} catch (e) {
console.log(e);
}
}
// Calls main()
function solveChallenge() {
// Check if its a Skill / Alphabet / Checkpoint URL
if (/[sac][klh][ipe][lhc][lak]/gi.test(window.location.href) == true) {
if (DEBUG) {console.log("Skill URL Detected");}
mainInterval = setInterval(main, TIME_OUT);
}
// Check if its a Stories URL
if (/stories/gi.test(window.location.href) == true) {
if (DEBUG) {console.log("Stories URL Detected");}
mainInterval = setInterval(stories, TIME_OUT);
}
console.log(`to stop the script run "clearInterval(${mainInterval})"`);
}
(solveChallenge)();