diff --git a/manifest.json b/manifest.json index 24ccff6..30c8cd2 100644 --- a/manifest.json +++ b/manifest.json @@ -14,7 +14,8 @@ "permissions": [ "storage", "clipboardWrite", - "activeTab" + "activeTab", + "contextMenus" ], "host_permissions": [ "https://archive.org/*", diff --git a/src/background/index.js b/src/background/index.js index 217075e..93cec10 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -17,3 +17,33 @@ browser.runtime.onMessage.addListener(async (msg, sender) => { } }); +// Context menu to collect links on archive.org /download/* pages +browser.runtime.onInstalled.addListener(() => { + try { + browser.contextMenus.create({ + id: 'aolg-collect', + title: 'Collect archive.org links', + contexts: ['page'], + documentUrlPatterns: ['https://archive.org/download/*'] + }); + } catch (e) { + // ignore if already exists or not supported + } +}); + +browser.contextMenus.onClicked.addListener(async (info, tab) => { + if (info.menuItemId !== 'aolg-collect' || !tab?.id) return; + try { + const items = await browser.tabs.sendMessage(tab.id, { type: 'collectLinks' }); + const count = (items || []).length; + await browser.storage.local.set({ + lastCollected: { tabId: tab.id, url: tab.url, time: Date.now(), count }, + lastItems: items + }); + try { await browser.action.setBadgeBackgroundColor({ color: '#3b82f6', tabId: tab.id }); } catch (e) {} + try { await browser.action.setBadgeText({ text: count ? String(count) : '', tabId: tab.id }); } catch (e) {} + try { if (browser.action.openPopup) await browser.action.openPopup(); } catch (e) {} + } catch (e) { + // Swallow errors; context menu is best-effort + } +}); diff --git a/src/popup/index.html b/src/popup/index.html index 52fef09..3dca231 100644 --- a/src/popup/index.html +++ b/src/popup/index.html @@ -32,9 +32,15 @@