-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowtor-hack.js
470 lines (390 loc) · 20 KB
/
powtor-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
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
468
469
470
// uwaga. niektóre komentarze są po angielsku ze względu na pomoc od Github Copilot :)
console.log("Cheat uruchamia się...");
console.log("Informacje i zgłaszanie błędów: https://github.com/Davilarek/PowtorHack");
var cheatStartTime;
var answersGetTime;
/**
* Pobiera ilość commitów z API GitHuba (właściwie opcjonalne),
* następnie sprawdza, czy bieżąca strona jest powtórką,
* następnie pobiera adres URL API pytania,
* następnie pobiera dane pytania z API pytania,
* następnie porównuje dane pytania z pytaniem na stronie,
* a następnie wyświetla odpowiedzi do konsoli.
*/
function startCheat() {
cheatStartTime = performance.now();
var githubApiUrl = "https://api.github.com/repos/Davilarek/PowtorHack/commits?sha=main&per_page=1&page=1";
var githubApiRequest = new XMLHttpRequest();
githubApiRequest.open("GET", githubApiUrl);
githubApiRequest.setRequestHeader("User-Agent", "request");
githubApiRequest.onreadystatechange = function () {
if (githubApiRequest.readyState === 4) {
let githubApiResponse = githubApiRequest.getResponseHeader("link").split("https://api.github.com")[2].split("&")[2].split("=")[1].split(">")[0];
console.log("%cPowtor Hack " + "%cv1." + githubApiResponse + "%c by Davilarek", "color: red", "color: white", "color: red");
answersGetTime = performance.now();
// // check if url contains "https://powtorkomat8.apps.gwo.pl/practice-exercises/" and if it doesn't show console.log "Nie znaleziono powtórki"
// if (!window.location.href.includes("https://powtorkomat8.apps.gwo.pl/practice-exercises/")) {
// console.log("%cNie znaleziono powtorki!", "color: red");
// return;
// }
if (window.location.href.includes("https://powtorkomat8.apps.gwo.pl/practice-exercises/")) {
practicesHack();
}
else if (window.location.href.includes("https://powtorkomat8.apps.gwo.pl/sections/exams")) {
examsHack();
}
else {
console.log("%cNie znaleziono powtórki!", "color: red");
return;
}
}
};
githubApiRequest.send();
}
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l = this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
function practicesHack() {
var open = window.XMLHttpRequest.prototype.open;
var questionApiUrl = "";
/**
* podmiana XMLHttpRequest.prototype.open() na funkcję, która zapisuje url pierwszego żądania do questionApiUrl
* @param {string} method - metoda HTTP użyta do żądania
* @param {string} url - adres URL użyty do żądania
*/
function openReplacement(method, url) {
this._url = url;
// uwaga. wypadałoby sprawdzić czy url na pewno jest odpowiedzią api zawierającą jsona
questionApiUrl = arguments[1];
return open.apply(this, arguments);
}
window.XMLHttpRequest.prototype.open = openReplacement;
// manualne odświeżenie strony z zadaniem, aby wystąpiło ponowne żądanie do API
document.getElementsByClassName("indicator exercise-mode ng-star-inserted")[0].parentElement.childNodes[0].click()
var apiRequest = new XMLHttpRequest();
apiRequest.open("GET", questionApiUrl);
// nagłówki znajdują się w ciasteczkach dokumentu
// uwaga. prawdopodobnie można uzyskać te ciasteczka w samym dokumencie
// var header1 = document.getElementsByClassName("content screen-exercises")[0].ownerDocument.cookie.split("; ")[0].split("=")
// var header2 = document.getElementsByClassName("content screen-exercises")[0].ownerDocument.cookie.split("; ")[1].split("=")
// uwaga. /\ powyższy komentarz jest nieaktualny, okazuje się że można.
var headers = document.cookie.split("; ");
// przywracanie domyślnego XMLHttpRequest.prototype.open(), prawdopodobnie nie jest to potrzebne
window.XMLHttpRequest.prototype.open = open;
// ustawienie nagłówka zapytania
apiRequest.setRequestHeader("X-Authorization", headers[0].split("=")[1]);
apiRequest.setRequestHeader("japa_phpsessid", headers[1].split("=")[1]);
var questionClassNames = ['instruction', 'question'];
apiRequest.onreadystatechange = function () {
if (apiRequest.readyState === 4) {
// przetwarzanie danych z odpowiedzi API pytania
// uwaga. można dodać sprawdzanie czy dane są poprawne
let apiResponse = JSON.parse(apiRequest.responseText).pool;
// apiResponseRaw na ten moment jest potrzebne tylko do debugowania
let apiResponseRaw = JSON.parse(apiRequest.responseText);
var questionData = document.getElementsByTagName("app-exercise-loader")[0].getElementsByClassName("ng-star-inserted");
for (var questionElements = 0; questionElements < questionData.length; questionElements++) {
// sprawdzanie czy questionData[questionElements].className zawiera którykolwiek element z questionClassNames
const questionDataContainsClass = questionClassNames.some(element => {
if (questionData[questionElements].className.indexOf(element) !== -1) {
return true;
}
return false;
});
if (!questionDataContainsClass) {
continue;
}
hackAnswersUsingJSON(apiResponse, apiResponseRaw, questionData, questionElements);
}
}
};
apiRequest.send();
}
function hackAnswersUsingJSON(apiResponse, apiResponseRaw, questionData, questionElements) {
mainFor: for (var questionIndex = 0; questionIndex < apiResponse.length; questionIndex++) {
var questionInfo = "";
// jest kilka typów zadań. te ify są potrzebne do znalezienia odpowiedniego typu zadania
if ("instruction" in apiResponse[questionIndex])
questionInfo = apiResponse[questionIndex].instruction;
if ("question" in apiResponse[questionIndex])
questionInfo = apiResponse[questionIndex].question;
if ("question" in apiResponse[questionIndex].items[0])
questionInfo = apiResponse[questionIndex].items[0].question;
var parsedQuestionData = new DOMParser().parseFromString(questionInfo, "text/html").documentElement;
// rozwiązanie w komentarzu poniżej \/ jest bezpieczniejsze, ale nie zawsze działa
//var question = questionData[questionElements].childNodes[0];
var question = questionData[questionElements];
if (window.powtorHackDebug) console.log("parsedQuestionData: " + parsedQuestionData.textContent + "\nquestion: " + question.textContent);
parsedQuestionData.textContent = parsedQuestionData.textContent.replace(/\\\\frac{/g, "").replace(/}{/g, "").replace(/}/g, "");
question.textContent = question.textContent.replace(/\\\\frac{/g, "").replace(/}{/g, "").replace(/}/g, "");
parsedQuestionData.textContent = parsedQuestionData.textContent.replace(/\\frac{/g, "").replace(/}{/g, "").replace(/}/g, "");
question.textContent = question.textContent.replace(/\\frac{/g, "").replace(/}{/g, "").replace(/}/g, "");
// FIXME: uwaga. /\ niebezpieczne rozwiązanie.
if (window.powtorHackDebug) console.log("parsedQuestionData: " + parsedQuestionData.textContent + "\nquestion: " + question.textContent);
// remove all newlines and carriage return from question.textContent and parsedQuestionData.textContent
question.textContent = question.textContent.replace(/\n/g, "").replace(/\r/g, "");
parsedQuestionData.textContent = parsedQuestionData.textContent.replace(/\n/g, "").replace(/\r/g, "");
// FIXME: uwaga. /\ (potencjalnie) niebezpieczne rozwiązanie.
if (window.powtorHackDebug) console.log("parsedQuestionData: " + parsedQuestionData.textContent + "\nquestion: " + question.textContent);
// replace ' ' with '' globally in question.textContent and parsedQuestionData.textContent
question.textContent = question.textContent.replace(/\s+/gm, '');
parsedQuestionData.textContent = parsedQuestionData.textContent.replace(/\s+/gm, '');
// FIXME: uwaga. /\ (potencjalnie) niebezpieczne rozwiązanie.
if (window.powtorHackDebug) console.log("parsedQuestionData: " + parsedQuestionData.textContent + "\nquestion: " + question.textContent);
var questionReady = question.textContent.replace(/\n*$/, "").normalize("NFD").replace(/\p{Diacritic}/gu, "").replace(/(\u2212)/gim, "-");
var parsedQuestionDataReady = parsedQuestionData.textContent.replace(/\n*$/, "").normalize("NFD").replace(/\p{Diacritic}/gu, "").replace(/(\u2212)/gim, "-");
//console.log(question)
function s(x, y) {
var pre = ['string', 'number', 'bool']
if (typeof x !== typeof y) return pre.indexOf(typeof y) - pre.indexOf(typeof x);
if (x === y) return 0;
else return (x > y) ? 1 : -1;
}
if (question.parentElement.getElementsByClassName("values-abcd").length > 0 || (question.parentElement.getElementsByClassName("items-abcd")[0] && question.parentElement.getElementsByClassName("items-abcd")[0].getElementsByClassName("values-abcd").length > 0)) {
let answersData = null;
console.log("Próba uzyskania odpowiedzi za pomocą pytania...")
if (question.parentElement.getElementsByClassName("values-abcd").length > 0)
answersData = question.parentElement.getElementsByClassName("values-abcd")[0].children;
else
answersData = question.parentElement.getElementsByClassName("items-abcd")[0].getElementsByClassName("values-abcd")[0].children;
let alternativeAnswers = [];
for (let index = 0; index < answersData.length; index++) {
const element3 = answersData[index];
alternativeAnswers.push(element3.children[1].textContent);
}
alternativeAnswers = alternativeAnswers.map(function (x) {
return parseInt(x, 10);
});
let alternativeApiResponse = [];
//for (let i = 0; i < apiResponse.length; i++) {
//for (let j = 0; j < apiResponse[i].items[0].values.length; j++) {
for (let j = 0; j < apiResponse[questionIndex].items[0].values.length; j++) {
//var parsed = new DOMParser().parseFromString(apiResponse[i].items[0].values[j], "text/html").documentElement
var parsed = new DOMParser().parseFromString(apiResponse[questionIndex].items[0].values[j], "text/html").documentElement
parsed.textContent = parsed.textContent.replace(/\\\\frac{/g, "").replace(/}{/g, "").replace(/}/g, "");
parsed.textContent = parsed.textContent.replace(/\\frac{/g, "").replace(/}{/g, "").replace(/}/g, "");
parsed.textContent = parsed.textContent.replace(/\n/g, "").replace(/\r/g, "");
parsed.textContent = parsed.textContent.replace(/\s+/gm, '');
var parsedReady = parsed.textContent.replace(/\n*$/, "").normalize("NFD").replace(/\p{Diacritic}/gu, "").replace(/(\u2212)/gim, "-");
alternativeApiResponse.push(parsedReady);
}
//}
const chunkSize = 4;
for (let i = 0; i < alternativeApiResponse.length; i += chunkSize) {
const chunk = alternativeApiResponse.slice(i, i + chunkSize).map(function (x) {
return parseInt(x, 10);
});
//console.log(chunk.sort(s));
//console.log(alternativeAnswers.sort(s))
if (alternativeAnswers.sort(s).equals(chunk.sort(s))) {
console.log("Odnaleziono odpowiedź po pytaniu.");
console.log(apiResponse[questionIndex].items);
for (let subQuestionIndex = 0; subQuestionIndex < apiResponse[questionIndex].items.length; subQuestionIndex++) {
const element = apiResponse[questionIndex].items[subQuestionIndex];
if (element.answer && element.values) {
var parsedSubQuestionData = new DOMParser().parseFromString(element.values[element.answer], "text/html").documentElement;
console.log(`Sugestia odpowiedzi w pod-zadaniu ${subQuestionIndex + 1}: ` + parsedSubQuestionData.textContent);
}
}
if (window.powtorHackDebug) console.log(apiResponseRaw);
console.log("Operacja ukończona w " + (performance.now() - cheatStartTime) + "ms.");
console.log("Uzyskano odpowiedzi w " + (performance.now() - answersGetTime) + "ms.");
success = true;
return;
}
}
}
else
// ostatnia część kodu. zamienia wszystkie znaki specjalne na ich odpowiedniki, upraszcza pytania do maksimum, a następnie porównuje dane z API z danymi ze strony. nie jest to dokładne rozwiązanie, ale czasem działa.
if (questionReady.localeCompare(parsedQuestionDataReady) === 0) {
//console.log(alternativeApiResponse.sort(s))
// for (let subQuestionIndex = 0; subQuestionIndex < apiResponse[questionIndex].items.length; subQuestionIndex++) {
// const element = apiResponse[questionIndex].items[subQuestionIndex];
// if (element.answer && element.values) {
// for (let index = 0; index < element.values.length; index++) {
// const element2 = element.values[index];
// const chunkSize = 4;
// for (let i = 0; i < document.getElementsByClassName("value-abcd").length; i += chunkSize) {
// const chunk = [].slice.call(document.getElementsByClassName("value-abcd")).slice(i, i + chunkSize);
// console.log(chunk[index])
// console.log(element2)
// if (chunk[index] == element2) {
// console.log("found")
// }
// }
// }
// }
// }
console.log("Odnaleziono odpowiedź. ");
console.log(apiResponse[questionIndex].items);
for (let subQuestionIndex = 0; subQuestionIndex < apiResponse[questionIndex].items.length; subQuestionIndex++) {
const element = apiResponse[questionIndex].items[subQuestionIndex];
if (element.answer && element.values) {
var parsedSubQuestionData = new DOMParser().parseFromString(element.values[element.answer], "text/html").documentElement;
console.log(`Sugestia odpowiedzi w pod-zadaniu ${subQuestionIndex + 1}: ` + parsedSubQuestionData.textContent);
}
}
if (window.powtorHackDebug) console.log(apiResponseRaw);
console.log("Operacja ukończona w " + (performance.now() - cheatStartTime) + "ms.");
console.log("Uzyskano odpowiedzi w " + (performance.now() - answersGetTime) + "ms.");
success = true;
return;
}
// else {
// for (let subQuestionIndex = 0; subQuestionIndex < apiResponse[questionIndex].items.length; subQuestionIndex++) {
// const element = apiResponse[questionIndex].items[subQuestionIndex];
// if (element.answer && element.values) {
// for (let index = 0; index < element.values.length; index++) {
// const element2 = element.values[index];
// const chunkSize = 4;
// for (let i = 0; i < array.length; i += chunkSize) {
// const chunk = array.slice(i, i + chunkSize);
// if (chunk[index].localeCompare(element2) === 0) {
// console.log(chunk[index])
// console.log(element2)
// }
// }
// }
// }
// }
// }
}
// jeżeli wszystko zawiedzie, wyświetla surową odpowiedź API
// uwaga. należy zwrócić uwagę na to, że apiResponseRaw nie jest tym samym co apiResponse. apiResponseRaw zawiera też informacje o typie zadania, ilości punktów, itp. nie koniecznie musi to interesować użytkownika.
console.log("UWAGA! Nie odnaleziono odpowiedzi. Możesz nadal spróbować odnaleźć je ręcznie. ");
console.log(apiResponse);
console.log("Operacja ukończona w " + (performance.now() - cheatStartTime) + "ms.");
console.log("Uzyskano odpowiedzi w " + (performance.now() - answersGetTime) + "ms.");
}
function examsHack() {
function examsHackStart() {
var open = window.XMLHttpRequest.prototype.open;
var xhrIndex = 0;
var allQuestions = [];
//var questionApiUrl = "";
/**
* podmiana XMLHttpRequest.prototype.open() na funkcję, która zapisuje url pierwszego żądania do questionApiUrl
* @param {string} method - metoda HTTP użyta do żądania
* @param {string} url - adres URL użyty do żądania
*/
function openReplacement(method, url, aaa, aaa2, aa3) {
this._url = url;
// uwaga. wypadałoby sprawdzić czy url na pewno jest odpowiedzią api zawierającą jsona
//questionApiUrl = arguments[1];
//console.log(arguments)
//window.powtorHackDebug1 = {}
//window.powtorHackDebug1.questionApiUrl = arguments;
if (url == "")
return open.apply(this, arguments);
else
xhrIndex = xhrIndex + 1;
allQuestions.push(url);
return open.apply(this, arguments);
}
window.XMLHttpRequest.prototype.open = openReplacement;
setInterval(
function () {
if (xhrIndex == document.getElementsByClassName("exam-exercise").length) {
//console.log("test");
//console.log(allQuestions);
// aby przerwać pętlę, nie miałem lepszego pomysłu ¯\_(ツ)_/¯
xhrIndex = xhrIndex + 1;
getAnswers(allQuestions);
//clearInterval(interval);
}
},
50
)
setInterval(
function () {
if (window.location.href == "https://powtorkomat8.apps.gwo.pl/sections/exams") {
window.location.reload();
}
},
50
)
function getAnswers(_questionApiUrls) {
window.XMLHttpRequest.prototype.open = open;
//for loop document.getElementsByClassName("exam-exercise")
for (let examIndex = 0; examIndex < document.getElementsByClassName("exam-exercise").length; examIndex++) {
document.getElementsByClassName("exam-exercise")[examIndex].addEventListener("click", function () {
const questionApiUrl = _questionApiUrls[examIndex];
//console.log("questionApiUrl: " + questionApiUrl);
// manualne odświeżenie strony z zadaniem, aby wystąpiło ponowne żądanie do API
// document.getElementsByClassName("indicator exercise-mode ng-star-inserted")[0].parentElement.childNodes[0].click()
var apiRequest = new XMLHttpRequest();
apiRequest.open("GET", questionApiUrl);
// nagłówki znajdują się w ciasteczkach dokumentu
// uwaga. prawdopodobnie można uzyskać te ciasteczka w samym dokumencie
// var header1 = document.getElementsByClassName("content screen-exercises")[0].ownerDocument.cookie.split("; ")[0].split("=")
// var header2 = document.getElementsByClassName("content screen-exercises")[0].ownerDocument.cookie.split("; ")[1].split("=")
// uwaga. /\ powyższy komentarz jest nieaktualny, okazuje się że można.
var headers = document.cookie.split("; ");
// przywracanie domyślnego XMLHttpRequest.prototype.open(), prawdopodobnie nie jest to potrzebne
//window.XMLHttpRequest.prototype.open = open;
// ustawienie nagłówka zapytania
apiRequest.setRequestHeader("X-Authorization", headers[0].split("=")[1]);
apiRequest.setRequestHeader("japa_phpsessid", headers[1].split("=")[1]);
var questionClassNames = ['instruction', 'question', 'question-abcd', 'instruction-tf'];
apiRequest.onreadystatechange = function () {
if (apiRequest.readyState === 4) {
// przetwarzanie danych z odpowiedzi API pytania
// uwaga. można dodać sprawdzanie czy dane są poprawne
let apiResponse = JSON.parse(apiRequest.responseText).pool;
// apiResponseRaw na ten moment jest potrzebne tylko do debugowania
let apiResponseRaw = JSON.parse(apiRequest.responseText);
var questionData = document.getElementsByTagName("app-exercise-loader")[examIndex].getElementsByClassName("ng-star-inserted");
for (var questionElements = 0; questionElements < questionData.length; questionElements++) {
// sprawdzanie czy questionData[questionElements].className zawiera którykolwiek element z questionClassNames
const questionDataContainsClass = questionClassNames.some(element => {
if (questionData[questionElements].className.indexOf(element) !== -1) {
return true;
}
return false;
});
if (!questionDataContainsClass) {
continue;
}
hackAnswersUsingJSON(apiResponse, apiResponseRaw, questionData, questionElements);
}
}
};
apiRequest.send();
});
}
document.getElementsByClassName("exam-exercise")[0].click();
}
}
for (let index = 0; index < document.getElementsByClassName("new-exam").length; index++) {
const element = document.getElementsByClassName("new-exam")[index];
try {
const element2 = document.getElementsByClassName("retry-exam")[index];
element2.addEventListener("click", function () {
examsHackStart();
});
} catch (e) {
}
element.addEventListener("click", function () {
examsHackStart();
});
}
}
startCheat();