-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
161 lines (140 loc) · 5.09 KB
/
content.js
File metadata and controls
161 lines (140 loc) · 5.09 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const loadTesseract = () => {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = chrome.runtime.getURL("libs/tesseract.min.js");
document.head.appendChild(script);
script.onload = () => {
console.log("Tesseract.js loaded");
resolve();
};
script.onerror = () => {
console.error("Failed to load Tesseract.js");
reject(new Error("Failed to load Tesseract.js"));
};
});
};
async function processTextQuestions() {
const questions = document.querySelectorAll("[role='listitem']");
console.log("Questions:", questions);
questions.forEach(async (questionElement) => {
const questionTextElement = questionElement.querySelector("div[role='heading']");
const radioGroup = questionElement.querySelector("[role='radiogroup']");
const checkboxList = questionElement.querySelector("[role='list']"); // Identify checkbox groups
let questionText = "";
if (questionTextElement) {
questionText = questionTextElement.innerText.trim();
}
// Handle radio group
if (radioGroup) {
const labels = radioGroup.querySelectorAll("label");
const options = Array.from(labels).map((label, index) => {
const optionSpan = label.querySelector("span");
return `${index + 1}) ${optionSpan ? optionSpan.innerText.trim() : "Option not found"}`;
});
questionText += ` Choose from one of these MCQ Options: ${options}`;
}
// Handle checkbox list
if (checkboxList) {
const checkboxItems = checkboxList.querySelectorAll("[role='listitem']");
const options = Array.from(checkboxItems).map((item, index) => {
const labelSpan = item.querySelector("span");
return `${index + 1}) ${labelSpan ? labelSpan.innerText.trim() : "Option not found"}`;
});
questionText += ` Select from these checkbox options: ${options}`;
}
if (questionText) {
console.log(`Processing question: ${questionText}`);
try {
const response = await fetch("http://localhost:5000/get-answer", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ question: questionText }),
});
if (!response.ok) {
console.error(`Error fetching answer for question: ${questionText}`);
return;
}
const data = await response.json();
const answer = data.answer || "No answer found";
const answerNode = document.createElement("div");
answerNode.textContent = `Answer: ${answer}`;
answerNode.style.cssText = `
margin-top: 8px;
font-size: 14px;
color: #1b5e20;
background: #e8f5e9;
border: 1px solid #4caf50;
padding: 6px;
border-radius: 4px;
`;
questionElement.appendChild(answerNode);
} catch (error) {
console.error("Error:", error);
}
}
});
}
async function processImageQuestions() {
const images = document.querySelectorAll("[role='listitem'] img");
console.log("Images:", images);
images.forEach(async (image) => {
try {
console.log("Processing image question...");
const { data } = await Tesseract.recognize(image.src, "eng");
const ocrText = data.text.trim();
if (ocrText) {
console.log(`Extracted OCR text: ${ocrText}`);
const response = await fetch("http://localhost:5000/get-answer", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ question: ocrText }),
});
if (!response.ok) {
console.error(`Error fetching answer for image text: ${ocrText}`);
return;
}
const data = await response.json();
const answer = data.answer || "No answer found";
const container = image.closest("[role='listitem']");
const ocrTextNode = document.createElement("div");
ocrTextNode.textContent = `Extracted Text: ${ocrText}`;
ocrTextNode.style.cssText = `
margin-top: 8px;
font-size: 12px;
color: #333;
background: #f9f9f9;
border: 1px solid #ddd;
padding: 6px;
border-radius: 4px;
`;
container.appendChild(ocrTextNode);
const answerNode = document.createElement("div");
answerNode.textContent = `Answer: ${answer}`;
answerNode.style.cssText = `
margin-top: 8px;
font-size: 14px;
color: #1b5e20;
background: #e8f5e9;
border: 1px solid #4caf50;
padding: 6px;
border-radius: 4px;
`;
container.appendChild(answerNode);
}
} catch (error) {
console.error("Error processing image:", error);
}
});
}
document.addEventListener("DOMContentLoaded", async () => {
console.log("Google Forms detected. Processing questions...");
await loadTesseract();
console.log("Processing text questions...");
processTextQuestions();
console.log("Processing image questions...");
processImageQuestions();
});