-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
336 lines (299 loc) · 11.6 KB
/
Copy pathpopup.js
File metadata and controls
336 lines (299 loc) · 11.6 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
// popup.js - UI logic for Bookmark Semantic Search
const searchInput = document.getElementById("searchInput");
const clearBtn = document.getElementById("clearBtn");
const resultsArea = document.getElementById("resultsArea");
const stateMessage = document.getElementById("stateMessage");
const statusDot = document.getElementById("statusDot");
const statusText = document.getElementById("statusText");
const footerInfo = document.getElementById("footerInfo");
const reindexBtn = document.getElementById("reindexBtn");
const progressWrap = document.getElementById("progressWrap");
const progressBar = document.getElementById("progressBar");
const debugPanel = document.getElementById("debugPanel");
const debugLog = document.getElementById("debugLog");
const debugToggle = document.getElementById("debugToggle");
let debounceTimer = null;
let bookmarkCount = 0;
let debugVisible = false;
let debugEnabled = false;
let pollInterval = null;
// ── Status helpers ────────────────────────────────────────────────────────────
function setStatus(state, text) {
statusDot.className = `status-dot ${state}`;
statusText.textContent = text;
}
function showProgress(pct) {
progressWrap.classList.add("visible");
progressBar.style.width = `${Math.min(100, pct)}%`;
document.getElementById("progressPct").textContent = `${Math.round(pct)}%`;
}
function hideProgress() {
progressBar.style.width = "100%";
setTimeout(() => {
progressWrap.classList.remove("visible");
progressBar.style.width = "0%";
document.getElementById("progressPct").textContent = "";
}, 500);
}
function showState(emoji, title, sub) {
stateMessage.style.display = "block";
const emojiEl = document.createElement("span");
emojiEl.className = "emoji";
emojiEl.textContent = emoji;
const titleEl = document.createElement("strong");
titleEl.textContent = title;
const subEl = document.createElement("span");
subEl.textContent = sub;
stateMessage.replaceChildren(emojiEl, titleEl, subEl);
resultsArea.replaceChildren(stateMessage);
}
// ── Debug panel ───────────────────────────────────────────────────────────────
function renderDebugLogs(logs) {
debugLog.innerHTML = "";
if (!logs || logs.length === 0) {
const empty = document.createElement("div");
empty.style.color = "var(--muted)";
empty.textContent = "No log entries yet.";
debugLog.appendChild(empty);
return;
}
logs.forEach((entry) => {
const line = document.createElement("div");
line.className = `log-line log-${entry.level}`;
const ts = document.createElement("span");
ts.className = "log-ts";
ts.textContent = entry.ts;
const msg = document.createElement("span");
msg.textContent = ` ${entry.msg}`;
line.appendChild(ts);
line.appendChild(msg);
debugLog.appendChild(line);
});
debugLog.scrollTop = debugLog.scrollHeight;
}
function refreshDebug() {
if (!debugVisible) return;
chrome.runtime.sendMessage({ type: "GET_DEBUG" }, (response) => {
if (chrome.runtime.lastError || !response) return;
renderDebugLogs(response.logs);
});
}
debugToggle.addEventListener("click", () => {
debugVisible = !debugVisible;
debugPanel.classList.toggle("open", debugVisible);
debugToggle.textContent = debugVisible ? "▲ debug" : "▼ debug";
if (debugVisible) refreshDebug();
});
document.getElementById("debugCopy").addEventListener("click", () => {
chrome.runtime.sendMessage({ type: "GET_DEBUG" }, (response) => {
if (!response?.logs) return;
const text = response.logs
.map((l) => `[${l.ts}] [${l.level}] ${l.msg}`)
.join("\n");
navigator.clipboard.writeText(text).then(() => {
document.getElementById("debugCopy").textContent = "copied!";
setTimeout(() => {
document.getElementById("debugCopy").textContent = "copy";
}, 1500);
});
});
});
document.getElementById("debugClear").addEventListener("click", () => {
debugLog.innerHTML = "";
});
// ── Init ──────────────────────────────────────────────────────────────────────
async function init() {
// Don't eagerly show the loading screen — wait to hear from the background
// first. If it responds "ready" within 150ms (cache-hit cold start or warm
// worker), the user never sees the flash. Only show it if we know we have to.
let loadingTimer = setTimeout(() => {
setStatus("loading", "loading model");
showState(
"📦",
"Loading AI model…",
"This only happens once. Future searches are instant.",
);
}, 150);
// Check if debug is enabled in background
chrome.runtime.sendMessage({ type: "GET_DEBUG" }, (response) => {
if (response?.debug) {
debugEnabled = true;
debugToggle.style.display = "flex";
// Auto-open the debug panel since DEBUG=true
debugVisible = true;
debugPanel.classList.add("open");
debugToggle.textContent = "▲ debug";
refreshDebug();
}
});
chrome.runtime.sendMessage({ type: "INIT" }, (response) => {
clearTimeout(loadingTimer); // cancel the flash if we're already ready
if (chrome.runtime.lastError) {
setStatus("error", "error");
showState("❌", "Something went wrong", chrome.runtime.lastError.message);
return;
}
if (response?.status === "ready") {
onReady(response.count);
} else {
// We know we genuinely have to wait — show loading state now
setStatus("loading", "loading model");
showState(
"📦",
"Loading AI model…",
"This only happens once. Future searches are instant.",
);
pollReady();
}
});
}
function pollReady() {
if (pollInterval) clearInterval(pollInterval);
pollInterval = setInterval(() => {
chrome.runtime.sendMessage({ type: "STATUS" }, (response) => {
if (chrome.runtime.lastError) {
clearInterval(pollInterval);
setStatus("error", "error");
return;
}
if (debugVisible) refreshDebug();
if (response?.progress !== undefined) showProgress(response.progress);
if (response?.status === "ready") {
clearInterval(pollInterval);
hideProgress();
onReady(response.count);
} else if (response?.status === "indexing") {
setStatus("loading", "indexing");
showState(
"📚",
"Indexing your bookmarks…",
`Processing ${response.count || "..."} bookmarks.`,
);
} else if (response?.status === "loading") {
setStatus("loading", "loading model");
} else if (response?.status === "error") {
clearInterval(pollInterval);
setStatus("error", "error");
showState("❌", "Error", response.message || "Something went wrong.");
if (debugVisible) refreshDebug();
}
});
}, 700);
}
function onReady(count) {
if (pollInterval) clearInterval(pollInterval);
bookmarkCount = count || 0;
setStatus("ready", "ready");
footerInfo.textContent = `${bookmarkCount} bookmarks indexed`;
searchInput.disabled = false;
searchInput.focus();
showState("🔍", "Start typing…", "Describe the bookmark in plain English.");
if (debugVisible) refreshDebug();
}
// ── Search ────────────────────────────────────────────────────────────────────
searchInput.addEventListener("input", () => {
const q = searchInput.value.trim();
clearBtn.classList.toggle("visible", q.length > 0);
clearTimeout(debounceTimer);
if (!q) {
showState("🔍", "Start typing…", "Describe the bookmark in plain English.");
return;
}
if (q.length < 2) return;
setStatus("loading", "searching");
debounceTimer = setTimeout(() => doSearch(q), 280);
});
clearBtn.addEventListener("click", () => {
searchInput.value = "";
clearBtn.classList.remove("visible");
searchInput.focus();
showState("🔍", "Start typing…", "Describe the bookmark in plain English.");
setStatus("ready", "ready");
});
async function doSearch(query) {
chrome.runtime.sendMessage({ type: "SEARCH", query }, (response) => {
setStatus("ready", "ready");
if (chrome.runtime.lastError || !response) {
showState("❌", "Search failed", "Try re-indexing your bookmarks.");
return;
}
renderResults(response.results, query);
if (debugVisible) refreshDebug();
});
}
// ── Render ────────────────────────────────────────────────────────────────────
function renderResults(results, query) {
resultsArea.innerHTML = "";
if (!results || results.length === 0) {
showState(
"🤷",
"No matches found",
`Nothing close to "${query}" in your bookmarks.`,
);
return;
}
const header = document.createElement("div");
header.className = "results-header";
header.textContent = `${results.length} results`;
resultsArea.appendChild(header);
results.forEach((item) => {
const a = document.createElement("a");
a.className = "result-item";
a.href = item.url;
a.title = item.url;
a.target = "_blank";
a.rel = "noopener";
const favicon = document.createElement("img");
favicon.className = "result-favicon";
try {
favicon.src = `https://www.google.com/s2/favicons?domain=${new URL(item.url).hostname}&sz=32`;
} catch {
favicon.src = "";
}
favicon.onerror = () => {
const fb = document.createElement("div");
fb.className = "result-favicon-fallback";
fb.textContent = item.title?.[0]?.toUpperCase() || "?";
a.replaceChild(fb, favicon);
};
const body = document.createElement("div");
body.className = "result-body";
const title = document.createElement("div");
title.className = "result-title";
title.textContent = item.title || "Untitled";
const url = document.createElement("div");
url.className = "result-url";
try {
const u = new URL(item.url);
url.textContent = u.hostname + u.pathname.replace(/\/$/, "");
} catch {
url.textContent = item.url;
}
body.appendChild(title);
body.appendChild(url);
const score = document.createElement("div");
score.className = "result-score";
score.textContent = `${Math.round(item.score * 100)}%`;
a.appendChild(favicon);
a.appendChild(body);
a.appendChild(score);
a.addEventListener("click", (e) => {
e.preventDefault();
chrome.tabs.create({ url: item.url });
window.close();
});
resultsArea.appendChild(a);
});
}
// ── Re-index ──────────────────────────────────────────────────────────────────
reindexBtn.addEventListener("click", () => {
searchInput.value = "";
searchInput.disabled = true;
clearBtn.classList.remove("visible");
setStatus("loading", "indexing");
showState("📚", "Re-indexing…", "Rebuilding the bookmark index.");
showProgress(5);
chrome.runtime.sendMessage({ type: "REINDEX" }, () => pollReady());
});
// ── Start ─────────────────────────────────────────────────────────────────────
init();