Automatically selects correct answers for Miacademy assignments
Size
8.3 KB
Version
1.1.1
Created
Feb 19, 2026
Updated
18 days ago
1// ==UserScript==
2// @name Miacademy Auto-Correct Answer Helper
3// @description Automatically selects correct answers for Miacademy assignments
4// @version 1.1.1
5// @match https://*.miacademy.co/*
6// @icon https://robomonkey.io/favicon.ico
7// ==/UserScript==
8(function() {
9 'use strict';
10
11 console.log('Miacademy Auto-Correct Answer Helper loaded');
12
13 // Debounce function to prevent multiple rapid calls
14 function debounce(func, wait) {
15 let timeout;
16 return function executedFunction(...args) {
17 const later = () => {
18 clearTimeout(timeout);
19 func(...args);
20 };
21 clearTimeout(timeout);
22 timeout = setTimeout(later, wait);
23 };
24 }
25
26 // Function to extract question and answers from the page
27 function extractQuestionData() {
28 try {
29 // Get the question text
30 const questionElement = document.querySelector('.mia-Prompt .mia-promptText');
31 if (!questionElement) {
32 console.log('No question found on page');
33 return null;
34 }
35
36 const questionText = questionElement.textContent.trim();
37
38 // Get all answer options
39 const answerElements = document.querySelectorAll('.mia-SelectableAnswer');
40 if (answerElements.length === 0) {
41 console.log('No answer options found');
42 return null;
43 }
44
45 const answers = Array.from(answerElements).map((el, index) => {
46 const textBlock = el.querySelector('.mia-TextBlock');
47 return {
48 index: index,
49 text: textBlock ? textBlock.textContent.trim() : '',
50 element: el
51 };
52 });
53
54 console.log('Question extracted:', questionText);
55 console.log('Answers extracted:', answers.map(a => a.text));
56
57 return {
58 question: questionText,
59 answers: answers
60 };
61 } catch (error) {
62 console.error('Error extracting question data:', error);
63 return null;
64 }
65 }
66
67 // Function to select the correct answer using AI
68 async function selectCorrectAnswer() {
69 try {
70 const questionData = extractQuestionData();
71 if (!questionData) {
72 console.log('Could not extract question data');
73 return;
74 }
75
76 console.log('Asking AI for the correct answer...');
77
78 // Create a prompt for the AI
79 const prompt = `You are helping with a Miacademy quiz question.
80
81Question: ${questionData.question}
82
83Answer options:
84${questionData.answers.map((a, i) => `${i + 1}. ${a.text}`).join('\n')}
85
86Which answer is correct? Respond with ONLY the number (1, 2, 3, or 4) of the correct answer.`;
87
88 // Use AI to determine the correct answer
89 const aiResponse = await RM.aiCall(prompt, {
90 type: 'json_schema',
91 json_schema: {
92 name: 'correct_answer',
93 schema: {
94 type: 'object',
95 properties: {
96 answerNumber: {
97 type: 'number',
98 minimum: 1,
99 maximum: 10,
100 description: 'The number of the correct answer'
101 },
102 reasoning: {
103 type: 'string',
104 description: 'Brief explanation of why this is correct'
105 }
106 },
107 required: ['answerNumber']
108 }
109 }
110 });
111
112 console.log('AI Response:', aiResponse);
113
114 const correctIndex = aiResponse.answerNumber - 1;
115 if (correctIndex >= 0 && correctIndex < questionData.answers.length) {
116 const correctAnswer = questionData.answers[correctIndex];
117 console.log(`Selecting answer ${correctIndex + 1}: ${correctAnswer.text}`);
118 console.log('Reasoning:', aiResponse.reasoning);
119
120 // Click the radio button for the correct answer
121 const radioButton = correctAnswer.element.querySelector('input[type="radio"]');
122 if (radioButton) {
123 radioButton.click();
124 console.log('Answer selected successfully!');
125
126 // Wait a moment, then click submit
127 setTimeout(() => {
128 const submitButton = document.querySelector('.mia-SubmitButton:not(.disabled)');
129 if (submitButton) {
130 console.log('Clicking submit button...');
131 submitButton.click();
132 } else {
133 console.log('Submit button not available yet');
134 }
135 }, 500);
136 } else {
137 console.error('Could not find radio button for answer');
138 }
139 } else {
140 console.error('Invalid answer index from AI:', correctIndex);
141 }
142 } catch (error) {
143 console.error('Error selecting correct answer:', error);
144 }
145 }
146
147 // Function to check if we're on a quiz page
148 function isQuizPage() {
149 return document.querySelector('.mia-Prompt') !== null &&
150 document.querySelector('.mia-SelectableAnswer') !== null;
151 }
152
153 // Function to add a helper button to the page
154 function addHelperButton() {
155 // Check if button already exists
156 if (document.getElementById('mia-auto-answer-btn')) {
157 return;
158 }
159
160 // Only add button if we're on a quiz page
161 if (!isQuizPage()) {
162 return;
163 }
164
165 const button = document.createElement('button');
166 button.id = 'mia-auto-answer-btn';
167 button.textContent = '🤖 Auto Answer';
168 button.style.cssText = `
169 position: fixed;
170 top: 20px;
171 right: 20px;
172 z-index: 10000;
173 padding: 12px 20px;
174 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
175 color: white;
176 border: none;
177 border-radius: 8px;
178 font-size: 16px;
179 font-weight: bold;
180 cursor: pointer;
181 box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
182 transition: all 0.3s ease;
183 `;
184
185 button.addEventListener('mouseenter', () => {
186 button.style.transform = 'translateY(-2px)';
187 button.style.boxShadow = '0 6px 20px rgba(0, 0, 0, 0.3)';
188 });
189
190 button.addEventListener('mouseleave', () => {
191 button.style.transform = 'translateY(0)';
192 button.style.boxShadow = '0 4px 15px rgba(0, 0, 0, 0.2)';
193 });
194
195 button.addEventListener('click', async () => {
196 button.disabled = true;
197 button.textContent = '⏳ Thinking...';
198 try {
199 await selectCorrectAnswer();
200 } finally {
201 setTimeout(() => {
202 button.disabled = false;
203 button.textContent = '🤖 Auto Answer';
204 }, 2000);
205 }
206 });
207
208 document.body.appendChild(button);
209 console.log('Auto Answer button added to page');
210 }
211
212 // Watch for page changes and add button when quiz appears
213 const debouncedAddButton = debounce(addHelperButton, 500);
214
215 // Use MutationObserver to detect when quiz content loads
216 const observer = new MutationObserver(debouncedAddButton);
217
218 // Initialize when page loads
219 function init() {
220 console.log('Initializing Miacademy Auto-Correct Answer Helper');
221
222 // Add button immediately if quiz is already visible
223 addHelperButton();
224
225 // Watch for changes in the page
226 observer.observe(document.body, {
227 childList: true,
228 subtree: true
229 });
230
231 console.log('Watching for quiz questions...');
232 }
233
234 // Wait for page to be ready
235 if (document.readyState === 'loading') {
236 document.addEventListener('DOMContentLoaded', init);
237 } else {
238 init();
239 }
240})();