-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassignmentHandler.js
More file actions
145 lines (118 loc) · 5.22 KB
/
assignmentHandler.js
File metadata and controls
145 lines (118 loc) · 5.22 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
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
// Assignment Handler - Automatically fills assignment questions based on user-provided JSON
(function() {
'use strict';
console.log('[AssignmentHandler] Script loaded');
// Store the user's answer map
let userAnswersMap = null;
/**
* Fills in the correct radio button for a given quiz question based on a map of answers.
* @param {Object} answersMap - A map where keys are question texts and values are answer texts.
*/
function fillAssignmentAnswers(answersMap) {
console.log('[AssignmentHandler] Starting assignment answer filling...');
console.log('[AssignmentHandler] Answer map:', answersMap);
// Get all quiz question containers on the page
const questionContainers = document.querySelectorAll('.que.multichoice');
if (questionContainers.length === 0) {
console.warn('[AssignmentHandler] No question containers found. Are you on an assignment page?');
return {
success: false,
error: 'No question containers found. Make sure you are on an assignment page with questions.',
questionsProcessed: 0
};
}
console.log(`[AssignmentHandler] Found ${questionContainers.length} question container(s)`);
let questionsProcessed = 0;
let questionsNotFound = [];
// Iterate over each question container
questionContainers.forEach((container, index) => {
// Find the question text element (adjust selector if needed for other quiz layouts)
const questionTextElement = container.querySelector('.qtext');
if (!questionTextElement) {
console.warn('[AssignmentHandler] Could not find question text in container', index);
return;
}
// Get the inner text and trim it for comparison
const questionText = questionTextElement.textContent.trim();
// Check if we have an answer for this question
const correctAnswerText = answersMap[questionText];
if (correctAnswerText) {
console.log(`[AssignmentHandler] Found answer for question: "${questionText}"`);
console.log(`[AssignmentHandler] Target answer text: "${correctAnswerText}"`);
// Find all answer labels/divs within the current question container
const answerLabels = container.querySelectorAll('[data-region="answer-label"]');
let found = false;
answerLabels.forEach(label => {
// Get the text content of the entire label
const labelText = label.textContent.trim();
// Check if the label's text includes the correct answer text
if (labelText.includes(correctAnswerText)) {
// We found the correct answer label. Now find the associated radio input.
const radioId = label.id.replace('_label', '');
const radioInput = document.getElementById(radioId);
if (radioInput && radioInput.type === 'radio') {
// Check the radio button
radioInput.checked = true;
// Fire a change event to let the quiz interface know the selection changed
const event = new Event('change', { bubbles: true });
radioInput.dispatchEvent(event);
console.log(`[AssignmentHandler] ✅ Successfully checked radio button for: ${correctAnswerText}`);
found = true;
questionsProcessed++;
}
}
});
if (!found) {
console.warn(`[AssignmentHandler] ❌ Could not find answer choice: "${correctAnswerText}"`);
questionsNotFound.push({
question: questionText,
expectedAnswer: correctAnswerText
});
}
} else {
console.log(`[AssignmentHandler] No answer provided for: "${questionText}"`);
}
});
console.log(`[AssignmentHandler] Finished. Processed ${questionsProcessed} of ${questionContainers.length} question(s)`);
return {
success: true,
questionsProcessed: questionsProcessed,
totalQuestions: questionContainers.length,
questionsNotFound: questionsNotFound
};
}
// Listen for messages from popup
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log('[AssignmentHandler] Received message:', request.action);
if (request.action === 'fillAssignment') {
try {
if (!request.answers) {
sendResponse({
success: false,
error: 'No answers provided'
});
return;
}
const result = fillAssignmentAnswers(request.answers);
sendResponse(result);
} catch (error) {
console.error('[AssignmentHandler] Error:', error);
sendResponse({
success: false,
error: error.message
});
}
return true; // Keep message channel open for async response
}
if (request.action === 'checkAssignmentPage') {
// Check if we're on an assignment page
const questionContainers = document.querySelectorAll('.que.multichoice');
sendResponse({
isAssignmentPage: questionContainers.length > 0,
questionCount: questionContainers.length
});
return true;
}
});
console.log('[AssignmentHandler] Ready and listening for messages');
})();