import { addUrisBatch } from "../lib/aria2.js"; browser.runtime.onMessage.addListener(async (msg, sender) => { if (!msg || !msg.type) return; if (msg.type === 'aria2.send') { const { endpoint, secret, uris, options } = msg.payload || {}; if (!endpoint || !Array.isArray(uris) || uris.length === 0) { return { ok: false, error: 'Missing endpoint or URIs' }; } try { const res = await addUrisBatch({ endpoint, secret, uris, options }); return { ok: true, result: res }; } catch (err) { return { ok: false, error: String(err && err.message || err) }; } } }); // 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 } });