-
Notifications
You must be signed in to change notification settings - Fork 2
/
getCorrectAnswers.js
125 lines (87 loc) · 4.88 KB
/
getCorrectAnswers.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
// @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 correctAnswers = []
console.log("here")
function DOMtoString(document_root) {
// Stores all of the quiz questions
if(document_root.getElementsByClassName("question-view").length > 0){
questionsArr = document_root.getElementsByClassName("question-view");
}
// Handles all of the quiz questions that are correct
for(i = 0; i < questionsArr.length; i++){
var allAnswerOptions = [];
// gets only the correct answer(s) from a question
correct_answers = questionsArr[i].children[1].getElementsByClassName("selected correct");
// gets all of the answers choices from a question
allAnswerOptionsHTML = questionsArr[i].getElementsByClassName("option-span");
//console.log(allAnswerOptionsHTML);
// Edits the answers from an HTML element to a String
for(t = 0; t < allAnswerOptionsHTML.length; t++){
allAnswerOptionsHTML[t] = allAnswerOptionsHTML[t].innerHTML;
allAnswerOptions.push(allAnswerOptionsHTML[t].innerHTML);
allAnswerOptions[t] = allAnswerOptions[t].replace("<p>", "");
allAnswerOptions[t] = allAnswerOptions[t].replace("</p>", "");
allAnswerOptions[t] = allAnswerOptions[t].replace(/ /g, "");
allAnswerOptions[t] = allAnswerOptions[t].slice(allAnswerOptions[t].indexOf(".") + 1);
}
if(correct_answers[0] != undefined){
// question string edits
question = questionsArr[i].children[0].innerHTML;
question = question.replace("<p>", "");
question = question.replace("</p>", "");
//console.log(correct_answers);
finalAnswers = [];
//correct answer(s) string edits
for(z = 0; z < correct_answers.length; z++){
//console.log(questionsArr[i]);
if(questionsArr[i].getElementsByClassName("ordering-review")[0] != null){
}
else if(questionsArr[i].getElementsByClassName("matching-review")[0] != null){
}
else if(questionsArr[i].getElementsByClassName("answer s-rte")[0] != null){
}
else if(questionsArr[i].getElementsByClassName("legacy-true-false-question")[0] != null){
finalAnswers.push(correct_answers[z].children[1].innerHTML);
// finalAnswers[z] = finalAnswers[z].replace("<p>", "");
// finalAnswers[z] = finalAnswers[z].replace("</p>", "");
// finalAnswers[z] = finalAnswers[z].trimEnd();
// //console.log(finalAnswers[z]);
// finalAnswers[z] = finalAnswers[z].slice(finalAnswers[z].indexOf(".") + 1);
// finalAnswers[z] = finalAnswers[z].trim();
finalAnswers[z] = finalAnswers[z].replace("<p>", "");
finalAnswers[z] = finalAnswers[z].replace("</p>", "");
finalAnswers[z] = finalAnswers[z].replace(/ /g, "");
finalAnswers[z] = finalAnswers[z].slice(finalAnswers[z].indexOf(".") + 1);
correctAnswers.push({
"question": question,
"answers": allAnswerOptions,
"correct_answers": finalAnswers
})
}else if(questionsArr[i].getElementsByClassName("legacy-multiple-choice-question")[0] != null){
//console.log(correct_answers[z].getElementsByClassName("legacy-multiple-choice-question")[0].innerHTML);
finalAnswers.push(correct_answers[z].getElementsByClassName("legacy-multiple-choice-question")[0].innerHTML);
finalAnswers[z] = finalAnswers[z].replace("<p>", "");
finalAnswers[z] = finalAnswers[z].replace("</p>", "");
finalAnswers[z] = finalAnswers[z].replace(/ /g, "");
finalAnswers[z] = finalAnswers[z].slice(finalAnswers[z].indexOf(".") + 1);
console.log(finalAnswers)
correctAnswers.push({
"question": question,
"answers": allAnswerOptions,
"correct_answers": finalAnswers
})
}
}
}
}
// Stores correct answers to local storage
chrome.storage.local.set({correctAnswersArr: correctAnswers}, function() {
//console.log('correctAnswersArr is set to ' + correctAnswers);
});
}
chrome.runtime.sendMessage({
action: "getSource",
source: DOMtoString(document)
});