forked from AndreNijman/EducationPerfectedAgain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplyperfected.js
More file actions
310 lines (274 loc) · 12.5 KB
/
simplyperfected.js
File metadata and controls
310 lines (274 loc) · 12.5 KB
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
const puppeteer = require('puppeteer');
(async () => {
const DIR = {
loginUrl: 'https://app.educationperfect.com/app/login',
selectors: {
baseWords: 'div.baseLanguage.question-label.native-font.ng-binding',
targetWords: 'div.targetLanguage.question-label.native-font.ng-binding',
questionText: '#question-text.native-font',
answerInput: 'input#answer-text',
continueButton: 'button#continue-button',
modal: 'div[uib-modal-window=modal-window]',
modalFields: {
question: 'td.field.native-font#question-field',
correct: 'td.field.native-font#correct-answer-field'
}
}
};
let dict = {}, reverseDict = {}, audioMap = {};
let running = false;
let mode = 'delay';
let question_mode = '';
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const cleanString = s => String(s).replace(/\([^)]*\)/g, '').split(/[;]/)[0].trim();
const randStr = (min, max) => {
const len = Math.floor(Math.random() * (max - min + 1)) + min;
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
return Array.from({ length: len }).map(() => chars[Math.floor(Math.random() * chars.length)]).join('');
};
async function notify(msg) {
await page.evaluate(message => alert(message), msg);
}
async function refreshAll() {
await notify('Please wait while the bot refreshes the word lists. This may take a few seconds.');
await page.evaluate(() => {
document.getElementById('refresh-btn').style.backgroundColor = 'lightyellow';
document.getElementById('ep-control-panel').style.backgroundColor = 'lightyellow';
}, running);
dict = {};
reverseDict = {};
audioMap = {};
try {
const base = await page.$$eval(DIR.selectors.baseWords, els => els.map(e => e.textContent));
const target = await page.$$eval(DIR.selectors.targetWords, els => els.map(e => e.textContent));
base.forEach((bRaw, i) => {
const t = cleanString(target[i] || '');
const b = cleanString(bRaw);
if (t && b) {
dict[t] = b;
reverseDict[b] = t.split(",")[0].trim();
}
});
} catch {}
try {
const items = await page.$$('.preview-grid .stats-item');
for (const item of items) {
const icon = await item.$('.sound-icon.has-sound');
if (!icon) continue;
const rawA = await item.$eval('.baseLanguage', el => el.textContent.trim());
const a = cleanString(rawA);
await page.evaluate(el => el.click(), icon);
await sleep(500);
const src = await page.evaluate(() => window.lastAudioSrc || null);
if (src) {
audioMap[src] = a;
}
}
} catch {}
question_mode = await page.evaluate(() => {
return document.querySelectorAll('.selected[ng-click="starter.selectLearningMode(item)"]')[0].children[1].children[0].children[0].textContent.toString();
});
let totalEntries = Object.keys(dict).length;
let entriesWithAudio = Object.values(dict).filter(v =>
Object.values(audioMap).includes(v)
).length;
let message = `All word lists refreshed:\nTotal entries: ${totalEntries}\nWith audio: ${entriesWithAudio}\n\n`;
// for (const [k, v] of Object.entries(dict)) {
// const link = Object.entries(audioMap).find(([, word]) => word === v)?.[0] || 'no audio';
// message += `${k} → ${v} (${link})\n\n`;
// }
await notify(message);
await page.evaluate(() => {
document.getElementById('refresh-btn').style.backgroundColor = '#f0f0f0';
document.getElementById('ep-control-panel').style.backgroundColor = '#fff';
}, running);
}
async function fixAnswer(lastAnswer) {
try {
await page.waitForFunction(sel => document.querySelector(sel)?.textContent.trim().length > 0, {}, DIR.selectors.modalFields.question);
const correctText = await page.$eval(DIR.selectors.modalFields.correct, el => el.textContent);
dict[lastAnswer] = cleanString(correctText);
await page.$eval(DIR.selectors.continueButton, btn => btn.disabled = false);
await page.click(DIR.selectors.continueButton);
} catch (err) {}
}
async function loopAnswers() {
let answer;
let last_answer;
let qText = '';
if ((question_mode === 'Dictation' || question_mode === 'Listening') && mode !== "auto") {
notify("Due to some vocabulary having multiple audio, semi and delay mode may get questions wrong. Auto mode is recommended for this.")
}
while (running) {
try {
qText = await page.$eval(DIR.selectors.questionText, el => el.textContent.trim());
} catch {}
if (qText) {
const cleaned = cleanString(qText).split(/[;,]/)[0].trim();
if (dict[cleaned]) {
answer = dict[cleaned];
} else if (reverseDict[cleaned]) {
answer = reverseDict[cleaned]
} else {
answer = randStr(8, 10);
}
//await notify(`Question: "${qText}"\nCleaned: "${cleaned}"\nAnswer: "${answer}"`);
} else {
let src = null;
try {
const speaker = await page.$('.voice-speaker');
if (speaker) {
await page.evaluate(el => el.click(), speaker);
await sleep(500);
src = await page.evaluate(() => window.lastAudioSrc || null);
}
} catch {}
if (question_mode === "Dictation") {
answer = (src && reverseDict[audioMap[src]]) || randStr(8, 10);
} else {
answer = (src && audioMap[src]) || randStr(8, 10);
}
// await notify(`Audio question: src="${src}"\nAnswer: "${answer}"`);
}
if (answer === last_answer) {
continue;
} else {
last_answer = answer;
}
await page.click(DIR.selectors.answerInput, { clickCount: 3 });
await page.keyboard.sendCharacter(answer);
if (mode === 'auto') {
await page.keyboard.press('Enter');
} else if (mode === 'delay') {
await sleep(Math.random() * 3000);
await page.keyboard.press('Enter');
}
if (await page.$(DIR.selectors.modal)) {
await fixAnswer(answer);
}
}
}
async function toggleRun() {
running = !running;
await page.evaluate(run => {
document.getElementById('start-btn').style.backgroundColor = run ? 'lightgreen' : '#f0f0f0';
document.getElementById('ep-control-panel').style.backgroundColor = run ? 'lightgreen' : '#fff';
}, running);
if (running) {
await loopAnswers().catch(err => {
running = false;
});
await page.evaluate(() => {
document.getElementById('start-btn').style.backgroundColor = '#f0f0f0';
document.getElementById('ep-control-panel').style.backgroundColor = '#fff';
});
}
}
async function setMode(newMode) {
mode = newMode;
await page.evaluate(active => {
['Auto', 'Semi', 'Delay'].forEach(l => {
const btn = document.getElementById(`mode-${l}`);
if (btn) {
btn.style.backgroundColor = l.toLowerCase() === active ? 'lightgreen' : '#f0f0f0';
}
});
}, mode);
}
async function initPanel() {
await page.evaluate(currentMode => {
if (document.querySelector('#ep-control-panel')) return;
window.currentMode = currentMode || 'delay';
window.running = window.running || false;
const panel = document.createElement('div');
panel.id = 'ep-control-panel';
Object.assign(panel.style, {
position: 'fixed',
top: '10px',
right: '120px',
padding: '10px',
backgroundColor: '#fff',
border: '1px solid #ccc',
borderRadius: '8px',
zIndex: 9999,
fontFamily: 'sans-serif',
boxShadow: '0 0 10px rgba(0,0,0,0.1)'
});
const statusLine = `Mode: ${window.currentMode}`;
panel.innerHTML = `
<div id="status-line" style="font-size:24px; color:#333; margin-bottom:8px;">SimplyPerfected v1.0.1</div>
<div style="margin-bottom:8px;">
<button id="refresh-btn" title="Refresh all (Alt+R)">🔄</button>
<button id="start-btn" title="Start/stop (Alt+S)">▶️</button>
<button id="mode-Auto" title="Instant (Alt+1)">⚡</button>
<button id="mode-Semi" title="Semi-auto (Alt+2)">⏸️</button>
<button id="mode-Delay" title="Delayed (Alt+3)" style="background-color:lightgreen;">⏱️</button>
<button id="hide-panel" title="Hide panel">❌</button>
</div>
<div id="status-line" style="font-size:12px; color:#333;">
${statusLine}
</div>
<div style="font-size:11px; color:#666; margin-top:6px;">Alt+S to start/stop</div>
`;
document.body.appendChild(panel);
const setStatus = msg => {
const el = document.getElementById('status-line');
if (el) el.textContent = msg;
};
window.setStatus = setStatus;
document.getElementById('refresh-btn').onclick = window.refresh;
document.getElementById('start-btn').onclick = window.startAnswer;
document.getElementById('mode-Auto').onclick = window.setAuto;
document.getElementById('mode-Semi').onclick = window.setSemi;
document.getElementById('mode-Delay').onclick = window.setDelay;
document.getElementById('hide-panel').onclick = () => {
panel.style.display = 'none';
const showBtn = document.createElement('button');
showBtn.textContent = '📋';
showBtn.title = 'Show panel';
Object.assign(showBtn.style, {
position: 'fixed',
top: '10px',
right: '120px',
zIndex: 9999,
padding: '4px',
fontSize: '16px'
});
showBtn.onclick = () => {
panel.style.display = 'block';
showBtn.remove();
};
document.body.appendChild(showBtn);
};
document.addEventListener('keydown', e => {
if (!e.altKey) return;
if (e.key.toLowerCase() === 'r') window.refresh();
if (e.key.toLowerCase() === 's') window.startAnswer();
if (e.key === '1') window.setAuto();
if (e.key === '2') window.setSemi();
if (e.key === '3') window.setDelay();
});
}, mode);
}
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
args: ['--start-maximized']
});
const [page] = await browser.pages();
await page.evaluateOnNewDocument(() => {
window.lastAudioSrc = null;
const origPlay = HTMLAudioElement.prototype.play;
HTMLAudioElement.prototype.play = function () {
window.lastAudioSrc = this.src;
return origPlay.call(this);
};
});
await page.exposeFunction('refresh', refreshAll);
await page.exposeFunction('startAnswer', toggleRun);
await page.exposeFunction('setAuto', () => setMode('auto'));
await page.exposeFunction('setSemi', () => setMode('semi'));
await page.exposeFunction('setDelay', () => setMode('delay'));
page.on('load', initPanel);
await page.goto(DIR.loginUrl);
})();