// snippets/Loading/Find-non-Lazy-Loaded-Images-outside-of-the-viewport.js | sha256:84cf7a5adcd5c632 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Find-non-Lazy-Loaded-Images-outside-of-the-viewport.js (function(){function e(e){const t=e.getRootNode(),n=e=>{try{return 1===t.querySelectorAll(e).length}catch{return!1}},o=[];let i=e;for(;i&&1===i.nodeType;){if(i.id&&n(`#${window.CSS.escape(i.id)}`)){o.unshift(`#${window.CSS.escape(i.id)}`);break}const e=i.localName;let t=e+Array.from(i.classList).slice(0,2).map(e=>`.${window.CSS.escape(e)}`).join("");const r=i.parentNode?Array.from(i.parentNode.children):[i];if(r.filter(e=>e.matches(t)).length>1&&(t+=`:nth-of-type(${r.filter(t=>t.localName===e).indexOf(i)+1})`),o.unshift(t),n(o.join(" > ")))break;i=i.parentElement}return o.join(" > ")}function t(e){const t=e.getBoundingClientRect();return t.top0&&t.left0&&t.width>0&&t.height>0}function n(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const t=["B","KB","MB","GB"],n=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),t.length-1));return(e/Math.pow(1024,n)).toFixed(1)+" "+t[n]}const o=Array.from(document.querySelectorAll("img")).filter(e=>t(e)&&e.getBoundingClientRect().width>0);let i=null,r=0;o.forEach(e=>{const t=e.getBoundingClientRect(),n=t.width*t.height;n>r&&(r=n,i=e)});const d=document.querySelectorAll('img:not([data-src]):not([loading="lazy"])'),s={belowFold:[],hiddenContainers:[],excluded:{inViewport:0,lcpCandidate:null,tooSmall:0},elements:[]};d.forEach(o=>{const r=o.getBoundingClientRect();if(t(o))return void(o===i?s.excluded.lcpCandidate=e(o):s.excluded.inViewport++);if(r.width<50||r.height<50)return void s.excluded.tooSmall++;const d=o.currentSrc||o.src,a=function(e){const t=e.currentSrc||e.src;if(!t||t.startsWith("data:"))return 0;const n=performance.getEntriesByType("resource").filter(e=>!e.name.endsWith("#webperf-snippets")).find(e=>e.name===t);return n&&(n.transferSize||n.encodedBodySize)||0}(o),l=function(t){let n=t.parentElement;const o=["[hidden]",'[aria-hidden="true"]',".modal:not(.show)",".tab-pane:not(.active)",'[role="tabpanel"]:not(.active)',".accordion-collapse:not(.show)",".carousel-item:not(.active)",".swiper-slide:not(.swiper-slide-active)"];for(;n&&n!==document.body;){const t=window.getComputedStyle(n);if("none"===t.display||"hidden"===t.visibility)return{hidden:!0,reason:"CSS hidden",container:e(n)};for(const t of o)try{if(n.matches(t))return{hidden:!0,reason:t,container:e(n)}}catch{}n=n.parentElement}return{hidden:!1}}(o),c={selector:e(o),src:d.length>60?"..."+d.slice(-57):d,fullSrc:d,dimensions:`${o.naturalWidth}×${o.naturalHeight}`,size:a,sizeFormatted:a>0?n(a):"unknown",element:o};l.hidden?(c.hiddenReason=l.reason,c.container=l.container,s.hiddenContainers.push(c)):(c.distanceFromViewport=Math.round(r.top-window.innerHeight)+"px",s.belowFold.push(c)),s.elements.push(o)}),s.belowFold.sort((e,t)=>parseInt(t.distanceFromViewport)-parseInt(e.distanceFromViewport));const a=s.belowFold.length+s.hiddenContainers.length,l=[...s.belowFold,...s.hiddenContainers].reduce((e,t)=>e+t.size,0);return 0===a||(s.belowFold.length>0&&s.belowFold.slice(0,20).map(e=>({selector:e.selector,src:e.src,dimensions:e.dimensions,size:e.size,sizeFormatted:e.sizeFormatted,distanceFromViewport:e.distanceFromViewport})),s.hiddenContainers.length>0&&s.hiddenContainers.slice(0,15).map(e=>({selector:e.selector,src:e.src,dimensions:e.dimensions,size:e.size,sizeFormatted:e.sizeFormatted,hiddenReason:e.hiddenReason,container:e.container})),s.elements.slice(0,15).forEach((e,t)=>{})),{script:"Find-non-Lazy-Loaded-Images-outside-of-the-viewport",status:"ok",count:a,details:{belowFoldCount:s.belowFold.length,hiddenContainerCount:s.hiddenContainers.length,totalSizeBytes:l,excluded:{inViewport:s.excluded.inViewport,lcpCandidate:s.excluded.lcpCandidate,tooSmall:s.excluded.tooSmall}},items:[...s.belowFold.map(e=>({selector:e.selector,src:e.fullSrc,sizeBytes:e.size,category:"below-fold",distancePx:parseInt(e.distanceFromViewport)})),...s.hiddenContainers.map(e=>({selector:e.selector,src:e.fullSrc,sizeBytes:e.size,category:"hidden-container",hiddenReason:e.hiddenReason,container:e.container}))],issues:a>0?[{code:"eager-off-viewport",severity:"warning",message:`${a} image(s) outside the viewport are missing loading="lazy"`}]:[]}})();