diff --git a/script.js b/script.js index db76d51..b6cfa25 100644 --- a/script.js +++ b/script.js @@ -943,8 +943,24 @@ function renderPDFs() { const AD_FREQUENCY = 4; let adCounter = 1; + // --- OPTIMIZATION START --- + // Calculate global values once to avoid re-computation inside the loop + const now = new Date(); + const rawSearchTerm = searchInput.value.trim(); + let searchRegex = null; + if (rawSearchTerm) { + try { + // Case-insensitive regex for highlighting + searchRegex = new RegExp(`(${rawSearchTerm})`, 'gi'); + } catch (e) { + // Fallback for invalid regex chars if any + searchRegex = null; + } + } + // --- OPTIMIZATION END --- + filteredPdfs.forEach((pdf, index) => { - gridHTML += createPDFCard(pdf, favorites, index); + gridHTML += createPDFCard(pdf, favorites, index, searchRegex, now); // if ((index + 1) % AD_FREQUENCY === 0) { // const adData = getAdData(`slot_grid_${adCounter}`); @@ -969,15 +985,20 @@ function renderPDFs() { pdfGrid.innerHTML = gridHTML; } +const escapeHtml = (text) => { + if (!text) return ''; + return text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); +}; + // UPDATE THE FUNCTION SIGNATURE to include "index = 0" -function createPDFCard(pdf, favoritesList, index = 0) { +function createPDFCard(pdf, favoritesList, index = 0, searchRegex = null, now = new Date()) { const favorites = favoritesList || getFavorites(); const isFav = favorites.includes(pdf.id); const heartIconClass = isFav ? 'fas' : 'far'; const btnActiveClass = isFav ? 'active' : ''; const uploadDateObj = new Date(pdf.uploadDate); - const timeDiff = new Date() - uploadDateObj; + const timeDiff = now - uploadDateObj; const isNew = timeDiff < (7 * 24 * 60 * 60 * 1000); // 7 days const newBadgeHTML = isNew @@ -997,17 +1018,10 @@ function createPDFCard(pdf, favoritesList, index = 0) { year: 'numeric', month: 'short', day: 'numeric' }); - const escapeHtml = (text) => { - if (!text) return ''; - return text.replace(/&/g, "&").replace(//g, ">").replace(/"/g, """).replace(/'/g, "'"); - }; - const highlightText = (text) => { - const searchTerm = document.getElementById('searchInput').value.trim(); // Ensure direct access or pass it in const safeText = escapeHtml(text); - if (!searchTerm) return safeText; - const regex = new RegExp(`(${searchTerm})`, 'gi'); - return safeText.replace(regex, '$1'); + if (!searchRegex) return safeText; + return safeText.replace(searchRegex, '$1'); }; const safePdfString = JSON.stringify(pdf).replace(/"/g, '"'); diff --git a/verification_initial.png b/verification_initial.png new file mode 100644 index 0000000..a85d79d Binary files /dev/null and b/verification_initial.png differ diff --git a/verification_result.png b/verification_result.png new file mode 100644 index 0000000..3a6a92d Binary files /dev/null and b/verification_result.png differ diff --git a/verification_search.png b/verification_search.png new file mode 100644 index 0000000..1ae1ee5 Binary files /dev/null and b/verification_search.png differ