// snippets/CoreWebVitals/LCP.js | sha256:58eb5a9f00258b43 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/CoreWebVitals/LCP.js (async()=>{if(!PerformanceObserver.supportedEntryTypes?.includes("largest-contentful-paint"))return{script:"LCP",status:"unsupported",error:"largest-contentful-paint entries not supported in this browser"};const e=e=>e<=2500?"good":e<=4e3?"needs-improvement":"poor",t=()=>{const e=performance.getEntriesByType("navigation")[0];return e?.activationStart||0};let n=-1;new PerformanceObserver(r=>{const o=r.getEntries(),s=o[o.length-1];s&&(r=>{const o=t(),s=Math.max(0,r.startTime-o);if(s===n)return;n=s,e(s);const i=r.element;if(i){const e=i.tagName.toLowerCase();"img"===e||"video"===e||window.getComputedStyle(i),i.style.outline="3px dashed lime",i.style.outlineOffset="2px"}})(s)}).observe({type:"largest-contentful-paint",buffered:!0});const r=await new Promise(e=>{const t=[],n=new PerformanceObserver(e=>t.push(...e.getEntries()));n.observe({type:"largest-contentful-paint",buffered:!0}),setTimeout(()=>{n.disconnect(),e(t.at(-1)??null)},100)});if(!r)return{script:"LCP",status:"error",error:"No LCP entries buffered"};const o=t(),s=Math.round(Math.max(0,r.startTime-o)),i=e(s),a=r.element;let l=null,c=null;if(a){l=function(e){const t=e.getRootNode(),n=e=>{try{return 1===t.querySelectorAll(e).length}catch{return!1}},r=[];let o=e;for(;o&&1===o.nodeType;){if(o.id&&n(`#${window.CSS.escape(o.id)}`)){r.unshift(`#${window.CSS.escape(o.id)}`);break}const e=o.localName;let t=e+Array.from(o.classList).slice(0,2).map(e=>`.${window.CSS.escape(e)}`).join("");const s=o.parentNode?Array.from(o.parentNode.children):[o];if(s.filter(e=>e.matches(t)).length>1&&(t+=`:nth-of-type(${s.filter(t=>t.localName===e).indexOf(o)+1})`),r.unshift(t),n(r.join(" > ")))break;o=o.parentElement}return r.join(" > ")}(a);const e=a.tagName.toLowerCase();c="img"===e?"Image":"video"===e?"Video poster":"none"!==window.getComputedStyle(a).backgroundImage?"Background image":"h1"===e||"p"===e?"Text block":e}return{script:"LCP",status:"ok",metric:"LCP",value:s,unit:"ms",rating:i,thresholds:{good:2500,needsImprovement:4e3},details:{element:l,elementType:c,url:r.url||null,sizePixels:r.size||null}}})();