From 92ec0a9a7488879de3308a1e8dd3f2e1a3f8eb6b Mon Sep 17 00:00:00 2001 From: wagesj45 Date: Fri, 22 Aug 2025 00:29:48 -0500 Subject: [PATCH] fix(collect): include relative links under /download and add fallback collection path - Content script: base href check via absolute URL; scope to listing container - Popup: fallback to background-assisted collection when no receiver exists - Background: expose collect.fromTab to run injection + collection - Add debug logging for visibility --- src/background/index.js | 11 +++++++++++ src/content/collect.js | 16 ++++++++++------ src/popup/index.js | 10 +++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/background/index.js b/src/background/index.js index 798484f..8164672 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -15,6 +15,17 @@ browser.runtime.onMessage.addListener(async (msg, sender) => { return { ok: false, error: String(err && err.message || err) }; } } + + if (msg.type === 'collect.fromTab') { + const tabId = msg.tabId || sender?.tab?.id; + if (!tabId) return { ok: false, error: 'No tabId' }; + try { + const items = await collectFromTab(tabId); + return { ok: true, items }; + } catch (err) { + return { ok: false, error: String(err && err.message || err) }; + } + } }); async function collectFromTab(tabId) { diff --git a/src/content/collect.js b/src/content/collect.js index 2eaadac..49795b6 100644 --- a/src/content/collect.js +++ b/src/content/collect.js @@ -36,12 +36,14 @@ } async function collectLinks() { - const anchors = Array.from(document.querySelectorAll('a[href]')); + const scope = document.querySelector('.download-directory-listing') || document; + const anchors = Array.from(scope.querySelectorAll('a[href]')); const items = []; for (const a of anchors) { - const href = a.getAttribute('href') || ''; - // Only include likely file links under /download/ - if (href.includes('/download/')) { + let path = ''; + try { path = new URL(a.href, document.baseURI).pathname; } catch (_) {} + // Only include links that resolve under /download/ (relative or absolute) + if (path.startsWith('/download/')) { const item = toItem(a); if (item && item.name) items.push(item); } @@ -52,8 +54,10 @@ // Respond to messages from the popup to collect links browser.runtime.onMessage.addListener((msg, sender) => { if (msg && msg.type === 'collectLinks') { - return collectLinks(); + return collectLinks().then((items) => { + try { console.debug('[aolg] collected items:', items.length); } catch (_) {} + return items; + }); } }); })(); - diff --git a/src/popup/index.js b/src/popup/index.js index 201dbcd..78419e9 100644 --- a/src/popup/index.js +++ b/src/popup/index.js @@ -47,7 +47,15 @@ async function refresh() { try { const tabId = await getActiveTabId(); if (!tabId) throw new Error('No active tab'); - const items = await browser.tabs.sendMessage(tabId, { type: 'collectLinks' }); + let items = []; + try { + items = await browser.tabs.sendMessage(tabId, { type: 'collectLinks' }); + } catch (e) { + // Fallback: ask background to inject and collect + const res = await browser.runtime.sendMessage({ type: 'collect.fromTab', tabId }); + if (res?.ok) items = res.items || []; + else throw new Error(res?.error || e?.message || 'collect failed'); + } allItems = items || []; updateCount(); } catch (e) {