From 724940ebf7f4899fc3f7e8123e3cb2105df9dfc8 Mon Sep 17 00:00:00 2001 From: tegwick Date: Thu, 15 Jan 2026 15:14:04 +0100 Subject: [PATCH] Fix: Restore fallback to check recent downloads When the persistent queue is empty (e.g., after extension reload), fall back to checking recent downloads from Chrome downloads API. Co-Authored-By: Claude Opus 4.5 --- src/popup/popup.ts | 62 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/src/popup/popup.ts b/src/popup/popup.ts index 45630a0..e4fbaa2 100644 --- a/src/popup/popup.ts +++ b/src/popup/popup.ts @@ -164,7 +164,6 @@ async function loadPDFQueue() { const exists = pdfQueue.some(p => p.url === currentTabPDF.url); if (!exists) { console.log('[Popup] Adding current tab PDF to queue:', currentTabPDF.filename); - // Add to queue (will be added to persistent queue when user interacts) const entry: PDFQueueEntry = { ...currentTabPDF, status: 'pending' @@ -173,10 +172,71 @@ async function loadPDFQueue() { } } + // Fallback: check recent downloads if queue is still empty + if (pdfQueue.length === 0) { + console.log('[Popup] Queue empty, checking recent downloads...'); + const recentPDFs = await checkRecentDownloads(); + for (const pdf of recentPDFs) { + const exists = pdfQueue.some(p => p.url === pdf.url); + if (!exists) { + pdfQueue.push({ + ...pdf, + status: 'pending' + }); + } + } + } + // Render the list renderPDFList(); } +/** + * Check recent downloads for PDFs (fallback mechanism) + */ +async function checkRecentDownloads(): Promise { + return new Promise((resolve) => { + chrome.downloads.search( + { + limit: 20, + orderBy: ['-startTime'] + }, + (items) => { + console.log('[Popup] Checked recent downloads:', items.length, 'items'); + + const pdfs: DetectedPDF[] = []; + + for (const item of items) { + if ( + item.state === 'complete' && + (item.filename.toLowerCase().endsWith('.pdf') || item.mime === 'application/pdf') + ) { + let domain = 'unknown'; + try { + const urlObj = new URL(item.url); + domain = urlObj.hostname; + } catch (e) { + // Keep default + } + + pdfs.push({ + id: `download-${item.id}`, + filename: item.filename.split('/').pop() || item.filename, + url: item.url, + size: item.fileSize, + timestamp: new Date(item.startTime).getTime(), + sourceDomain: domain + }); + } + } + + console.log('[Popup] Found', pdfs.length, 'PDF downloads'); + resolve(pdfs); + } + ); + }); +} + /** * Render the PDF list */