-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
499 lines (412 loc) · 15.6 KB
/
Copy pathbackground.js
File metadata and controls
499 lines (412 loc) · 15.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
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
// background.js
import {
CACHE_TTL_MS,
FLUSH_MS,
CLEAR_IN_TIME,
EP,
fetchWithAuth,
dataUrlToBlob,
timestampName,
refreshAccessToken,
getTokens,
setBadge,
clearBadge,
makeQueryKey
} from "./shared.js";
// ---------------------- Caching ----------------------
const queryCache = new Map(); // key -> { ts, data }
function putCache(searchText, data) {
queryCache.set(makeQueryKey(searchText), { ts: Date.now(), data });
}
function getCache(searchText) {
const k = makeQueryKey(searchText);
const entry = queryCache.get(k);
if (!entry) return null;
if (Date.now() - entry.ts > CACHE_TTL_MS) { queryCache.delete(k); return null; }
return entry.data;
}
// ---------------------- Batching ----------------------
let actionBuffer = new Set();
let flushTimer = null;
let lastQuery = null; // ✅ FIXED: declared
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
try {
const tab = await chrome.tabs.get(tabId);
if (!tab?.url) return;
if (await isDomainExcluded(tab.url)) return;
await triggerAutoRefreshIfEnabled();
queueUserAction(tab);
}
catch (e) {
console.warn("[BG] onActivated error", e);
}
});
chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {
try {
if (!(info.status === "complete" || "title" in info || "url" in info)) return;
if (!tab?.url) return;
if (await isDomainExcluded(tab.url)) return;
if (info.url || info.title) queueUserAction(tab);
} catch (e) {
console.warn("[BG] onUpdated error", e);
}
});
async function isDomainExcluded(url) {
if (!url) return false;
let domain = "";
try {
domain = new URL(url).hostname;
} catch {
return false;
}
const { disabledDomains = [] } = await chrome.storage.sync.get(["disabledDomains"]);
if (disabledDomains.includes(domain)) {
console.log(`[BG] Skipping domain ${domain} (excluded by user)`);
return true;
}
return false;
}
async function triggerAutoRefreshIfEnabled() {
const { autoRefreshEnabled } = await chrome.storage.sync.get(["autoRefreshEnabled"]);
if (!autoRefreshEnabled) return;
try {
await chrome.runtime.sendMessage({ type: "REFRESH_IF_OPEN" });
} catch (e) {
console.warn("[BG] autoRefresh message failed", e);
}
}
// Push a user action into the buffer and (re)start the flush timer
function queueUserAction(tab) {
let domain = "";
try { domain = new URL(tab.url).hostname; } catch {}
const title = (tab.title || "").trim();
const key = `${title} ${domain}`.trim();
if (!key) return;
actionBuffer.add(key);
// schedule flush
if (flushTimer) {
clearTimeout(flushTimer);
}
flushTimer = setTimeout(flushActions, FLUSH_MS);
}
async function flushActions() {
flushTimer = null;
if (!actionBuffer.size) {
return;
}
const currentQuery = Array.from(actionBuffer).join(" || ");
actionBuffer.clear();
if (!currentQuery) {
await clearBadge();
return;
}
if (currentQuery === lastQuery) {
return;
}
lastQuery = currentQuery;
try {
const { has } = await hasResultsFor(currentQuery);
if (has) {
setBadge("●", "#3b82f6");
setTimeout(() => clearBadge(), CLEAR_IN_TIME);
} else {
await clearBadge();
}
} catch (err) {
console.error("[flushActions error]", err);
await clearBadge();
}
}
async function hasResultsFor(searchText) {
try {
const { access_token, refresh_token } = await getTokens();
if (!access_token && !refresh_token) {
return { has: false, data: null };
}
const r = await fetchWithAuth(EP.RELEVANT, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ searchText })
});
if (!r.ok) return { has: false, data: null };
const j = await r.json().catch(() => null);
if (!j) return { has: false, data: null };
const arr = j?.images || j?.results || j?.items || [];
const threshold = 0.25;
const topScore = arr.length > 0 && typeof arr[0].hybrid_score === "number"
? arr[0].hybrid_score
: 0;
// ✅ If topScore passes, return full list
if (topScore >= threshold) {
putCache(searchText, j);
const ret = { has: true, data: j };
return ret;
}
else {
console.log("[hasResultsFor] below threshold → no results");
return { has: false, data: null };
}
}
catch {
return { has: false, data: null };
}
}
// ---------------------- User Tier and Saves ----------------------
let userTierInfo = {
tier: "Free",
currentSaves: 0,
maxSaves: 3,
uploadsLeft: 3,
};
async function fetchUserTierInfo() {
try {
const { access_token } = await getTokens();
if (!access_token) {
console.log("[BG] No access token, skipping tier info fetch.");
return;
}
const res = await fetchWithAuth(EP.USER_TIER_INFO);
if (!res.ok) {
console.warn(`[BG] Failed to fetch user tier info: ${res.status}`);
// If 401, tokens might be bad, prompt login
if (res.status === 401) {
chrome.runtime.sendMessage({ type: "PROMPT_LOGIN" }).catch(() => {});
}
return;
}
const data = await res.json();
userTierInfo = {
tier: data.tier || "Free",
currentSaves: data.current_saves ?? 0,
maxSaves: data.max_saves ?? 3,
uploadsLeft: data.uploads_left ?? 3
};
// console.log("[BG] fetchUserTierInfo userTierInfo:", userTierInfo);
// Update side panel if it's open
chrome.runtime.sendMessage({ type: "UPDATE_TIER_INFO", data: userTierInfo }).catch(() => {});
// Update context menu state based on new limits
updateContextMenuState();
} catch (err) {
console.error("[BG] Error fetching user tier info:", err);
}
}
async function incrementSaveCounter() {
userTierInfo.currentSaves++;
console.log("[BG] Save counter incremented:", userTierInfo.currentSaves);
// Update side panel if it's open
chrome.runtime.sendMessage({ type: "UPDATE_TIER_INFO", data: userTierInfo }).catch(() => {});
updateContextMenuState();
}
// ---------------------- Server ----------------------
async function uploadScreenshot({ dataUrl, pageUrl = "", pageTitle = "", selectionText = "" }) {
try {
if (!dataUrl) throw new Error("No dataUrl provided");
const blob = dataUrlToBlob(dataUrl);
const file = new File([blob], timestampName(), { type: blob.type });
const form = new FormData();
form.append("image", file); // field name expected by backend
// form.append("source", "chrome_screenshot");
form.append("page_url", pageUrl);
form.append("page_title", pageTitle);
form.append("selection", selectionText);
const res = await fetchWithAuth(EP.UPLOAD_IMAGE, { method: "POST", body: form });
if (!res.ok) throw new Error(`Upload failed: ${res.status}`);
// If upload is successful, increment the counter
await incrementSaveCounter();
return res.json().catch(() => ({}));
}
catch (err) {
console.error("[BG] uploadScreenshot error", err);
}
}
async function uploadImageUrl({ imageUrl, pageUrl = "" }) {
try {
if (!imageUrl) throw new Error("No imageUrl provided");
const form = new FormData();
form.append("image_url", imageUrl); // server expects this
form.append("post_url", pageUrl || "-");
const res = await fetchWithAuth(EP.UPLOAD_IMAGEURL, { method: "POST", body: form });
if (!res.ok) throw new Error(`Upload URL failed: ${res.status}`);
// If upload is successful, increment the counter
await incrementSaveCounter();
return res.json().catch(() => ({}));
}
catch (err) {
console.error("[BG] uploadScreenshot error", err);
}
}
// ---------------------- Login ----------------------
chrome.runtime.onInstalled.addListener(async (details) => {
if (details.reason === "install") {
console.log("[BG] Installed so opening login.html...");
await chrome.tabs.create({ url: chrome.runtime.getURL("login.html") });
}
if (details.reason === "update") {
console.log("[BG] Updated so testing tokens...");
// await chrome.tabs.create({ url: chrome.runtime.getURL("login.html") });
const { access_token, refresh_token } = await getTokens();
if (!access_token && refresh_token) {
console.log(`[BG] no access token but has refresh token so refreshing the access token`);
try {
const newAccess = await refreshAccessToken();
console.log("[BG] Refreshed access token OK:", newAccess ? "yes" : "no");
await fetchUserTierInfo(); // Fetch tier info after successful refresh
} catch (err) {
console.warn("[BG] Refresh failed, clearing tokens", err);
await clearTokens();
await chrome.tabs.create({ url: chrome.runtime.getURL("login.html") });
}
}
else if (!access_token || !refresh_token) {
console.log(`[BG] No access token or refresh token so prompt re-login`);
await chrome.tabs.create({ url: chrome.runtime.getURL("login.html") });
}
else if (access_token && refresh_token) {
console.log(`[BG] Got both tokens!`);
await fetchUserTierInfo(); // Fetch tier info if tokens already exist
}
}
});
// ---------------------- Extension Icon ----------------------
chrome.action.onClicked.addListener((tab) => {
chrome.sidePanel.setOptions({ path: "sidepanel.html", enabled: true }, async () => {
await chrome.sidePanel.open({ windowId: tab.windowId });
// Clear badge when panel is opened
await clearBadge();
// Ask the sidepanel to refresh itself if it's already open
chrome.runtime.sendMessage({ type: "REFRESH_IF_OPEN" }).catch(() => {});
// Also fetch user tier info when sidepanel is opened
await fetchUserTierInfo();
});
});
// ---------------------- Context Menu ----------------------
function updateContextMenuState() {
const canSave = userTierInfo.uploadsLeft > 0;
chrome.contextMenus.update("forgor-capture-upload", {
enabled: canSave,
});
chrome.contextMenus.update("forgor-upload-image-url", {
enabled: canSave,
});
// console.log(`[BG] Context menu enabled: ${canSave} (Saves: ${userTierInfo.currentSaves}/${userTierInfo.maxSaves})`);
}
chrome.runtime.onInstalled.addListener(() => {
// Existing screenshot menu
chrome.contextMenus.create({
id: "forgor-capture-upload",
title: "FORGOR: Save screenshot",
contexts: ["page", "selection", "image", "link", "video", "audio"]
});
// Upload the specific image you right-clicked
chrome.contextMenus.create({
id: "forgor-upload-image-url",
title: "FORGOR: Save image",
contexts: ["image"]
});
// Initialize context menu state
updateContextMenuState();
});
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
// Check save limits before attempting any action
if (userTierInfo.currentSaves >= userTierInfo.maxSaves) {
console.warn("[BG] Save limit reached, context menu action blocked.");
await setBadge("🚫", "#ff0000");
setTimeout(() => clearBadge(), CLEAR_IN_TIME);
return;
}
// upload the image the user right-clicked
if (info.menuItemId === "forgor-upload-image-url") {
try {
await setBadge("...", "#ffa500");
const pageUrl = info.pageUrl || (tab && tab.url) || "";
const srcUrl = info.srcUrl || "";
if (!srcUrl) throw new Error("No srcUrl on image context");
if (srcUrl.startsWith("data:")) {
// Fallback for inline/data images: upload the actual bytes
await uploadScreenshot({
dataUrl: srcUrl,
pageUrl,
pageTitle: tab?.title || "",
selectionText: info.selectionText || ""
});
} else {
// Normal web image: use the URL endpoint
await uploadImageUrl({ imageUrl: srcUrl, pageUrl });
}
await setBadge("✔", "#b1ff7cff");
}
catch (err) {
console.error(err);
await setBadge("❗", "#ff0000");
}
setTimeout(() => clearBadge(), CLEAR_IN_TIME);
return; // Prevent falling through to the screenshot branch
}
// Existing screenshot menu
if (info.menuItemId === "forgor-capture-upload") {
try {
await setBadge("...", "#ffa500");
const dataUrl = await chrome.tabs.captureVisibleTab(tab.windowId, { format: "png" });
const payload = {
dataUrl,
pageUrl: info.pageUrl || (tab && tab.url) || "",
pageTitle: tab && tab.title ? tab.title : "",
selectionText: info.selectionText || ""
};
await uploadScreenshot(payload);
await setBadge("✔", "#b1ff7cff");
}
catch (err) {
console.error(err);
await setBadge("❗", "#ff0000");
}
setTimeout(() => clearBadge(), CLEAR_IN_TIME);
return;
};
});
// ---------------------- General ----------------------
let loginTabOpened = false;
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
(async () => {
if (msg?.type === "PROMPT_LOGIN") {
if (!loginTabOpened) {
loginTabOpened = true;
chrome.tabs.create({ url: chrome.runtime.getURL("login.html") })
.catch(e => console.warn("[AUTH] Failed to open login tab", e));
setTimeout(() => { loginTabOpened = false; }, 10000);
}
sendResponse({ ok: true });
return;
}
if (msg?.type === "GET_TOKENS") {
sendResponse(await getTokens());
return;
}
// Sidebar asks for cached-or-fetch
if (msg?.type === "QUERY_CACHED_OR_FETCH") {
const key = msg.searchText || "";
const cached = getCache(key);
if (cached) {
sendResponse({ ok: true, body: JSON.stringify(cached), fromCache: true });
return;
}
// ✅ reuse hasResultsFor so filtering applies
const result = await hasResultsFor(key);
sendResponse({ ok: true, body: JSON.stringify(result.data || {}), fromCache: false });
return;
}
// Side panel requests tier info
if (msg?.type === "GET_TIER_INFO") {
sendResponse(userTierInfo);
return;
}
})();
return true; // async
});
// Initial fetch of user tier info when background script starts (e.g., browser start, extension reload)
// This ensures the context menu is correctly initialized even if the side panel isn't opened first.
getTokens().then(({ access_token }) => {
if (access_token) {
fetchUserTierInfo();
}
});