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
This commit is contained in:
		
					parent
					
						
							
								f7e7b3fb99
							
						
					
				
			
			
				commit
				
					
						92ec0a9a74
					
				
			
		
					 3 changed files with 30 additions and 7 deletions
				
			
		| 
						 | 
					@ -15,6 +15,17 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
 | 
				
			||||||
      return { ok: false, error: String(err && err.message || err) };
 | 
					      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) {
 | 
					async function collectFromTab(tabId) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -36,12 +36,14 @@
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async function collectLinks() {
 | 
					  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 = [];
 | 
					    const items = [];
 | 
				
			||||||
    for (const a of anchors) {
 | 
					    for (const a of anchors) {
 | 
				
			||||||
      const href = a.getAttribute('href') || '';
 | 
					      let path = '';
 | 
				
			||||||
      // Only include likely file links under /download/
 | 
					      try { path = new URL(a.href, document.baseURI).pathname; } catch (_) {}
 | 
				
			||||||
      if (href.includes('/download/')) {
 | 
					      // Only include links that resolve under /download/ (relative or absolute)
 | 
				
			||||||
 | 
					      if (path.startsWith('/download/')) {
 | 
				
			||||||
        const item = toItem(a);
 | 
					        const item = toItem(a);
 | 
				
			||||||
        if (item && item.name) items.push(item);
 | 
					        if (item && item.name) items.push(item);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
| 
						 | 
					@ -52,8 +54,10 @@
 | 
				
			||||||
  // Respond to messages from the popup to collect links
 | 
					  // Respond to messages from the popup to collect links
 | 
				
			||||||
  browser.runtime.onMessage.addListener((msg, sender) => {
 | 
					  browser.runtime.onMessage.addListener((msg, sender) => {
 | 
				
			||||||
    if (msg && msg.type === 'collectLinks') {
 | 
					    if (msg && msg.type === 'collectLinks') {
 | 
				
			||||||
      return collectLinks();
 | 
					        return collectLinks().then((items) => {
 | 
				
			||||||
 | 
					          try { console.debug('[aolg] collected items:', items.length); } catch (_) {}
 | 
				
			||||||
 | 
					          return items;
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,7 +47,15 @@ async function refresh() {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    const tabId = await getActiveTabId();
 | 
					    const tabId = await getActiveTabId();
 | 
				
			||||||
    if (!tabId) throw new Error('No active tab');
 | 
					    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 || [];
 | 
					    allItems = items || [];
 | 
				
			||||||
    updateCount();
 | 
					    updateCount();
 | 
				
			||||||
  } catch (e) {
 | 
					  } catch (e) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue