forked from Aryan-Sheregar/Nanas-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarousel_inject.js
More file actions
123 lines (104 loc) · 5.24 KB
/
carousel_inject.js
File metadata and controls
123 lines (104 loc) · 5.24 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
(function() {
// Check if the script has already run
if (window.hasFixyRun) {
console.log("Fixy content script has already run, skipping initialization.");
return;
}
window.hasFixyRun = true;
console.log("Initializing Fixy content script.");
// Function for webpage data extraction
function extractDivContent() {
const element = document.querySelector('.view-lines.monaco-mouse-cursor-text');
const element_q = document.querySelector('.elfjS');
const pythonButton = document.querySelector(
'button.rounded.items-center.whitespace-nowrap.focus\\:outline-none.inline-flex.bg-transparent.dark\\:bg-dark-transparent.text-text-secondary.dark\\:text-text-secondary.active\\:bg-transparent.dark\\:active\\:bg-dark-transparent.hover\\:bg-fill-secondary.dark\\:hover\\:bg-fill-secondary.px-1\\.5.py-0\\.5.text-sm.font-normal.group'
);
return {
question: element_q ? element_q.innerText.trim() : "Question element not found.",
answer: element ? element.innerText.trim() : "Answer element not found.",
program_type: pythonButton ? pythonButton.textContent.trim() : "Program type element not found."
};
}
// Function to extract content and send to background script
function autoExtractText() {
const extractedData = extractDivContent();
if (
extractedData &&
extractedData.question !== "Question element not found." &&
extractedData.answer !== "Answer element not found." &&
extractedData.program_type !== "Program type element not found."
) {
console.log("Extracted Data:", extractedData);
// Log the time when the message is sent
console.log("Sending data to background script at:", new Date().toLocaleTimeString());
// Try to send extracted data to the background script
chrome.runtime.sendMessage({ action: 'processData', data: extractedData }, response => {
if (chrome.runtime.lastError) {
console.error("Error sending message:", chrome.runtime.lastError.message);
} else {
console.log("Message sent successfully:", response);
}
});
} else {
console.log("Incomplete data extracted, not sending to background script.");
}
}
// Listener for messages from background script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.similarityScore !== undefined) {
console.log("Received similarity score:", request.similarityScore);
// Display the image based on the similarity score
displayImageBasedOnScore(request.similarityScore);
} else {
console.log("Received message without similarityScore:", request);
}
});
// Image display logic
function displayImageBasedOnScore(similarityScore) {
console.log("Raw similarityScore received:", similarityScore);
// Handle undefined or null similarityScore
if (similarityScore === undefined || similarityScore === null || similarityScore === "") {
similarityScore = "N/A";
}
let imageIndex = parseInt(similarityScore, 10);
console.log("Parsed imageIndex:", imageIndex);
if (!isNaN(imageIndex) && imageIndex >= 1 && imageIndex <= 5) {
// Valid imageIndex between 1 and 5, do nothing
console.log("Valid imageIndex:", imageIndex);
} else {
imageIndex = 6; // Use image6.png for invalid similarityScore
console.log("Invalid similarityScore, defaulting imageIndex to 6");
}
// Create image container if it doesn't exist
let imageContainer = document.getElementById("imageContainer");
if (!imageContainer) {
imageContainer = document.createElement("div");
imageContainer.id = "imageContainer";
document.body.appendChild(imageContainer);
// Create toggle button
const toggleButton = document.createElement("button");
toggleButton.id = "imageToggleButton";
toggleButton.innerText = "Toggle Image";
imageContainer.appendChild(toggleButton);
// Create image element
const resultImage = document.createElement("img");
resultImage.id = "resultImage";
imageContainer.appendChild(resultImage);
// Toggle button click handler
let imageVisible = true;
toggleButton.addEventListener("click", () => {
imageVisible = !imageVisible;
resultImage.style.visibility = imageVisible ? "visible" : "hidden"; // Toggle visibility
});
}
// Update the image source based on the imageIndex
const resultImage = document.getElementById("resultImage");
const imagePath = chrome.runtime.getURL(`image${imageIndex}.png`);
console.log(`chrome.runtime.getURL('image${imageIndex}.png') returned:`, imagePath);
resultImage.src = imagePath;
}
// Start the periodic data extraction
setInterval(autoExtractText, 4000);
// Run it immediately once
autoExtractText();
})();