(function() { // Get template data and configuration const templateData = document.getElementById('template-data'); const type = templateData.getAttribute('data-type'); const hardCacheEnabled = templateData.getAttribute('data-hard-cache-enabled') === 'true'; // Track all favicon/image elements and their IDs let allMediaElements = []; let allMediaIds = []; const mediaMap = new Map(); // Add loading effects to image/favicon and associated text function addLoadingEffects(imgElement) { const container = imgElement.closest(type === 'image' ? '.image' : '.result_item'); if (!container) return; const titleSelector = type === 'image' ? '.img_title' : '.result-url'; const title = container.querySelector(titleSelector); imgElement.closest('.favicon-wrapper')?.classList.add('loading'); // if (title) title.classList.add('title-loading'); } // Remove loading effects when image/favicon loads function removeLoadingEffects(imgElement) { const container = imgElement.closest(type === 'image' ? '.image' : '.result_item'); const titleSelector = type === 'image' ? '.img_title' : '.result-url'; const title = container?.querySelector(titleSelector); imgElement.closest('.favicon-wrapper')?.classList.remove('loading'); if (title) title.classList.remove('title-loading'); if (type === 'image' && imgElement.src.endsWith('/images/globe.svg')) { container.remove(); } } // Handle image/favicon loading errors function handleImageError(imgElement, retryCount = 10, retryDelay = 200) { const isFavicon = !!imgElement.closest('.favicon-wrapper'); const container = imgElement.closest(type === 'image' ? '.image' : '.result_item'); const titleSelector = type === 'image' ? '.img_title' : '.result-url'; const title = container?.querySelector(titleSelector); const fullURL = imgElement.getAttribute('data-full'); if (retryCount > 0 && !imgElement.dataset.checked410) { imgElement.dataset.checked410 = '1'; // avoid infinite loop fetch(fullURL, { method: 'HEAD' }) .then(res => { if (res.status === 410) { fallbackToGlobe(imgElement); } else { setTimeout(() => { imgElement.src = fullURL; imgElement.onerror = () => handleImageError(imgElement, retryCount - 1, retryDelay); }, retryDelay); } }) .catch(() => { fallbackToGlobe(imgElement); }); } else { fallbackToGlobe(imgElement); } } function fallbackToGlobe(imgElement) { const type = document.getElementById('template-data').getAttribute('data-type'); const isFavicon = !!imgElement.closest('.favicon-wrapper'); const container = imgElement.closest(type === 'image' ? '.image' : '.result_item'); const titleSelector = type === 'image' ? '.img_title' : '.result-url'; const title = container?.querySelector(titleSelector); imgElement.closest('.favicon-wrapper')?.classList.remove('loading'); if (title) title.classList.remove('title-loading'); if (isFavicon) { const wrapper = imgElement.closest('.favicon-wrapper') || imgElement.parentElement; const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); svg.setAttribute("xmlns", "http://www.w3.org/2000/svg"); svg.setAttribute("viewBox", "0 -960 960 960"); svg.setAttribute("height", imgElement.height || "16"); svg.setAttribute("width", imgElement.width || "16"); svg.setAttribute("fill", "currentColor"); svg.classList.add("favicon", "globe-fallback"); svg.innerHTML = ``; imgElement.remove(); wrapper.appendChild(svg); } else if (type === 'image') { container?.remove(); } } // Shared configuration const statusCheckInterval = 500; const scrollThreshold = 500; const loadingIndicator = document.getElementById('message-bottom-right'); let loadingTimer; let isFetching = false; let page = parseInt(templateData.getAttribute('data-page')) || 1; let query = templateData.getAttribute('data-query'); let noMoreImages = false; function showLoadingMessage() { loadingIndicator.classList.add('visible'); } function hideLoadingMessage() { loadingIndicator.classList.remove('visible'); } function ensureScrollable() { if (noMoreImages) return; if (document.body.scrollHeight <= window.innerHeight) { fetchNextPage(); } } // Register a new media element for tracking function registerMediaElement(imgElement) { const id = imgElement.getAttribute('data-id'); if (!id) return; let wrapper = imgElement.closest('.favicon-wrapper'); if (!wrapper) { wrapper = document.createElement('span'); wrapper.classList.add('favicon-wrapper'); imgElement.replaceWith(wrapper); wrapper.appendChild(imgElement); } addLoadingEffects(imgElement); const tryLoadImage = (attempts = 25, delay = 300) => { fetch(`/image_status?image_ids=${id}`) .then(res => res.json()) .then(map => { const url = map[id]; if (!url) { if (attempts > 0) { // Exponential backoff with jitter const nextDelay = Math.min(delay * 2 + Math.random() * 100, 5000); setTimeout(() => tryLoadImage(attempts - 1, nextDelay), nextDelay); } else { fallbackToGlobe(imgElement); } } else if (url.endsWith('globe.svg') || url.endsWith('missing.svg')) { fallbackToGlobe(imgElement); } else { // Remove cache buster to leverage browser caching const newImg = imgElement.cloneNode(); newImg.src = url; newImg.onload = () => removeLoadingEffects(newImg); // Add retry mechanism for final load newImg.onerror = () => handleImageError(newImg, 3, 500); imgElement.parentNode.replaceChild(newImg, imgElement); } }) .catch(() => { if (attempts > 0) { const nextDelay = Math.min(delay * 2, 5000); setTimeout(() => tryLoadImage(attempts - 1, nextDelay), nextDelay); } else { fallbackToGlobe(imgElement); } }); }; // Initial load attempt with retry handler imgElement.src = imgElement.getAttribute('data-full'); imgElement.onload = () => removeLoadingEffects(imgElement); // Start polling immediately instead of waiting for error setTimeout(() => tryLoadImage(), 500); // Start polling after initial load attempt // Store reference in tracking map if (!mediaMap.has(id)) mediaMap.set(id, []); mediaMap.get(id).push(imgElement); } function pollFaviconUntilReady(imgElement, id, retries = 8, delay = 700) { let attempts = 0; function poll() { fetch(`/image_status?image_ids=${id}`) .then(res => res.json()) .then(map => { const url = map[id]; if (url && !url.endsWith('globe.svg') && !url.endsWith('missing.svg')) { const newImg = imgElement.cloneNode(); newImg.src = url + "?v=" + Date.now(); newImg.onload = () => removeLoadingEffects(newImg); newImg.onerror = () => fallbackToGlobe(newImg); imgElement.parentNode.replaceChild(newImg, imgElement); } else if (attempts < retries) { attempts++; setTimeout(poll, delay); } else { fallbackToGlobe(imgElement); } }).catch(() => { if (attempts < retries) { attempts++; setTimeout(poll, delay); } else { fallbackToGlobe(imgElement); } }); } poll(); } // Check status of all tracked media elements function checkMediaStatus() { const allIds = Array.from(mediaMap.keys()); if (allIds.length === 0) return; const idGroups = []; for (let i = 0; i < allIds.length; i += 50) { idGroups.push(allIds.slice(i, i + 50)); } const processGroups = async () => { const stillPending = new Map(); for (const group of idGroups) { try { const response = await fetch(`/image_status?image_ids=${group.join(',')}`); const statusMap = await response.json(); group.forEach(id => { const elements = mediaMap.get(id); const resolved = statusMap[id]; if (!elements) return; if (resolved && resolved !== 'pending') { elements.forEach(img => { img.src = resolved; img.onload = () => removeLoadingEffects(img); img.onerror = () => handleImageError(img); }); } else { stillPending.set(id, elements); } }); } catch (err) { console.error('Status check failed:', err); group.forEach(id => { if (mediaMap.has(id)) { stillPending.set(id, mediaMap.get(id)); } }); } } for (const id of Array.from(mediaMap.keys())) { if (!stillPending.has(id)) { mediaMap.delete(id); } } }; processGroups(); } function fetchNextPage() { if (isFetching || noMoreImages) return; loadingTimer = setTimeout(() => { showLoadingMessage(); }, 150); isFetching = true; page += 1; fetch(`/search?q=${encodeURIComponent(query)}&t=${type}&p=${page}&ajax=true`) .then(response => response.text()) .then(html => { clearTimeout(loadingTimer); hideLoadingMessage(); let tempDiv = document.createElement('div'); tempDiv.innerHTML = html; let newItems = tempDiv.querySelectorAll(type === 'image' ? '.image' : '.result_item'); if (newItems.length > 0) { let resultsContainer = document.querySelector(type === 'image' ? '.images' : '.results'); newItems.forEach(item => { let clonedItem = item.cloneNode(true); resultsContainer.appendChild(clonedItem); // Register any new media elements const img = clonedItem.querySelector('img[data-id]'); if (img) { registerMediaElement(img); } }); ensureScrollable(); } else { noMoreImages = true; } isFetching = false; }) .catch(error => { clearTimeout(loadingTimer); hideLoadingMessage(); console.error('Fetch error:', error); isFetching = false; }); } // Initialize all existing media elements function initializeMediaElements() { document.querySelectorAll('img[data-id]').forEach(img => { registerMediaElement(img); }); } function startStatusPolling() { checkMediaStatus(); setInterval(checkMediaStatus, statusCheckInterval); } if (document.readyState === 'complete') { initializeMediaElements(); if (hardCacheEnabled) startStatusPolling(); } else { window.addEventListener('load', () => { initializeMediaElements(); if (hardCacheEnabled) startStatusPolling(); }); } // Infinite scroll handler window.addEventListener('scroll', () => { if (isFetching || noMoreImages) return; if (window.innerHeight + window.scrollY >= document.body.offsetHeight - scrollThreshold) { fetchNextPage(); } }); // // Clean up on page unload // window.addEventListener('beforeunload', () => { // if (statusCheckTimeout) { // clearTimeout(statusCheckTimeout); // } // }); })();