-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
204 lines (171 loc) Β· 6.23 KB
/
index.ts
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
import * as puppeteer from 'puppeteer';
import * as cliProgress from 'cli-progress';
import {
targetURL,
email,
password,
perfect as perfectWhenSubject,
search as searchText,
} from './credentials.json';
import console = require('console');
declare const $: JQueryStatic;
const isTask = targetURL.includes('task');
interface IProblem {
name: string;
value: string;
}
const solveProblems = async (browser: puppeteer.Browser) => {
const page = await browser.newPage();
await page.goto(targetURL, { timeout: 0 });
await page.$eval('#loginID', (element, email: string) =>
(element as HTMLInputElement).value = email, email);
await page.$eval('#loginPW', (element, password: string) =>
(element as HTMLInputElement).value = password, password);
await page.click('div.login-buttons > button:first-child');
await page.goto(targetURL, {
timeout: 0,
waitUntil: 'domcontentloaded',
});
const uncompletedProblems = await page.evaluate((searchText: string, isTask: boolean) => {
const problemRows = [...document.querySelectorAll('tbody > tr')];
return problemRows
// @ts-ignore
.flatMap((row) => {
const selectorForTargetType = `td:nth-child(${isTask ? 4 : 5})`;
const problemName = row.querySelector(selectorForTargetType) as HTMLTableDataCellElement;
if (!problemName) {
return [];
}
const problemNameText = problemName.innerText;
const isDailyTask = problemNameText.includes(searchText);
const isValidTask = !row.className.includes('complete')
&& row.getAttribute('role') === 'row'
&& isDailyTask;
if (isValidTask) {
return [
{
name: problemNameText,
value: row.getAttribute('value'),
},
];
}
return [];
});
}, searchText, isTask);
console.log(uncompletedProblems);
if (!uncompletedProblems.length) {
console.log('π All problems solved!');
await page.screenshot({ path: 'result.png' });
return;
}
for (const uncompletedProblem of uncompletedProblems) {
const {
name: problemName,
value: problemValue,
} = uncompletedProblem as IProblem;
await page.click(`tr[value='${problemValue}']`);
await page.waitFor(500);
console.log(`π Solving '${problemName}'`);
const values = await page.evaluate(() => {
const element = document.querySelector('#TestDetail-table > tbody > tr');
if (!element) return [];
return [{
value: element.getAttribute('value'),
detailvalue: element.getAttribute('detailvalue'),
}];
});
const type = 'ymWuGYYSOfmJLRPkt3xlfw{e}{e}';
const response = await page.evaluate((values: string[], type: string) => $.ajax({
url: '/Utils/TestDetailPrint',
data: { values, type },
type: 'POST',
async: false,
}), values, type);
const { Table01: array } = response;
const keys = Object.keys(array);
const answers = keys.map((key) => Number(array[key].QST_CORRECT));
console.log(`Answers: ${answers.join(', ')}`);
await page.evaluate(() => {
const element = document.querySelector('div.gotoStudy') as HTMLDivElement;
if (element) {
element.click();
}
});
await page.waitForNavigation({ timeout: 0 });
const SECONDS = 1000;
const MINIMAL_SOLVE_TIME_TO_GET_POINTS = 21;
const timeoutBias = Math.floor(Math.random() * 6);
const timeoutDelayBeforeSubmit = (MINIMAL_SOLVE_TIME_TO_GET_POINTS + timeoutBias) * SECONDS;
console.log('π Solving task started.');
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(timeoutDelayBeforeSubmit, 0);
const delayIncrement = timeoutDelayBeforeSubmit / 100;
const delayForHalf = timeoutDelayBeforeSubmit / 2;
for (let currentDelay = 0; currentDelay <= delayForHalf; currentDelay += delayIncrement) {
progressBar.update(currentDelay);
await page.waitFor(delayIncrement);
}
await page.evaluate((problemName: string, perfectWhenSubject: string, answers: number[]) => {
const selectors = [...document.querySelectorAll('table#Answer tr')].slice(1);
selectors.forEach((selector, problemNumber) => {
const delayForEachSelect = problemNumber * 500;
setTimeout(() => {
const subjectiveInput = selector.querySelector('input');
if (subjectiveInput) {
subjectiveInput.value = (answers[problemNumber] || '.') as string;
return;
}
const badges = [...selector.querySelectorAll('span.badge')];
const answer = (() => {
const isPerfect = perfectWhenSubject && problemName.includes(perfectWhenSubject);
if (isPerfect || perfectWhenSubject === '*') {
return answers[problemNumber];
}
const random = Math.random() * 100;
if (random <= 50) {
return answers[problemNumber];
}
return Math.floor(Math.random() * 5 + 1);
})();
const badgeNumber = answer - 1;
(badges[badgeNumber] as HTMLSpanElement).click();
}, delayForEachSelect);
});
console.log('\nβ
Checked all π');
}, problemName, perfectWhenSubject, answers);
for (let currentDelay = delayForHalf; currentDelay <= timeoutDelayBeforeSubmit; currentDelay += delayIncrement) {
progressBar.update(currentDelay);
await page.waitFor(delayIncrement);
}
console.log('\nπ Solving task finished!');
await page.evaluate(() => {
const element = document.querySelector('div.AnswerSubmit > a') as HTMLAnchorElement;
if (element) {
element.click();
}
});
await page.waitFor(1500);
await page.screenshot({ path: 'result.png' });
await page.goto(targetURL, {
timeout: 0,
waitUntil: 'domcontentloaded',
});
}
console.log('π Finished.');
return await page.close();
};
(async () => {
const browser = await puppeteer.launch({
dumpio: true,
});
try {
await solveProblems(browser);
} catch (error) {
console.log(error);
process.exit();
} finally {
console.log('Return to main function.');
await browser.close();
process.exit();
}
})();