-
Notifications
You must be signed in to change notification settings - Fork 1
/
hack.js
144 lines (123 loc) · 4.98 KB
/
hack.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
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function sleepByAudioTime(audioSrc, fn) {
// Create an instance of AudioContext
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
var proxyUrl = 'https://cors-anywhere.herokuapp.com/';
var response = await fetch(proxyUrl + audioSrc);
var buffer = await response.arrayBuffer();
// Start Request
var audioBuffer = await audioContext.decodeAudioData(buffer);
var duration = audioBuffer.duration;
console.log("The duration is:" + duration + " seconds... sleeping...");
await sleep(duration * 1000 + 100);
}
// async function sleepByWords(text) {
// var numWords = text.split(" ").length;
// var numPsiks = (text.match(/,/g) || []).length;
// var three_dots = (text.match(/\…/g) || []).length;
// var sleepVal = numWords*700 + numPsiks*700 + three_dots*1500;
// console.log(`Sleeping ${sleepVal}`);
// await sleep(sleepVal);
// }
async function startStory() {
await sleep(2000);
console.log("Starting...");
var start_btn = document.querySelectorAll("button.story-starter-start-story")[0];
if (start_btn) {
start_btn.click();
}
await sleep(2000);
for (let i=0;i<elms["elements"].length; i++) {
cur_elm = elms["elements"][i];
// console.log(i);
// console.log(cur_elm);
if (cur_elm["type"] === "LINE" || cur_elm["type"] === "TITLE") {
console.log(cur_elm["line"]["content"]["text"]);
// await sleepByWords(cur_elm["line"]["content"]["text"]);
await sleepByAudioTime(cur_elm["line"]["content"]["audio"]["url"]);
console.log("Clicking cont...");
document.querySelectorAll("button.continue")[0].click();
}
else if(cur_elm["type"] === "MULTIPLE_CHOICE" || cur_elm["type"] === "SELECT_PHRASE") {
ansInd = cur_elm['correctAnswerIndex'];
console.log(`Clicking answer index: ${ansInd} and then continue`);
await sleep(500);
document.querySelectorAll("ul.challenge-answers li button")[ansInd].click();
await sleep(500);
document.querySelectorAll("button.continue")[0].click();
// document.querySelectorAll("ul.challenge-answers").children[ansInd].children[0].click()
}
else if (cur_elm["type"] === "CHALLENGE_PROMPT") {
console.log("Skipping prompt... sleeping 500");
await sleep(500);
continue;
}
else if (cur_elm["type"] === "MATCH") {
var tokens_arr = Array.from(document.querySelectorAll("ul.tokens button"));
for (let j=0; j< cur_elm["fallbackHints"].length; j++) {
await sleep(500);
tokens_arr.filter(elem => elem.innerText == cur_elm["fallbackHints"][j]["phrase"])[0].click();
await sleep(500);
tokens_arr.filter(elem => elem.innerText == cur_elm["fallbackHints"][j]["translation"])[0].click();
}
console.log("Challange resolved... continue...");
await sleep(2000);
document.querySelectorAll("button.continue")[0].click();
}
else if (cur_elm["type"] === "POINT_TO_PHRASE") {
var ans_arr = Array.from(document.querySelectorAll("div.tappable-phrase"));
var ans_text = Array.from(cur_elm["transcriptParts"]).filter(elem=>elem.selectable)[cur_elm["correctAnswerIndex"]].text;
ans_arr.filter(e => e.textContent == ans_text)[0].click();
console.log("Challange resolved... continue...");
await sleep(2000);
document.querySelectorAll("button.continue")[0].click();
}
else if (cur_elm["type"] === "ARRANGE") {
buttons_arr = document.querySelectorAll(".phrase-bank span");
for (let z=0;z<buttons_arr.length;z++) {
buttons_arr[cur_elm["phraseOrder"][z]].click();
await sleep(100);
}
console.log("Arranging challange resolved, contrinue...");
await sleep(1000);
document.querySelectorAll("button.continue")[0].click();
}
else {
console.log("Unknown element");
}
};
document.querySelectorAll("button.continue")[0].click();
await sleep(2500);
console.log("Getting new story :)");
var stories = JSON.parse(httpGet("https://stories.duolingo.com/api2/stories?fromLanguage=en&learningLanguage=es&masterVersions=false&illustrationFormat=svg"));
var randSet = stories.sets[getRandomInt(3)];
var randStory = randSet[getRandomInt(randSet.length)].id;
loc = "https://stories.duolingo.com/lessons/" + randStory;
location.replace(loc);
}
(function() {
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener('load', function() {
if (this.responseType == '' || this.responseType == 'text') {
if (this.responseText.startsWith("{\"elements")) {
elms = JSON.parse(this.responseText); //whatever the response was
startStory();
}
}
});
origOpen.apply(this, arguments);
};
})();
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}