// snippets/Loading/Find-Above-The-Fold-Lazy-Loaded-Images.js | sha256:5b640f598b3405e3 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Find-Above-The-Fold-Lazy-Loaded-Images.js (function(){function e(e){const t=e.getRootNode(),i=e=>{try{return 1===t.querySelectorAll(e).length}catch{return!1}},a=[];let n=e;for(;n&&1===n.nodeType;){if(n.id&&i(`#${window.CSS.escape(n.id)}`)){a.unshift(`#${window.CSS.escape(n.id)}`);break}const e=n.localName;let t=e+Array.from(n.classList).slice(0,2).map(e=>`.${window.CSS.escape(e)}`).join("");const r=n.parentNode?Array.from(n.parentNode.children):[n];if(r.filter(e=>e.matches(t)).length>1&&(t+=`:nth-of-type(${r.filter(t=>t.localName===e).indexOf(n)+1})`),a.unshift(t),i(a.join(" > ")))break;n=n.parentElement}return a.join(" > ")}function t(e){const t=e.getBoundingClientRect();return t.top0&&t.left0&&t.width>0&&t.height>0}function i(e){const t=e.getBoundingClientRect(),i=[t.top,window.innerHeight-t.bottom,t.left,window.innerWidth-t.right];return Math.min(...i.map(e=>Math.abs(e)))}function a(e){if(!e)return null;try{const t=performance.getEntriesByType("resource").filter(e=>!function(e){return e.name.endsWith("#webperf-snippets")}(e)),i=t.find(t=>t.name===e||t.name.includes(e.split("/").pop()));if(i&&i.transferSize)return(i.transferSize/1024).toFixed(1)+" KB"}catch{}return null}function n(e){const t="lazy"===e.getAttribute("loading"),i=e.hasAttribute("data-src")||e.hasAttribute("data-lazy");return t&&i?"both":t?"loading-lazy":i?"data-src":null}const r={lazyImages:[],lcpCandidate:null,nodeArray:[],summary:{total:0,withLoadingLazy:0,withDataSrc:0,lcpAffected:!1}},o='[loading="lazy"], [data-src], [data-lazy]',s=document.querySelectorAll(`img${o}, picture source${o}`),l=Array.from(document.querySelectorAll("img")).filter(e=>t(e)&&e.getBoundingClientRect().width>0);let c=null,d=0;return l.forEach(e=>{const t=e.getBoundingClientRect(),i=t.width*t.height;i>d&&(d=i,c=e)}),s.forEach(o=>{const s="SOURCE"===o.tagName?o.closest("picture")?.querySelector("img"):o;if(!s||!t(s))return;const l=function(t){const r=t.getBoundingClientRect(),o=r.width*r.height;return{selector:e(t),lazyType:n(t),dimensions:`${Math.round(r.width)}×${Math.round(r.height)}`,position:{top:Math.round(r.top+window.scrollY),left:Math.round(r.left+window.scrollX)},distanceFromEdge:Math.round(i(t))+"px",src:t.currentSrc||t.src||t.getAttribute("data-src")||"",srcset:t.srcset||null,sizes:t.sizes||null,alt:t.alt||"(no alt)",fetchPriority:t.fetchPriority||"auto",fileSize:a(t.currentSrc||t.src),area:o,element:t}}(s);l.isLcpCandidate=s===c,r.lazyImages.push(l),r.nodeArray.push(s),r.summary.total++,"loading-lazy"!==l.lazyType&&"both"!==l.lazyType||r.summary.withLoadingLazy++,"data-src"!==l.lazyType&&"both"!==l.lazyType||r.summary.withDataSrc++,l.isLcpCandidate&&(r.summary.lcpAffected=!0,r.lcpCandidate=l)}),0===r.lazyImages.length||(r.lazyImages.map(e=>({selector:e.selector,lazyType:e.lazyType,dimensions:e.dimensions,top:e.position.top+"px",distanceFromEdge:e.distanceFromEdge,src:e.src,srcset:e.srcset,sizes:e.sizes,alt:e.alt,fetchPriority:e.fetchPriority,fileSize:e.fileSize,isLcpCandidate:e.isLcpCandidate?"⚠️ YES":"no"})),r.nodeArray.forEach((e,t)=>{})),{script:"Find-Above-The-Fold-Lazy-Loaded-Images",status:"ok",count:r.lazyImages.length,details:{total:r.summary.total,withLoadingLazy:r.summary.withLoadingLazy,withDataSrc:r.summary.withDataSrc,lcpAffected:r.summary.lcpAffected},items:r.lazyImages.map(e=>({selector:e.selector,lazyType:e.lazyType,dimensions:e.dimensions,position:`${e.position.top}px, ${e.position.left}px`,distanceFromEdge:e.distanceFromEdge,src:e.src,srcset:e.srcset,sizes:e.sizes,alt:e.alt,fetchPriority:e.fetchPriority,fileSize:e.fileSize,isLcpCandidate:e.isLcpCandidate})),issues:[...r.summary.lcpAffected?[{code:"lcp-lazy",severity:"error",message:'LCP candidate image has lazy loading: remove loading="lazy" and add fetchpriority="high"'}]:[],...r.lazyImages.filter(e=>!e.isLcpCandidate).length>0?[{code:"lazy-above-fold",severity:"warning",message:`${r.lazyImages.filter(e=>!e.isLcpCandidate).length} above-fold image(s) have lazy loading: remove loading="lazy"`}]:[]]}})();