-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
621 lines (521 loc) · 14.8 KB
/
script.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
(() => {
const QuestionType = {
BLANKS: "blanks",
CHOICES: "choices",
DROPDOWNS: "dropdowns",
UNKNOWN: "unknown"
};
/* eslint-disable camelcase */
const CLASS_QUESTION_TYPE = {
fill_in_multiple_blanks_question: QuestionType.BLANKS,
short_answer_question: QuestionType.BLANKS,
multiple_answers_question: QuestionType.CHOICES,
multiple_choice_question: QuestionType.CHOICES,
true_false_question: QuestionType.CHOICES,
matching_question: QuestionType.DROPDOWNS
};
/* eslint-enable camelcase */
// Eg question_26789
const REGEX_QUESTION_ID = /^question_(?<id>\d+)$/u;
// Eg answer-7050 for results, or question_26789_answer_7050 for ongoing
const REGEX_ANSWER_ID = /^(?:question_\d+_)?answer[-_](?<id>\d+)$/u;
const REGEX_MARKS = /^(?<actualMarks>\d+(?:\.\d+)?) \/ (?<maxMarks>\d+(?:\.\d+)?) pts$/u;
const LOCAL_STORAGE_KEY = "CanvasTransfer";
class QuestionInfo {
constructor(
id,
type,
answerInfos,
element,
actualMarks,
maxMarks
) {
Object.assign(
this,
{
id,
type,
// Processing must return all answer elements to facilitate importing
answerInfos,
element,
actualMarks,
maxMarks
}
);
}
export() {
return {
id: this.id,
type: this.type,
answerInfos: this.answerInfos.map((answerInfo) => answerInfo.export()),
actualMarks: this.actualMarks,
maxMarks: this.maxMarks
};
}
}
class AnswerInfo {
constructor(element) {
Object.assign(
this,
{ element }
);
}
}
class BlanksAnswerInfo extends AnswerInfo {
constructor(
input,
text
) {
super(input);
Object.assign(
this,
{ text }
);
}
export() {
return {
text: this.text
};
}
}
class ChoicesAnswerInfo extends AnswerInfo {
constructor(
input,
id,
checked
) {
super(input);
Object.assign(
this,
{
id,
checked
}
);
}
export() {
return {
id: this.id,
checked: this.checked
};
}
}
class DropdownsAnswerInfo extends AnswerInfo {
constructor(
select,
answerId,
text
) {
super(select);
Object.assign(
this,
{
id: answerId,
text
}
);
}
export() {
return {
id: this.id,
text: this.text
};
}
}
function l(content, group = false) {
let consoleFunction = (!group) ? console.log : console.group;
consoleFunction(
// Skip instanceof check for type object + class String from new String()s
(typeof content !== "string")
? content
: `>>> ${content}`
);
}
function w(content) {
console.warn(
(typeof content !== "string")
? content
: `[!] ${content}!`
);
}
function e(content) {
console.error(
(typeof content !== "string")
? content
: `ERR ${content}!`
);
}
function d(content) {
console.debug(
(typeof content !== "string")
? content
: `*** ${content}`
);
}
function extractRegexId(element, regex) {
// Try ID
let elementId = element.id;
if (elementId === "") {
// Try for attribute instead, eg labels
elementId = element.htmlFor;
}
if (elementId === "") return -1;
let result = regex.exec(elementId);
if (result === null) return -1;
return parseInt(result.groups.id);
}
function triggerUpdate(element) {
// Event has to be change event, and must bubble, in order to:
// • Trigger autosave
// • Update question list status (eg icons)
// • Prevent unanswered question alert (if all filled)
element.dispatchEvent(
new Event(
"change",
{
bubbles: true
}
)
);
}
class QuestionManager {
cannotProceed = false;
questionInfos = [];
constructor(scan) {
let successCount = 0;
let questionCount = 0;
for (let question of scan.questions) {
questionCount++;
l(`⚙️ Processing page's Q${questionCount}...`, true);
// Process question ID
let questionId = extractRegexId(question, REGEX_QUESTION_ID);
if (questionId === -1) {
e("Unable to extract ID from question");
console.groupEnd();
continue;
}
// Process question type
let questionType = this.#processQuestionType(question);
if (questionType === QuestionType.UNKNOWN) {
e("⚠️ This question type isn't supported");
// DOMTokenList to array for cleaner formatting
d([...question.classList]);
console.groupEnd();
continue;
}
// Process answer infos
let answerInfos = null;
switch (questionType) {
case QuestionType.BLANKS:
answerInfos = this.#processAnswerBlanks(question);
break;
case QuestionType.CHOICES:
answerInfos = this.#processAnswerChoices(question);
break;
case QuestionType.DROPDOWNS:
answerInfos = this.#processAnswerDropdowns(question);
break;
}
if (answerInfos === null) {
// Rely on processing methods above to give error feedback
console.groupEnd();
continue;
}
// Process marks, if available
let actualMarks = -1;
let maxMarks = -1;
let marksHolder = question.querySelector("div.user_points");
if (marksHolder !== null) {
// Don't use .textContent as it returns everything, including whitespace
let marksText = marksHolder.innerText;
let result = REGEX_MARKS.exec(marksText);
if (result !== null) {
actualMarks = parseFloat(result.groups.actualMarks);
maxMarks = parseFloat(result.groups.maxMarks);
}
}
let questionInfo = new QuestionInfo(
questionId,
questionType,
answerInfos,
question,
actualMarks,
maxMarks
);
d(questionInfo);
this.questionInfos.push(questionInfo);
successCount++;
console.groupEnd();
}
l(`📦 Processed ${successCount}/${questionCount} questions`);
d(this.questionInfos);
}
#processQuestionType(question) {
for (let [className, questionType] of Object.entries(CLASS_QUESTION_TYPE)) {
if (question.classList.contains(className)) return questionType;
}
return QuestionType.UNKNOWN;
}
#processAnswerBlanks(question) {
let inputs = question.querySelectorAll("input[type=text]");
if (inputs.length === 0) {
e("No inputs found in question");
return null;
}
let answerInfos = [];
for (let input of inputs) {
// Process text
let text = input.value;
let answerInfo = new BlanksAnswerInfo(
input,
text
);
answerInfos.push(answerInfo);
}
return answerInfos;
}
#processAnswerChoices(question) {
let inputs = question.querySelectorAll("input[type=radio], input[type=checkbox]");
if (inputs.length === 0) {
e("No inputs found in question");
return null;
}
let answerInfos = [];
for (let input of inputs) {
// Process answer ID
let answerId = extractRegexId(input, REGEX_ANSWER_ID);
if (answerId === -1) {
e("Unable to extract answer ID from input");
console.groupEnd();
continue;
}
// Process checked status
let { checked } = input;
let answerInfo = new ChoicesAnswerInfo(
input,
answerId,
checked
);
answerInfos.push(answerInfo);
}
return answerInfos;
}
#processAnswerDropdowns(question) {
// Assumption: All expected elements are present and in the right quantities
// Exclude:
// • Extra "answer" divs that just contain the correct answer when viewing results
// • The "answer" div within the above type of full-opacity answer div
let answers = question.querySelectorAll("div.answer:not(.full-opacity, .full-opacity *)");
let answerInfos = [];
for (let answer of answers) {
let select = answer.querySelector("select");
let label = answer.querySelector("label");
let answerId = extractRegexId(label, REGEX_ANSWER_ID);
// We take raw text instead of IDs, as the non-displayed ID could be the provided
// solution instead of the user's selection (which may be wrong)
let option = select.options[select.selectedIndex];
let text = option.textContent;
let answerInfo = new DropdownsAnswerInfo(
select,
answerId,
text
);
answerInfos.push(answerInfo);
}
return answerInfos;
}
store() {
let data = this.questionInfos.map((questionInfo) => questionInfo.export());
d(data);
localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify(data)
);
}
}
class Scan {
cannotProceed = false;
extractorMode = true;
questions = [];
constructor() {
let questionsHolder = document.querySelector("div#questions");
if (questionsHolder === null) {
e("Can neither extract nor import (no questions holder found in document)");
this.cannotProceed = true;
return;
}
if (questionsHolder.classList.contains("assessment_results")) {
// Quiz results
l("📈 Using extractor mode");
this.extractorMode = true;
} else if (questionsHolder.classList.contains("assessing")) {
// Ongoing attempt
l("🪄 Using importer mode");
this.extractorMode = false;
} else {
e("Can neither extract nor import (unknown kind of questions holder)");
this.cannotProceed = true;
return;
}
// Exclude:
// • Questions only for displaying text, which can't be answered and thus aren't real questions
// • Questions that are nested within another question as text
let nodeList = questionsHolder.querySelectorAll("div.question:not(.text_only_question, div.question *)");
// Convert to array for array methods
this.questions = [...nodeList];
}
process() {
return new QuestionManager(this);
}
}
class StoredData {
#questionInfos = [];
cannotProceed = false;
data = null;
constructor(questionManager) {
// Clone for #importOne() to progressively remove overwritten questions
this.#questionInfos = [...questionManager.questionInfos];
let rawData = localStorage.getItem(LOCAL_STORAGE_KEY);
if (rawData === null) {
e("⛔ Did not find any stored answers to retrieve. Run this script on your quiz results (not an ongoing attempt) to extract those answers first");
this.cannotProceed = true;
return;
}
try {
this.data = JSON.parse(rawData);
} catch (syntaxError) {
e("Stored data unreadable");
e(syntaxError);
this.cannotProceed = true;
return;
}
l("✨ Stored answers retrieved");
d(this.data);
}
import() {
let successCount = 0;
let dataCount = 0;
for (let questionData of this.data) {
dataCount++;
l(`🔮 Importing stored answer data #${dataCount}...`, true);
try {
let success = this.#importOne(questionData);
if (success) successCount++;
} catch (error) {
e("⛔ Your stored answer is in a different format. You may be running a newer version of the script on outdated data - try re-extracting your answers");
e(error);
console.groupEnd();
continue;
}
console.groupEnd();
}
l(`☁️ Imported ${successCount}/${dataCount} answer data`);
}
// Question data is from storage with answers, question info is fresh from page with
// elements
#importOne(questionData) {
let index = this.#questionInfos.findIndex(
(questionInfo) => questionInfo.id === questionData.id
);
if (index === -1) {
e("⚠️ Your stored answer doesn't match any of this quiz's processed questions");
return false;
}
let questionInfo = this.#questionInfos[index];
switch (questionData.type) {
case QuestionType.BLANKS:
this.#importAnswerBlanks(questionInfo, questionData);
break;
case QuestionType.CHOICES:
this.#importAnswerChoices(questionInfo, questionData);
break;
case QuestionType.DROPDOWNS:
this.#importAnswerDropdowns(questionInfo, questionData);
break;
default:
e("Stored question type not supported");
return false;
}
this.#highlightQuestion(questionInfo, questionData);
this.#questionInfos.splice(index, 1);
return true;
}
#importAnswerBlanks(questionInfo, questionData) {
// Assumption: Stored answer data count matches that of QuestionInfo#AnswerInfo
for (let i = 0; i < questionInfo.answerInfos.length; i++) {
let blanksAnswerInfo = questionInfo.answerInfos[i];
let blanksAnswerData = questionData.answerInfos[i];
blanksAnswerInfo.element.value = blanksAnswerData.text;
triggerUpdate(blanksAnswerInfo.element);
}
}
#importAnswerChoices(questionInfo, questionData) {
// Clone for removing imported answer data
let choicesAnswerDatas = [...questionData.answerInfos];
for (let choicesAnswerInfo of questionInfo.answerInfos) {
// For each element, try to find data of the same answer ID. If found, overwrite
// checked status (be it checking or unchecking)
let index = choicesAnswerDatas.findIndex(
(answerData) => answerData.id === choicesAnswerInfo.id
);
if (index === -1) {
e("Answer info is missing corresponding answer data");
continue;
}
let choicesAnswerData = choicesAnswerDatas[index];
choicesAnswerInfo.element.checked = choicesAnswerData.checked;
triggerUpdate(choicesAnswerInfo.element);
choicesAnswerDatas.splice(index, 1);
}
}
#importAnswerDropdowns(questionInfo, questionData) {
// Assumption: All expected elements are present and in the right quantities
let dropdownsAnswerDatas = questionData.answerInfos;
for (let dropdownsAnswerInfo of questionInfo.answerInfos) {
let index = dropdownsAnswerDatas.findIndex(
(answerData) => answerData.id === dropdownsAnswerInfo.id
);
let dropdownsAnswerData = dropdownsAnswerDatas[index];
// Find the option with text matching the data
let select = dropdownsAnswerInfo.element;
let matchingOption = [...select.options].find(
(option) => option.textContent === dropdownsAnswerData.text
);
matchingOption.selected = true;
triggerUpdate(select);
}
}
#highlightQuestion(questionInfo, questionData) {
if (questionData.maxMarks === -1) return;
let header = questionInfo.element.querySelector("div.header");
if (header === null) {
w("No header found in question");
return;
}
let pointsHolder = header.querySelector("span.question_points_holder");
if (pointsHolder === null) {
w("No points holder found in header");
return;
}
let isFullMarks = questionData.actualMarks === questionData.maxMarks;
let rgb = (isFullMarks)
? "85 255 170" // Greenish blue
: "255 170 0"; // Orange
header.style["background-color"] = `rgb(${rgb} / 20%)`;
pointsHolder.textContent = `☁️ ${questionData.actualMarks} / ${questionData.maxMarks} pts`;
}
}
let scan = new Scan();
if (scan.cannotProceed) return;
let questionManager = scan.process();
if (questionManager.cannotProceed) return;
if (scan.extractorMode) {
questionManager.store();
l("✅ Your answers have been extracted & stored. Run this script again on an ongoing quiz attempt to import them");
} else {
let storedData = new StoredData(questionManager);
if (storedData.cannotProceed) return;
storedData.import();
l("✅ Your answers have been retrieved & imported. Matching questions have been overwritten");
}
})();