-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
146 lines (132 loc) · 4.72 KB
/
popup.js
File metadata and controls
146 lines (132 loc) · 4.72 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
document.addEventListener("DOMContentLoaded", function () {
const statusDiv = document.getElementById("status");
const syncSection = document.getElementById("sync-section");
const formattedContentTextarea = document.getElementById("formattedContent");
const copyButton = document.getElementById("copyButton");
const solutionInput = document.getElementById("solutionInput");
const solveButton = document.getElementById("solveButton");
const aiSolution = document.getElementById("aiSolution");
const copySolutionButton = document.getElementById("copySolutionButton");
// Load saved content
chrome.storage.local.get(
["formattedContent", "userSolution", "aiSolution"],
function (result) {
if (result.formattedContent)
formattedContentTextarea.value = result.formattedContent;
if (result.userSolution) solutionInput.value = result.userSolution;
if (result.aiSolution) aiSolution.value = result.aiSolution;
},
);
// Save content when it changes
formattedContentTextarea.addEventListener("input", saveContent);
solutionInput.addEventListener("input", saveContent);
aiSolution.addEventListener("input", saveContent);
function saveContent() {
chrome.storage.local.set({
formattedContent: formattedContentTextarea.value,
userSolution: solutionInput.value,
aiSolution: aiSolution.value,
});
}
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let currentTab = tabs[0];
if (currentTab.url.includes("hackerrank.com")) {
chrome.tabs.sendMessage(
currentTab.id,
{ action: "scrape" },
function (response) {
if (chrome.runtime.lastError) {
showError("An error occurred. Please try again.");
} else if (response && response.success) {
showSuccess(response.message, response.formattedContent);
} else if (response) {
showError(response.message);
} else {
showError(
"No response from the page. Please refresh and try again.",
);
}
},
);
} else {
showError("This extension only works on HackerRank pages.");
}
});
copyButton.addEventListener("click", function () {
formattedContentTextarea.select();
document.execCommand("copy");
copyButton.textContent = "Copied!";
setTimeout(() => {
copyButton.textContent = "Copy to clipboard";
}, 2000);
});
solveButton.addEventListener("click", async function () {
const problemStatement = formattedContentTextarea.value;
const userSolution = solutionInput.value;
if (!problemStatement || !userSolution) {
showError(
"Please ensure both the problem statement and your solution are present.",
);
return;
}
showStatus("Generating solution...");
try {
const response = await fetch("http://localhost:8005/solveProblem", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ problemStatement, userSolution }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
aiSolution.value = data.solution;
// Save the new AI solution
saveContent();
showSuccess("Solution generated successfully!");
} catch (error) {
showError("Error generating solution: " + error.message);
}
});
copySolutionButton.addEventListener("click", function () {
const cleanedSolution = aiSolution.value.replace(
/```[\s\S]*?```/g,
function (match) {
return match.replace(/```/g, "").trim();
},
);
const tempTextArea = document.createElement("textarea");
tempTextArea.value = cleanedSolution;
document.body.appendChild(tempTextArea);
tempTextArea.select();
document.execCommand("copy");
document.body.removeChild(tempTextArea);
copySolutionButton.textContent = "Copied!";
setTimeout(() => {
copySolutionButton.textContent = "Copy solution";
}, 2000);
});
function showSuccess(message, formattedContent = null) {
statusDiv.textContent = message;
statusDiv.className = "status success";
statusDiv.style.display = "block";
syncSection.style.display = "block";
if (formattedContent) {
formattedContentTextarea.value = formattedContent;
// Save the new formatted content
saveContent();
}
}
function showError(message) {
statusDiv.textContent = message;
statusDiv.className = "status error";
statusDiv.style.display = "block";
}
function showStatus(message) {
statusDiv.textContent = message;
statusDiv.className = "status";
statusDiv.style.display = "block";
}
});