Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
120 lines (96 loc) · 3.34 KB
/
Copy pathcontent.js
File metadata and controls
120 lines (96 loc) · 3.34 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
function getLeetClipIcon() {
const iconUrl = chrome.runtime.getURL("assets/copy.svg");
return `<img src="${iconUrl}" style="width: 18px; height: 18px;" alt="LeetClip">`;
}
function getCheckIcon(color = "#2cbb5d") {
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="${color}">
<path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z"/>
</svg>`;
}
function extractProblemDescription() {
const descElement = document.querySelector(
'[data-track-load="description_content"]',
);
return descElement ? descElement.innerText : "Problem description not found.";
}
// Listen for the Monaco code from inject.js
window.addEventListener("message", (event) => {
if (
event.source !== window ||
!event.data ||
event.data.type !== "LEETCLIP_CODE"
) {
return;
}
const code = event.data.text;
const description = extractProblemDescription();
chrome.storage.local.get(["activePrompt"], (data) => {
let activePromptText = "Is this code correct? Give me the time and space complexity analysis and suggest improvements.";
if (data.activePrompt) {
activePromptText = data.activePrompt.text;
}
const finalPayload = `### Problem Context & Constraints\n${description}\n\n### My Current Code\n\`\`\`\n${code}\n\`\`\`\n\n### Task\n${activePromptText}`;
navigator.clipboard.writeText(finalPayload).then(() => {
const btn = document.getElementById("leetclip-btn");
if (btn) {
btn.dataset.state = "success";
btn.style.backgroundColor = "rgba(44, 187, 93, 0.2)";
btn.innerHTML = getCheckIcon();
setTimeout(() => {
btn.dataset.state = "";
btn.style.backgroundColor = btn.matches(":hover")
? "rgba(0,0,0,0.1)"
: "rgba(0,0,0,0.05)";
btn.innerHTML = getLeetClipIcon();
}, 2000);
}
});
});
});
function requestCopyToLLM() {
const script = document.createElement("script");
script.src = chrome.runtime.getURL("inject.js");
script.onload = function () {
this.remove();
};
(document.head || document.documentElement).appendChild(script);
}
// --- DOM INJECTION ---
function injectLeetClipButton() {
if (document.getElementById("leetclip-btn")) return;
const toolbar = document.querySelector(".h-full.py-2");
if (!toolbar) return;
const actionBtnGrp = toolbar.children[0];
const copyBtn = document.createElement("button");
copyBtn.id = "leetclip-btn";
copyBtn.title = "Copy to LeetClip";
Object.assign(copyBtn.style, {
width: "32px",
height: "32px",
borderRadius: "6px",
border: "none",
cursor: "pointer",
backgroundColor: "rgba(0,0,0,0.05)",
display: "flex",
alignItems: "center",
justifyContent: "center",
transition: "background-color 0.2s ease",
});
// Hover effects
copyBtn.addEventListener("mouseenter", () => {
if (copyBtn.dataset.state !== "success") {
copyBtn.style.backgroundColor = "rgba(0,0,0,0.1)";
}
});
copyBtn.addEventListener("mouseleave", () => {
if (copyBtn.dataset.state !== "success") {
copyBtn.style.backgroundColor = "rgba(0,0,0,0.05)";
}
});
copyBtn.innerHTML = getLeetClipIcon();
copyBtn.addEventListener("click", () => {
requestCopyToLLM();
});
actionBtnGrp.appendChild(copyBtn);
}
setInterval(injectLeetClipButton, 2000);