Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
295 lines (262 loc) · 9.76 KB
/
Copy pathcontent.js
File metadata and controls
295 lines (262 loc) · 9.76 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
// Fungsi untuk membuat prompt
function createPrompt(originalText) {
return `Jawab hanya dalam satu baris tanpa membuat baris baru atau enter.
Jika soal objektif (pilihan ganda), cukup tulis nomor soal diikuti huruf jawaban, dipisahkan koma.
Jika soal esai, cukup jawab dengan kalimat jawaban.
Jangan gunakan bullet, list, atau line break.
${originalText}`;
}
// Fungsi umum untuk fetch respons dari Gemini API
async function fetchGeminiResponse(prompt, apiKey, model) {
const endpoint = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`;
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
contents: [{
parts: [{ text: prompt }]
}]
})
});
const result = await response.json();
return result.candidates?.[0]?.content?.parts?.[0]?.text?.trim() || "Tidak ada respons dari Gemini. Coba lagi nanti.";
} catch (error) {
console.error("Gagal mengambil respons dari Gemini:", error);
return "Terjadi kesalahan saat mengambil respons.";
}
}
// Fungsi untuk membuat container respons
function createResponseContainer(reply) {
const container = document.createElement("div");
container.style.position = "fixed";
container.style.bottom = "8px";
container.style.right = "20px";
container.style.width = "300px";
container.style.height = "40px";
container.style.fontSize = "7px";
container.style.overflow = "hidden";
container.style.backgroundColor = "transparent";
container.style.color = "rgba(128, 128, 128, 0.1)";
container.style.border = "none";
container.style.boxShadow = "none";
container.style.zIndex = 999999;
container.style.padding = "2px 72px 2px 2px";
container.style.borderRadius = "4px";
container.style.cursor = "move";
container.style.transition = "all 0.2s ease";
container.style.whiteSpace = "pre-wrap";
// Sembunyikan scrollbar
container.style.scrollbarWidth = "none"; // Firefox
container.style.msOverflowStyle = "none"; // IE 10+
container.style.overflowY = "auto";
container.style.setProperty("scrollbar-width", "none");
container.style.setProperty("::-webkit-scrollbar", "display: none");
const replySpan = document.createElement("span");
replySpan.textContent = reply;
replySpan.style.color = "rgba(128, 128, 128, 0.7)";
container.appendChild(replySpan);
// Scroll otomatis ke bawah setelah konten dimuat
requestAnimationFrame(() => {
container.scrollTop = container.scrollHeight;
});
container.autoHideEnabled = true; // Properti untuk auto-hide
addCopyButton(container, replySpan);
addAutoHideToggleButton(container);
addCloseButton(container);
addExpandClickHandler(container);
return container;
}
// Fungsi untuk menambahkan tombol copy
function addCopyButton(container, replySpan) {
const copyBtn = document.createElement("button");
copyBtn.textContent = "📋";
copyBtn.title = "Salin jawaban";
copyBtn.style.position = "absolute";
copyBtn.style.top = "2px";
copyBtn.style.right = "44px";
copyBtn.style.fontSize = "10px";
copyBtn.style.background = "transparent";
copyBtn.style.border = "none";
copyBtn.style.cursor = "pointer";
copyBtn.style.color = "rgba(128, 128, 128, 0.5)";
copyBtn.style.opacity = "0.2";
copyBtn.addEventListener("mouseenter", () => copyBtn.style.opacity = "1");
copyBtn.addEventListener("mouseleave", () => copyBtn.style.opacity = "0.3");
copyBtn.addEventListener("click", () => {
navigator.clipboard.writeText(replySpan.textContent)
.then(() => {
copyBtn.textContent = "✅";
setTimeout(() => copyBtn.textContent = "📋", 1500);
})
.catch(err => console.error("Gagal menyalin:", err));
});
container.appendChild(copyBtn);
}
// Fungsi untuk menambahkan tombol toggle auto-hide
function addAutoHideToggleButton(container) {
const autoHideBtn = document.createElement("button");
autoHideBtn.textContent = "⏳";
autoHideBtn.title = "Nonaktifkan auto-tutup";
autoHideBtn.style.position = "absolute";
autoHideBtn.style.top = "2px";
autoHideBtn.style.right = "64px";
autoHideBtn.style.fontSize = "10px";
autoHideBtn.style.background = "transparent";
autoHideBtn.style.border = "none";
autoHideBtn.style.cursor = "pointer";
autoHideBtn.style.color = "rgba(128, 128, 128, 0.3)";
autoHideBtn.style.opacity = "0.2";
autoHideBtn.addEventListener("mouseenter", () => autoHideBtn.style.opacity = "1");
autoHideBtn.addEventListener("mouseleave", () => autoHideBtn.style.opacity = "0.5");
autoHideBtn.addEventListener("click", () => {
container.autoHideEnabled = false;
clearTimeout(container.autoHideTimeout);
autoHideBtn.textContent = "✅";
autoHideBtn.title = "Auto-tutup dinonaktifkan";
setTimeout(() => {
autoHideBtn.textContent = "⏳";
autoHideBtn.title = "Nonaktifkan auto-tutup";
}, 2000);
});
container.appendChild(autoHideBtn);
}
// Fungsi untuk setup auto-hide
function setupAutoHide(container) {
container.autoHideTimeout = setTimeout(() => {
if (container.autoHideEnabled) {
container.remove();
}
}, 8000);
}
// Fungsi untuk menambahkan tombol close
function addCloseButton(container) {
const closeBtn = document.createElement("button");
closeBtn.textContent = "✖";
closeBtn.title = "Tutup";
closeBtn.style.position = "absolute";
closeBtn.style.top = "2px";
closeBtn.style.right = "4px";
closeBtn.style.fontSize = "10px";
closeBtn.style.background = "transparent";
closeBtn.style.border = "none";
closeBtn.style.cursor = "pointer";
closeBtn.style.color = "rgba(128, 128, 128, 0.5)";
closeBtn.style.opacity = "0.5";
closeBtn.addEventListener("mouseenter", () => closeBtn.style.opacity = "1");
closeBtn.addEventListener("mouseleave", () => closeBtn.style.opacity = "0.5");
closeBtn.addEventListener("click", () => {
container.remove();
});
container.appendChild(closeBtn);
}
// Fungsi untuk menambahkan handler expand saat klik
function addExpandClickHandler(container) {
container.addEventListener("click", (e) => {
if (e.target.tagName === "BUTTON") return;
const isExpanded = container.classList.contains("expanded");
if (isExpanded) {
container.classList.remove("expanded");
container.style.height = "12px";
container.style.overflow = "hidden";
} else {
container.classList.add("expanded");
container.style.height = "auto";
container.style.overflow = "auto";
requestAnimationFrame(() => {
container.scrollTop = container.scrollHeight;
});
}
});
}
// Fungsi untuk setup drag & drop
function setupDragAndDrop(container) {
let isDragging = false;
let offsetX, offsetY;
container.addEventListener("mousedown", (e) => {
if (e.target.tagName === "BUTTON") return;
isDragging = true;
offsetX = e.clientX - container.getBoundingClientRect().left;
offsetY = e.clientY - container.getBoundingClientRect().top;
container.style.transition = "none";
});
document.addEventListener("mousemove", (e) => {
if (isDragging) {
container.style.left = `${e.clientX - offsetX}px`;
container.style.top = `${e.clientY - offsetY}px`;
container.style.bottom = "auto";
container.style.right = "auto";
}
});
document.addEventListener("mouseup", () => {
isDragging = false;
});
}
// Event listeners untuk permintaan ke Gemini
window.addEventListener("gemini-flash-lite-request", async (e) => {
const originalText = e.detail;
const prompt = createPrompt(originalText);
const reply = await fetchGeminiResponse(prompt, "AIzaSyA6ZI7-jIFgzGeacEqDPyYgTr2LXYhEhKA", "gemini-2.5-flash-lite");
const container = createResponseContainer(reply);
document.body.appendChild(container);
setupDragAndDrop(container);
setupAutoHide(container);
});
window.addEventListener("gemini-flash-request", async (e) => {
const originalText = e.detail;
const prompt = createPrompt(originalText);
const reply = await fetchGeminiResponse(prompt, "AIzaSyCFWWvAH-qZEWMoFdh0NoF6qTKINuRcuG0", "gemini-2.5-flash");
const container = createResponseContainer(reply);
document.body.appendChild(container);
setupDragAndDrop(container);
setupAutoHide(container);
});
// Fitur tambahan: Auto-fetch saat copy (jika diaktifkan)
chrome.storage.local.get(["autoFetchOnCopy"], (result) => {
if (result.autoFetchOnCopy) {
document.addEventListener("copy", async (e) => {
const selectedText = window.getSelection().toString().trim();
if (!selectedText) return;
const customEvent = new CustomEvent("gemini-flash-lite-request", { detail: selectedText });
window.dispatchEvent(customEvent);
});
}
});
// Styling: Seleksi teks transparan
chrome.storage.local.get("blurSelectionEnabled", (result) => {
const enabled = result.blurSelectionEnabled ?? true;
if (enabled) {
const style = document.createElement('style');
style.textContent = `
::selection {
background-color: rgba(0, 255, 0, 0.02);
color: inherit;
}
`;
document.head.appendChild(style);
}
});
// Shortcut key: / untuk gemini-flash-lite-request
document.addEventListener("keydown", (e) => {
if (e.key === "/") {
e.preventDefault();
const selectedText = window.getSelection().toString().trim();
if (selectedText) {
const customEvent = new CustomEvent("gemini-flash-lite-request", { detail: selectedText });
window.dispatchEvent(customEvent);
}
}
});
// Shortcut key: ; untuk gemini-flash-request
document.addEventListener("keydown", (e) => {
if (e.key === ";") {
e.preventDefault();
const selectedText = window.getSelection().toString().trim();
if (selectedText) {
const customEvent = new CustomEvent("gemini-flash-request", { detail: selectedText });
window.dispatchEvent(customEvent);
}
}
});