1const STORAGE_KEY = 'photohuman_viewed'; 2const COOKIE_NAME = 'photohuman_viewed'; 3const MAX_ITEMS = 8; 4 5const readServerViewed = () => { 6 const bootstrap = document.getElementById('photohuman-viewed-bootstrap'); 7 8 if (!bootstrap) { 9 return null; 10 } 11 12 try { 13 const parsed = JSON.parse(bootstrap.textContent); 14 15 return Array.isArray(parsed) ? parsed.map((id) => Number(id)).filter(Boolean) : []; 16 } catch (error) { 17 return null; 18 } 19}; 20 21const readLocalViewed = () => { 22 try { 23 const raw = localStorage.getItem(STORAGE_KEY); 24 const parsed = raw ? JSON.parse(raw) : []; 25 26 return Array.isArray(parsed) ? parsed.map((id) => Number(id)).filter(Boolean) : []; 27 } catch (error) { 28 return []; 29 } 30}; 31 32const writeViewed = (viewed) => { 33 const normalized = []; 34 const seen = new Set(); 35 36 viewed.forEach((id) => { 37 const productId = Number(id); 38 39 if (!productId || seen.has(productId)) { 40 return; 41 } 42 43 seen.add(productId); 44 normalized.push(productId); 45 }); 46 47 const nextViewed = normalized.slice(0, MAX_ITEMS); 48 const encoded = JSON.stringify(nextViewed); 49 50 localStorage.setItem(STORAGE_KEY, encoded); 51 document.cookie = `${COOKIE_NAME}=${encodeURIComponent(encoded)}; path=/; max-age=${30 * 24 * 60 * 60}; SameSite=Lax`; 52 53 const bootstrap = document.getElementById('photohuman-viewed-bootstrap'); 54 55 if (bootstrap) { 56 bootstrap.textContent = encoded; 57 } 58 59 return nextViewed; 60}; 61 62const trackViewedProduct = (productId) => { 63 const id = Number(productId); 64 65 if (!id) { 66 return; 67 } 68 69 const viewed = readLocalViewed().filter((itemId) => itemId !== id); 70 71 writeViewed([id, ...viewed]); 72}; 73 74export const initViewedProducts = () => { 75 const serverViewed = readServerViewed(); 76 const viewed = serverViewed ?? readLocalViewed(); 77 78 if (serverViewed) { 79 writeViewed(serverViewed); 80 } 81 82 const productCard = document.getElementById('photohuman-product-card'); 83 const productId = Number(productCard?.dataset.productId || 0); 84 85 if (productId) { 86 trackViewedProduct(productId); 87 return; 88 } 89 90 if (viewed.length) { 91 writeViewed(viewed); 92 } 93};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.