-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizBot.user.js
More file actions
86 lines (73 loc) · 2.92 KB
/
QuizBot.user.js
File metadata and controls
86 lines (73 loc) · 2.92 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
// ==UserScript==
// @name Canvas QuizBot
// @namespace Violentmonkey Scripts
// @include https://*instructure.com/courses/*/quizzes/*
// @grant none
// @version 1.0
// @author Daniel Crooks
// @description Store correct answers and autofill them when retaking the quiz
// ==/UserScript==
function storeAnswers() {
const questions = document.querySelectorAll(".question_text.user_content.enhanced");
const answers = document.querySelectorAll(".correct_answer");
questions.forEach((question, i) => {
let answer;
if (correctAnswersHidden()) { // FIXME: calling each iteration; only need one
if (isCorrect(i)) {
answer = document.querySelectorAll(".selected_answer > div.select_answer > label > div.answer_text")[i].innerText;
if (answer == 'No answer text provided.') {
answer = document.querySelectorAll(".selected_answer > div.select_answer > label > div.answer_html")[i].innerText;
}
}
} else {
answer = answers[i].querySelector(".answer_text").innerText;
if (answer == 'No answer text provided.') {
answer = answers[i].querySelector(".answer_html").innerText;
}
}
question = question.innerText;
console.log({question: answer});
localStorage.setItem(question, answer);
});
}
function correctAnswersHidden() {
const element = document.querySelector("div.alert > span");
if (element && element.innerText == 'Correct answers are hidden.') {
return true;
} else {
return false;
}
}
function isCorrect(i) {
return document.querySelectorAll("span.name.question_name")[i].querySelector(".answer_arrow.incorrect") == null;
}
function fillAnswers() {
const questions = document.querySelectorAll(".question_text.user_content.enhanced");
questions.forEach((question, i) => {
const correctAnswer = localStorage.getItem(question.innerText);
const answerOptions = document.querySelectorAll(".answers")[i].querySelectorAll(".answer_label");
answerOptions.forEach((answerOption, j) => {
const option = answerOption.innerText;
if (option === correctAnswer) {
document.querySelectorAll(".answers")[i].querySelectorAll(".answer_input")[j].click();
}
});
});
}
function formatMathJaxtoText() {
const elements = document.querySelectorAll("span.math_equation_latex.fade-in-equation");
for (let i = 0; i < elements.length; i++) {
let mathTex = elements[i].querySelector("script").innerText;
elements[i].innerText = mathTex;
}
}
const observer = new MutationObserver(function(mutationsList, observer) {
const elements = document.querySelectorAll("span.math_equation_latex.fade-in-equation");
if (elements.length > 0) {
observer.disconnect();
formatMathJaxtoText();
try { storeAnswers(); } catch(e) { console.error(e) };
try { fillAnswers(); } catch(e) { console.error(e) };
}
});
observer.observe(document.documentElement, { childList: true, subtree: true });