From df202b236d6972cf650590135d27225d99ea9cda Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 22 Feb 2026 13:17:52 +0000 Subject: [PATCH] Refactor viewPDF to use ID and data attributes Optimized the `createPDFCard` function to pass the PDF ID via `data-id` attribute instead of serializing the entire PDF object into the `onclick` handler. This reduces DOM size and rendering time, and improves security by avoiding complex string escaping in inline handlers. Updated `viewPDF` to accept a string ID and look up the PDF object from the global `pdfDatabase`. Co-authored-by: MrAlokTech <107493955+MrAlokTech@users.noreply.github.com> --- script.js | 18 +++++++++++++++--- verification.png | Bin 154665 -> 0 bytes 2 files changed, 15 insertions(+), 3 deletions(-) delete mode 100644 verification.png diff --git a/script.js b/script.js index bdc06f3..ebe9dcb 100644 --- a/script.js +++ b/script.js @@ -788,7 +788,18 @@ function getEmbeddableUrl(url) { return `https://docs.google.com/gview?embedded=true&url=${encodeURIComponent(url)}`; } -async function viewPDF(pdf, pushToHistory = true) { +async function viewPDF(pdfOrId, pushToHistory = true) { + // Optimization: Support passing ID string to avoid large HTML payload + const pdf = (typeof pdfOrId === 'string') + ? pdfDatabase.find(p => p.id === pdfOrId) + : pdfOrId; + + if (!pdf) { + console.error('PDF not found:', pdfOrId); + showToast('Error: PDF data not found.', 'error'); + return; + } + const originalPdfPath = pdf.pdfUrl; logInteraction('view_pdf', pdf.title, pdf.id); @@ -1020,7 +1031,8 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) { return safeText.replace(highlightRegex, '$1'); }; - const safePdfString = JSON.stringify(pdf).replace(/"/g, '"'); + // Optimization: Removed expensive JSON.stringify(pdf) here. + // Instead, we pass the ID to viewPDF() which looks it up in O(1) or O(n). // --- NEW: Calculate Stagger Delay --- // Cap at 1s (20 items) so the list doesn't feel unresponsive @@ -1037,7 +1049,7 @@ function createPDFCard(pdf, favoritesList, index = 0, highlightRegex = null) {

${highlightText(pdf.description)}

-