-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolverClasstime.js
More file actions
94 lines (76 loc) · 3.1 KB
/
solverClasstime.js
File metadata and controls
94 lines (76 loc) · 3.1 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
87
88
89
90
91
92
93
94
function handleResponse(response) {
if (type == "single_choice_question") {
var correct_answer = answers_HTML[response-1];
correct_answer.click();
}
else if (type == "multiple_choice_question") {
const responseArray = response.split(", ").map(Number);
for (let i=0; i<responseArray.length; i++) {
var correct_answer = answers_HTML[responseArray[i]-1];
correct_answer.click();
}
}
}
function handleError(error) {
console.log(`Background error: ${error}`);
}
var answers_HTML = [];
var answers = [];
var type = "";
// Function to attach the event listener
function attachEventListener() {
const mainContent = document.getElementById("mainContent");
if (!mainContent) {
return false;
}
mainContent.addEventListener("click", async function(event) {
const clickedTab = event.target.closest("._3e864da4cfc4b0de376587e37e8abb12");
if (clickedTab) {
// Wait a short time for the content to load fully after clicking the tab
await new Promise(r => setTimeout(r, 300));
var answerWrapper = document.getElementsByClassName("_8043fa3b8fc08be90f43195ad3158af5");
if (answerWrapper && answerWrapper.length > 0) {
answers_HTML = Array.from(answerWrapper[0].children).map(e => e.getElementsByClassName("MuiFormControlLabel-root")[0]);
var answerContext = answers_HTML.map(e => e.innerText);
var questionContext = clickedTab.firstChild.firstChild.innerText;
// Get additional description if available
try {
const descriptionElement = document.getElementsByClassName("c4489eba3f80625a30f7805df37310ed")[0];
if (descriptionElement && descriptionElement.innerText.trim() !== "") {
// Add the additional description to the question context
questionContext += "\n" + descriptionElement.innerText.trim();
}
} catch (err) {
console.log("Error getting additional description:", err);
}
// Check for checkboxes to determine if it's multiple choice
let isMultipleChoice = false;
if (answers_HTML.length > 0) {
const checkboxes = answers_HTML[0].querySelectorAll(".MuiCheckbox-root");
isMultipleChoice = checkboxes.length > 0;
}
// Set the appropriate question type
type = isMultipleChoice ? "multiple_choice_question" : "single_choice_question";
chrome.runtime.sendMessage({
question: questionContext,
answers: answerContext,
type: type
}, handleResponse);
}
}
});
return true;
}
// Try to attach the listener immediately first
if (!attachEventListener()) {
// If it fails, set up a MutationObserver to watch for the mainContent element
const observer = new MutationObserver(function(mutations, obs) {
const mainContent = document.getElementById("mainContent");
if (mainContent) {
attachEventListener();
obs.disconnect(); // Stop observing once we've attached the listener
}
});
// Start observing the document with the configured parameters
observer.observe(document, { childList: true, subtree: true });
}