-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathselectCorrectAnswers.js
153 lines (116 loc) · 7.08 KB
/
selectCorrectAnswers.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
// @author Rob W <http://stackoverflow.com/users/938089/rob-w>
// Demo: var serialized_html = DOMtoString(document);
// Rest of the code was built off of his demo
var questionsArr = [];
var questionObjs = []
var answersArr = [];
function DOMtoString(document_root) {
// Gets all of the questions from the quiz
if(document_root.getElementsByClassName("question-wrapper").length > 0){
questionsArr = document_root.getElementsByClassName("question-wrapper");
// Creates an Obj of all the questions and their answers
for(i = 0; i < questionsArr.length; i++){
var questionOptions = []
question = questionsArr[i].getElementsByClassName("question-question")[0].getElementsByClassName("question-title")[0].innerHTML;
question = question.replace("<p>", "");
question = question.replace("</p>", "");
if(questionsArr[i].getElementsByClassName("radios-wrapper").length > 0){
questionOptions = questionsArr[i].getElementsByClassName("radios-wrapper")[0].children;
}
//console.log(questionsArr[i].getElementsByClassName("radios-wrapper")[0]);
questionObjs.push({
"question": question,
"answers": questionOptions
})
}
}
console.log(questionObjs)
// Gets the locally stored correct answers (see getCorrectAnswer.js for what that is)
chrome.storage.local.get(['correctAnswersArr'], function(result) {
answersArr = result.correctAnswersArr;
console.log(answersArr);
// Checks for the questions on the correct quiz that match the questions from the previous attempt
for(i = 0; i < questionObjs.length; i++){
question = questionObjs[i].question;
for(z = 0; z < answersArr.length; z++){
// checks if questions matched
if(question == answersArr[z].question){
TFQuestion = false;
if(questionObjs[i].answers[0] != null){
var questionsHTML = questionObjs[i].answers[0].getElementsByClassName("form-radios-table sCommonRadiosProcessed")[0];
if(questionsHTML == undefined){
if(questionObjs[i].answers[0].getElementsByClassName("form-radio")[0] == null){
questionsHTML = questionObjs[i].answers[0].getElementsByClassName("form-checkboxes-table sCommonCheckboxesProcessed")[0].tBodies[0].rows;
}
else{
questionsHTML = questionObjs[i].answers[0].getElementsByClassName("form-radio-title");
TFQuestion = true;
}
}else{
questionsHTML = questionObjs[i].answers[0].getElementsByClassName("form-radios-table sCommonRadiosProcessed")[0].tBodies[0].rows;
}
var matchedAnswers = 0;
for(c = 0; c < questionsHTML.length; c++){
var answerOption;
var answerOptionRadioButton;
for(v = 0; v < answersArr[z].answers.length; v++){
// Answer Choice Content and Answer Choice Radio Button
if(TFQuestion){
answerOption = questionsHTML[c].innerHTML;
answerOptionRadioButton = questionsHTML[c].parentNode.querySelector("input");
//console.log(answerOption);
//console.log(answerOptionRadioButton);
}else{
answerOption = questionsHTML[c].cells[1].querySelector("div").innerHTML;
answerOptionRadioButton = questionsHTML[c].cells[0].querySelector("div").querySelector("label").querySelector("input");
}
// Removes any HTML tags that may be present in the Answer choice content
// answerOption = answerOption.replace("<p>", "");
// answerOption = answerOption.replace("</p>", "");
answerOption = answerOption.replace("<p>", "");
answerOption = answerOption.replace("</p>", "");
answerOption = answerOption.replace(/ /g, "");
console.log("1:",answerOption, "2:",answersArr[z].answers[v], "3:", answerOption == answersArr[z].answers[v])
if(answerOption == answersArr[z].answers[v]){
matchedAnswers++;
}
}
}
if(matchedAnswers == answersArr[z].answers.length){
for(c = 0; c < questionsHTML.length; c++){
// Answer Choice Content and Answer Choice Radio Button
if(TFQuestion){
answerOption = questionsHTML[c].innerHTML;
answerOptionRadioButton = questionsHTML[c].parentNode.querySelector("input");
//console.log(answerOption);
//console.log(answerOptionRadioButton);
}else{
answerOption = questionsHTML[c].cells[1].querySelector("div").innerHTML;
answerOptionRadioButton = questionsHTML[c].cells[0].querySelector("div").querySelector("label").querySelector("input");
}
// Removes any HTML tags that may be present in the Answer choice content
// answerOption = answerOption.replace("<p>", "");
// answerOption = answerOption.replace("</p>", "");
answerOption = answerOption.replace("<p>", "");
answerOption = answerOption.replace("</p>", "");
answerOption = answerOption.replace(/ /g, "");
//console.log(answerOption);
//answerOption = answerOption.slice(answerOption.indexOf(".") + 1);
// Selects the correct answers
for(a = 0; a < answersArr[z].answers.length; a++){
if(answerOption == answersArr[z].correct_answers[a]){
answerOptionRadioButton.checked = true;
}
}
}
}
}
}
}
}
});
}
chrome.runtime.sendMessage({
action: "getSource",
source: DOMtoString(document)
});