forked from adubov1/khanacademy_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
khanacademy_revealer.user.js
248 lines (214 loc) · 9 KB
/
khanacademy_revealer.user.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
// ==UserScript==
// @name Khan Answers
// @version 1.8
// @description ur welcome cheater
// @author Alex Dubov (github@adubov1) / zgredinzyyy (github@zgredinzyyy)
// @match https://pl.khanacademy.org/*
// @match https://www.khanacademy.org/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
window.loaded = false;
window.khan_debug = {
enable: false,
latex: true,
graph: true,
fetch_data: true,
answer: true,
json: true,
prevent_printing: false
}
class Answer {
constructor(answer, type) {
this.body = answer;
this.type = type;
}
get isMultiChoice() {
return this.type == "multiple_choice";
}
get isFreeResponse() {
return this.type == "free_response";
}
get isExpression() {
return this.type == "expression";
}
get isDropdown() {
return this.type == "dropdown";
}
log() {
const answer = this.body;
if (window.khan_debug.enable && window.khan_debug.answer) {console.group("Answer Body:"); console.log(answer); console.groupEnd();};
const style = "color: coral; -webkit-text-stroke: .5px black; font-size:24px; font-weight:bold;";
const consoleCode = "background: #EEEEF6;" +
"border: 1px solid #B2B0C1;" +
"border-radius: 7px;" +
"padding: 2px 8px 3px;" +
"color: #5F5F5F;" +
"line-height: 22px;" +
"box-shadow: 0px 0px 1px 1px rgba(178,176,193,0.3)";
answer.map(ans => {
if (typeof ans == "string") {
if (ans.includes("web+graphie")) {
this.body[this.body.indexOf(ans)] = "";
this.printImage(ans);
} else {
answer[answer.indexOf(ans)] = ans.replaceAll("$", "");
}
}
});
if (window.khan_debug.enable && window.khan_debug.prevent_printing) return;
let text = (String.raw`${answer.join("\n")}`);
if (text.includes("\\") || !isLetter(text[0])) {
this.printLatex(text.trim());
return
}
if (text) {
console.log(`%c${text.trim()} `, consoleCode);
}
else {
console.log(`%cNone of the above. `, consoleCode);
}
}
printImage(ans) {
const url = ans.replace("![](web+graphie", "https").replace(")", ".svg");
if (window.khan_debug.enable && window.khan_debug.graph) {console.group("Graph URL:"); console.log(url); console.groupEnd();};
var image = new Image();
image.onload = function() {
var style = [
'font-size: 1px;',
'line-height: ' + this.height % 2 + 'px;',
'padding: ' + this.height * .5 + 'px ' + this.width * .5 + 'px;',
'background-size: ' + this.width + 'px ' + this.height + 'px;',
'background: url('+ url +') no-repeat;'
].join(' ');
console.log('%c ', style);
};
image.src = url;
}
printLatex(url) {
const mathurl = "https://math.now.sh?from=" + urlencode(String.raw`${url}`);
if (window.khan_debug.enable && window.khan_debug.latex) {console.group("LATEX URL:"); console.log(mathurl); console.groupEnd();};;
var image = new Image();
image.onload = function() {
var style = [
'font-size: 1px;',
'line-height: ' + this.height % 2 + 'px;',
'padding: ' + this.height * .5 + 'px ' + this.width * .5 + 'px;',
'margin: ' + this.height * .1 + 'px ' + this.width * .1 + 'px;',
'background-size: ' + this.width + 'px ' + this.height + 'px;',
'background: url('+ mathurl +') no-repeat;',
].join(' ');
console.log('%c ', style);
};
image.src = mathurl;
}
}
const originalFetch = window.fetch;
window.fetch = function () {
return originalFetch.apply(this, arguments).then((res) => {
if (res.url.includes("/getAssessmentItem")) {
const clone = res.clone();
clone.json().then(json => {
let item, question;
try {
item = json.data.assessmentItem.item.itemData;
if (window.khan_debug.enable && window.khan_debug.json) {console.group("JSON Response:"); console.log(JSON.parse(item)); console.groupEnd();};
question = JSON.parse(item).question;
} catch {
let errorIteration = () => { return localStorage.getItem("error_iter") || 0; }
localStorage.setItem("error_iter", errorIteration() + 1);
if (errorIteration() < 4) {
return location.reload();
} else {
return console.log("%c An error occurred", "color: red; font-weight: bolder; font-size: 20px;");
}
}
if (!question) return;
Object.keys(question.widgets).map(widgetName => {
switch (widgetName.split(" ")[0]) {
case "numeric-input":
return freeResponseAnswerFrom(question).log();
case "radio":
return multipleChoiceAnswerFrom(question).log();
case "expression":
return expressionAnswerFrom(question).log();
case "dropdown":
return dropdownAnswerFrom(question).log();
}
});
});
}
if (!window.loaded) {
console.clear();
console.log("%c Khan Answers ", "color: #e74c3c; -webkit-text-stroke: .5px black; font-size:40px; font-weight:bolder; padding: .2rem;");
console.log("%cOriginally created by Alex Dubov (@adubov1)", "color: white; font-size:15px;");
console.log("%cContinued by @zgredinzyyy", "color: white; font-size:15px;");
window.loaded = true;
}
return res;
})
}
function freeResponseAnswerFrom(question) {
const answer = Object.values(question.widgets).map((widget) => {
if (widget.options?.answers) {
return widget.options.answers.map(answer => {
if (answer.status == "correct") {
return answer.value;
}
});
}
}).flat().filter((val) => { return val !== undefined; });
return new Answer(answer, "free_response");
}
function multipleChoiceAnswerFrom(question) {
const answer = Object.values(question.widgets).map((widget) => {
if (widget.options?.choices) {
return widget.options.choices.map(choice => {
if (choice.correct) {
return choice.content;
}
});
}
}).flat().filter((val) => { return val !== undefined; });
return new Answer(answer, "multiple_choice");
}
function expressionAnswerFrom(question) {
const answer = Object.values(question.widgets).map((widget) => {
if (widget.options?.answerForms) {
return widget.options.answerForms.map(answer => {
if (Object.values(answer).includes("correct")) {
return answer.value;
}
});
}
}).flat();
return new Answer(answer, "expression");
}
function dropdownAnswerFrom(question) {
const answer = Object.values(question.widgets).map((widget) => {
if (widget.options?.choices) {
return widget.options.choices.map(choice => {
if (choice.correct) {
return choice.content;
}
});
}
}).flat();
return new Answer(answer, "dropdown");
}
function urlencode(str) {
str = (str + '').toString();
return encodeURIComponent(str)
.replaceAll('!', '%21')
.replaceAll('\'', '%5C')
.replaceAll('(', '%28')
.replaceAll(')', '%29')
.replaceAll('*', '%2A')
.replaceAll('+', '%5C;')
.replaceAll('%20', '%5C;'); // Adds LaTeX spacing
}
function isLetter(c) {
return c.toLowerCase() != c.toUpperCase();
}
})();