-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
656 lines (600 loc) · 22.4 KB
/
Copy pathpopup.js
File metadata and controls
656 lines (600 loc) · 22.4 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
document.addEventListener("DOMContentLoaded", function () {
// DOM elements
const aiProviderSelect = document.getElementById("ai-provider");
const apiKeyInput = document.getElementById("api-key");
const assistanceTypeSelect = document.getElementById("assistance-type");
const saveSettingsButton = document.getElementById("save-settings");
const getHelpButton = document.getElementById("get-help");
const optimizeCodeButton = document.getElementById("optimize-code");
const debugCodeButton = document.getElementById("debug-code");
const explainErrorButton = document.getElementById("explain-error");
const aiResponseDiv = document.getElementById("ai-response");
const insertCodeButton = document.getElementById("insert-code");
const problemDetailsDiv = document.getElementById("problem-details");
const modelInfoDiv = document.getElementById("model-info");
const loadingIndicator = document.getElementById("loading-indicator");
const settingsToggleButton = document.getElementById("settings-toggle");
const settingsSection = document.getElementById("settings-section");
// Current problem details
let currentProblemDetails = null;
let currentCode = "";
let currentTestCases = [];
let currentSubmissionResults = null;
// Settings toggle
settingsToggleButton.addEventListener("click", function () {
settingsSection.classList.toggle("hidden");
});
// Load saved settings
chrome.storage.sync.get(
["aiProvider", "apiKey", "assistanceType"],
function (result) {
if (result.aiProvider) aiProviderSelect.value = result.aiProvider;
if (result.apiKey) {
apiKeyInput.value = result.apiKey;
settingsToggleButton.classList.add("configured");
}
if (result.assistanceType)
assistanceTypeSelect.value = result.assistanceType;
}
);
// Save settings
saveSettingsButton.addEventListener("click", function () {
const settings = {
aiProvider: aiProviderSelect.value,
apiKey: apiKeyInput.value,
assistanceType: assistanceTypeSelect.value,
};
chrome.storage.sync.set(settings, function () {
showNotification("Settings saved!", "success");
// Hide settings section after saving
settingsSection.classList.add("hidden");
// Add configured class if API key is set
if (settings.apiKey) {
settingsToggleButton.classList.add("configured");
} else {
settingsToggleButton.classList.remove("configured");
}
});
});
// Check if we're on a LeetCode problem page
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
if (currentTab.url.includes("leetcode.com/problems/")) {
// Send message to content script to get problem details
chrome.tabs.sendMessage(
currentTab.id,
{ action: "getProblemDetails" },
function (response) {
if (response && response.problemDetails) {
currentProblemDetails = response.problemDetails;
displayProblemDetails(currentProblemDetails);
// Get current code
chrome.tabs.sendMessage(
currentTab.id,
{ action: "getCurrentCode" },
function (codeResponse) {
if (codeResponse && codeResponse.code) {
currentCode = codeResponse.code;
}
}
);
// Get test cases
chrome.tabs.sendMessage(
currentTab.id,
{ action: "getTestCases" },
function (testResponse) {
if (testResponse && testResponse.testCases) {
currentTestCases = testResponse.testCases;
}
}
);
// Get submission results
chrome.tabs.sendMessage(
currentTab.id,
{ action: "getSubmissionResults" },
function (resultResponse) {
if (resultResponse && resultResponse.results) {
currentSubmissionResults = resultResponse.results;
// If there's an error, enable the explain error button
if (currentSubmissionResults.status === "error") {
explainErrorButton.disabled = false;
explainErrorButton.classList.remove("hidden");
}
}
}
);
} else {
problemDetailsDiv.innerHTML =
"<p>Error retrieving problem details. Please refresh the page.</p>";
}
}
);
} else {
problemDetailsDiv.innerHTML =
"<p>Not on a LeetCode problem page. Please navigate to a problem.</p>";
disableAllButtons();
}
});
// Get help button click handler
getHelpButton.addEventListener("click", function () {
// Get current settings
chrome.storage.sync.get(
["aiProvider", "apiKey", "assistanceType"],
function (settings) {
if (!settings.apiKey) {
showNotification("Please enter an API key in the settings.", "error");
return;
}
// Show loading state
showLoading("Thinking...");
// Get problem details from the content script
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "getProblemDetails" },
function (response) {
if (response && response.problemDetails) {
currentProblemDetails = response.problemDetails;
// Send to background script to make API call
chrome.runtime.sendMessage(
{
action: "getAIHelp",
problemDetails: currentProblemDetails,
settings: settings,
},
function (aiResponse) {
hideLoading();
if (aiResponse && aiResponse.content) {
displayAIResponse(aiResponse.content, aiResponse.model);
// Show insert code button if we have a solution
if (
settings.assistanceType === "solution" ||
settings.assistanceType === "optimize"
) {
insertCodeButton.classList.remove("hidden");
}
} else {
showNotification(
"Error getting AI response. Please check your API key and try again.",
"error"
);
}
}
);
} else {
hideLoading();
showNotification(
"Error retrieving problem details. Please refresh the page.",
"error"
);
}
}
);
}
);
}
);
});
// Optimize code button click handler
optimizeCodeButton.addEventListener("click", function () {
// Get current settings
chrome.storage.sync.get(["aiProvider", "apiKey"], function (settings) {
if (!settings.apiKey) {
showNotification("Please enter an API key in the settings.", "error");
return;
}
// Show loading state
showLoading("Optimizing your code...");
// Get current code from the editor
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "getCurrentCode" },
function (response) {
if (response && response.code) {
currentCode = response.code;
// Send to background script to make API call
chrome.runtime.sendMessage(
{
action: "optimizeCode",
code: currentCode,
problemDetails: currentProblemDetails,
settings: settings,
},
function (aiResponse) {
hideLoading();
if (aiResponse && aiResponse.content) {
displayAIResponse(aiResponse.content, aiResponse.model);
insertCodeButton.classList.remove("hidden");
} else {
showNotification(
"Error optimizing code. Please check your API key and try again.",
"error"
);
}
}
);
} else {
hideLoading();
showNotification(
"Error retrieving your code. Please make sure you have code in the editor.",
"error"
);
}
}
);
});
});
});
// Debug code button click handler
if (debugCodeButton) {
debugCodeButton.addEventListener("click", function () {
// Get current settings
chrome.storage.sync.get(["aiProvider", "apiKey"], function (settings) {
if (!settings.apiKey) {
showNotification("Please enter an API key in the settings.", "error");
return;
}
// Show loading state
showLoading("Debugging your code...");
// Get current code from the editor
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "getCurrentCode" },
function (response) {
if (response && response.code) {
currentCode = response.code;
// Get test cases if we don't have them yet
if (currentTestCases.length === 0) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "getTestCases" },
function (testResponse) {
if (testResponse && testResponse.testCases) {
currentTestCases = testResponse.testCases;
}
// Send to background script to make API call
sendDebugRequest(
settings,
currentCode,
currentTestCases,
currentProblemDetails
);
}
);
} else {
// Send to background script to make API call
sendDebugRequest(
settings,
currentCode,
currentTestCases,
currentProblemDetails
);
}
} else {
hideLoading();
showNotification(
"Error retrieving your code. Please make sure you have code in the editor.",
"error"
);
}
}
);
}
);
});
});
}
// Explain error button click handler
if (explainErrorButton) {
explainErrorButton.addEventListener("click", function () {
// Get current settings
chrome.storage.sync.get(["aiProvider", "apiKey"], function (settings) {
if (!settings.apiKey) {
showNotification("Please enter an API key in the settings.", "error");
return;
}
// Show loading state
showLoading("Analyzing error...");
// Get current code and error message
chrome.tabs.query(
{ active: true, currentWindow: true },
function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "getCurrentCode" },
function (codeResponse) {
if (codeResponse && codeResponse.code) {
currentCode = codeResponse.code;
// Get submission results if we don't have them yet
if (!currentSubmissionResults) {
chrome.tabs.sendMessage(
tabs[0].id,
{ action: "getSubmissionResults" },
function (resultResponse) {
if (resultResponse && resultResponse.results) {
currentSubmissionResults = resultResponse.results;
sendExplainErrorRequest(
settings,
currentCode,
currentSubmissionResults,
currentProblemDetails
);
} else {
hideLoading();
showNotification(
"No error found. Please run your code first to get an error message.",
"warning"
);
}
}
);
} else {
sendExplainErrorRequest(
settings,
currentCode,
currentSubmissionResults,
currentProblemDetails
);
}
} else {
hideLoading();
showNotification(
"Error retrieving your code. Please make sure you have code in the editor.",
"error"
);
}
}
);
}
);
});
});
}
// Insert code button click handler
insertCodeButton.addEventListener("click", function () {
const codeMatch = aiResponseDiv.innerText.match(
/```(?:[\w]*)\n([\s\S]*?)```/
);
if (codeMatch && codeMatch[1]) {
const codeToInsert = codeMatch[1].trim();
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(
tabs[0].id,
{
action: "insertCode",
code: codeToInsert,
},
function (response) {
if (response && response.success) {
showNotification("Code inserted successfully!", "success");
} else {
showNotification(
"Failed to insert code. Please try again.",
"error"
);
}
}
);
});
} else {
showNotification(
"No code found in the AI response to insert.",
"warning"
);
}
});
// Helper functions
function displayProblemDetails(details) {
// Check if title is available and properly formatted
let title = details.title || "Unknown Problem";
// Clean up title if it contains HTML (like when scraping from LeetCode)
if (title.includes("<a") || title.includes("</a>")) {
// Extract text from HTML title
const tempDiv = document.createElement("div");
tempDiv.innerHTML = title;
title = tempDiv.textContent || tempDiv.innerText || title;
}
// Format difficulty with appropriate styling
let difficultyHTML = "";
if (details.difficulty) {
const diffClass = details.difficulty.toLowerCase();
difficultyHTML = `<span class="difficulty ${diffClass}">${details.difficulty}</span>`;
}
// Truncate description to a reasonable length
const truncatedDescription = details.description
? details.description.length > 120
? details.description.substring(0, 120) + "..."
: details.description
: "No description available";
problemDetailsDiv.innerHTML = `
<p><strong>Problem:</strong> ${title}</p>
<p><strong>Difficulty:</strong> ${difficultyHTML}</p>
<p><strong>Description:</strong> ${truncatedDescription}</p>
${
details.topics && details.topics.length > 0
? `<p><strong>Topics:</strong> ${details.topics
.map((topic) => `<span class="topic-tag">${topic}</span>`)
.join(" ")}</p>`
: ""
}
`;
}
function displayAIResponse(content, model) {
// Check if marked library is loaded
if (typeof marked === "undefined") {
console.error("Marked library is not loaded. Cannot render Markdown.");
// Display raw content with a warning
aiResponseDiv.innerHTML =
'<p style="color: red; font-weight: bold;">Error: Markdown library not found.</p>' +
"<pre><code>" +
escapeHtml(content) +
"</code></pre>";
// Optionally hide model info/insert button if rendering failed
if (modelInfoDiv) modelInfoDiv.classList.add("hidden");
insertCodeButton.classList.add("hidden");
return;
}
try {
// Use marked library to convert markdown to HTML
const formattedContent = marked.parse(content);
aiResponseDiv.innerHTML = formattedContent;
// Add copy buttons to all code blocks
const codeBlocks = aiResponseDiv.querySelectorAll("pre code");
codeBlocks.forEach((codeBlock, index) => {
const pre = codeBlock.parentNode;
// Create a container for the code block to position the copy button
const container = document.createElement("div");
container.className = "code-block-container";
pre.parentNode.insertBefore(container, pre);
container.appendChild(pre);
// Create copy button
const copyButton = document.createElement("button");
copyButton.className = "copy-code-button";
copyButton.innerHTML =
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg>';
copyButton.title = "Copy code";
copyButton.dataset.index = index;
// Add copy button to the container
container.appendChild(copyButton);
// Add click event to copy button
copyButton.addEventListener("click", function () {
const codeText = codeBlock.textContent;
navigator.clipboard.writeText(codeText).then(
function () {
showNotification("Code copied to clipboard!", "success");
// Change to checkmark when copied
const originalContent = copyButton.innerHTML;
copyButton.innerHTML =
'<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="20 6 9 17 4 12"></polyline></svg>';
copyButton.classList.add("copied");
setTimeout(() => {
copyButton.classList.remove("copied");
copyButton.innerHTML = originalContent;
}, 1500);
},
function () {
showNotification("Failed to copy code", "error");
}
);
});
});
} catch (error) {
console.error("Error parsing Markdown:", error);
// Display raw content if parsing fails
aiResponseDiv.innerHTML =
'<p style="color: red; font-weight: bold;">Error parsing Markdown.</p>' +
"<pre><code>" +
escapeHtml(content) +
"</code></pre>";
if (modelInfoDiv) modelInfoDiv.classList.add("hidden");
insertCodeButton.classList.add("hidden");
return;
}
// Display model info if available
if (modelInfoDiv && model) {
modelInfoDiv.textContent = `Generated by: ${model}`;
modelInfoDiv.classList.remove("hidden");
}
}
// Helper function to escape HTML (basic version)
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function sendDebugRequest(settings, code, testCases, problemDetails) {
chrome.runtime.sendMessage(
{
action: "debugCode",
code: code,
testCases: testCases,
settings: settings,
problemDetails: problemDetails,
},
function (aiResponse) {
hideLoading();
if (aiResponse && aiResponse.content) {
displayAIResponse(aiResponse.content, aiResponse.model);
insertCodeButton.classList.remove("hidden");
} else {
showNotification(
"Error debugging code. Please check your API key and try again.",
"error"
);
}
}
);
}
function sendExplainErrorRequest(
settings,
code,
submissionResults,
problemDetails
) {
const errorMessage =
submissionResults.errorDetails ||
submissionResults.message ||
"Unknown error";
chrome.runtime.sendMessage(
{
action: "explainError",
code: code,
errorMessage: errorMessage,
settings: settings,
problemDetails: problemDetails,
},
function (aiResponse) {
hideLoading();
if (aiResponse && aiResponse.content) {
displayAIResponse(aiResponse.content, aiResponse.model);
insertCodeButton.classList.remove("hidden");
} else {
showNotification(
"Error explaining the error. Please check your API key and try again.",
"error"
);
}
}
);
}
function showLoading(message) {
if (loadingIndicator) {
loadingIndicator.textContent = message || "Loading...";
loadingIndicator.classList.remove("hidden");
}
aiResponseDiv.innerHTML = `<p>${message || "Loading..."}</p>`;
}
function hideLoading() {
if (loadingIndicator) {
loadingIndicator.classList.add("hidden");
}
}
function showNotification(message, type) {
const notificationDiv = document.createElement("div");
notificationDiv.className = `notification ${type || "info"}`;
notificationDiv.textContent = message;
document.body.appendChild(notificationDiv);
// Remove after 3 seconds
setTimeout(() => {
notificationDiv.classList.add("fade-out");
setTimeout(() => {
document.body.removeChild(notificationDiv);
}, 500);
}, 3000);
}
function disableAllButtons() {
getHelpButton.disabled = true;
optimizeCodeButton.disabled = true;
if (debugCodeButton) debugCodeButton.disabled = true;
if (explainErrorButton) explainErrorButton.disabled = true;
}
});