diff --git a/manifest.json b/manifest.json index 30c8cd2..ea6666e 100644 --- a/manifest.json +++ b/manifest.json @@ -1,9 +1,9 @@ { - "manifest_version": 3, + "manifest_version": 2, "name": "Archive.org Link Grabber", "version": "0.1.0", "description": "Filter and export archive.org /download links; copy or send to aria2 RPC.", - "action": { + "browser_action": { "default_title": "Archive.org Link Grabber", "default_popup": "src/popup/index.html" }, @@ -15,16 +15,16 @@ "storage", "clipboardWrite", "activeTab", - "contextMenus" - ], - "host_permissions": [ + "contextMenus", "https://archive.org/*", "http://localhost:6800/*", "https://localhost:6800/*" ], "background": { - "service_worker": "src/background/index.js", - "type": "module" + "scripts": [ + "src/lib/aria2-bg.js", + "src/background/index.js" + ] }, "content_scripts": [ { diff --git a/src/background/index.js b/src/background/index.js index 93cec10..84dcd9b 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -1,4 +1,4 @@ -import { addUrisBatch } from "../lib/aria2.js"; +// In MV2 background, aria2 helpers are exposed on globalThis by src/lib/aria2-bg.js browser.runtime.onMessage.addListener(async (msg, sender) => { if (!msg || !msg.type) return; @@ -9,7 +9,7 @@ browser.runtime.onMessage.addListener(async (msg, sender) => { return { ok: false, error: 'Missing endpoint or URIs' }; } try { - const res = await addUrisBatch({ endpoint, secret, uris, options }); + const res = await globalThis.addUrisBatch({ endpoint, secret, uris, options }); return { ok: true, result: res }; } catch (err) { return { ok: false, error: String(err && err.message || err) }; @@ -40,9 +40,8 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => { 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) {} + try { await browser.browserAction.setBadgeBackgroundColor({ color: '#3b82f6' }); } catch (e) {} + try { await browser.browserAction.setBadgeText({ text: count ? String(count) : '' }); } catch (e) {} } catch (e) { // Swallow errors; context menu is best-effort } diff --git a/src/lib/aria2-bg.js b/src/lib/aria2-bg.js new file mode 100644 index 0000000..0e2051c --- /dev/null +++ b/src/lib/aria2-bg.js @@ -0,0 +1,58 @@ +// Background-friendly (non-module) aria2 helpers +// Exposes addUri, addUrisBatch, and getVersion on globalThis + +(function(){ + function rpcCall(endpoint, body) { + return fetch(endpoint, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(body) + }).then(async (res) => { + if (!res.ok) { + const text = await res.text().catch(() => String(res.status)); + throw new Error(`aria2 RPC error ${res.status}: ${text}`); + } + return res.json(); + }); + } + + function addUri({ endpoint, secret, uri, options = {} }) { + const body = { + jsonrpc: "2.0", + id: "archive-org-link-grabber:" + Date.now(), + method: "aria2.addUri", + params: [ + secret ? `token:${secret}` : undefined, + [uri], + options + ].filter(v => v !== undefined) + }; + return rpcCall(endpoint, body); + } + + async function addUrisBatch({ endpoint, secret, uris, options = {} }) { + const results = []; + for (const uri of uris) { + // eslint-disable-next-line no-await-in-loop + const r = await addUri({ endpoint, secret, uri, options }); + results.push(r); + } + return results; + } + + function getVersion({ endpoint, secret }) { + const body = { + jsonrpc: "2.0", + id: "archive-org-link-grabber:getVersion:" + Date.now(), + method: "aria2.getVersion", + params: [secret ? `token:${secret}` : undefined].filter(Boolean) + }; + return rpcCall(endpoint, body); + } + + // expose + globalThis.addUrisBatch = addUrisBatch; + globalThis.addUri = addUri; + globalThis.getVersion = getVersion; +})(); +