// snippets/CoreWebVitals/LCP-Trail.js | sha256:5c73673f4c889f18 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/CoreWebVitals/LCP-Trail.js (async()=>{if(!PerformanceObserver.supportedEntryTypes?.includes("largest-contentful-paint"))return{script:"LCP-Trail",status:"unsupported",error:"largest-contentful-paint entries not supported in this browser"};const e=[{color:"#EF4444",name:"Red"},{color:"#F97316",name:"Orange"},{color:"#22C55E",name:"Green"},{color:"#3B82F6",name:"Blue"},{color:"#A855F7",name:"Purple"},{color:"#EC4899",name:"Pink"}],t=e=>e<=2500?"good":e<=4e3?"needs-improvement":"poor",n=()=>{const e=performance.getEntriesByType("navigation")[0];return e?.activationStart||0};function r(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(" > ")}const o=(e,t)=>{if(!e)return t.url?{type:"Image",url:t.url}:{type:"Removed element"};const n=e.tagName.toLowerCase();return"img"===n?{type:"Image",url:t.url||e.src}:"video"===n?{type:"Video poster",url:t.url||e.poster}:"none"!==window.getComputedStyle(e).backgroundImage?{type:"Background image",url:t.url}:{type:"h1"===n||"p"===n?"Text block":n}},s=e=>e.element??`${e.startTime}|${e.size}`,a=e=>{if(e.startsWith("data:"))return`${e.split(",")[0]},…`;try{const t=new URL(e),n=t.pathname.split("/").pop()?.split("?")[0];return t.hostname!==location.hostname?`${t.hostname}/…/${n}`:n||e}catch{return e}},i=[];new PerformanceObserver(a=>{const l=n(),c=new Set(i.map(e=>s(e.entry)));for(const t of a.getEntries()){const n=s(t);if(c.has(n))continue;c.add(n);const{element:o}=t,{color:a,name:m}=e[i.length%e.length];o&&(o.style.outline=`3px dashed ${a}`,o.style.outlineOffset="2px"),i.push({index:i.length+1,element:o,selector:o?r(o):"(removed from DOM)",color:o?a:"#9CA3AF",name:m,time:Math.max(0,t.startTime-l),entry:t})}(()=>{const e=i[i.length-1];e&&(t(e.time),o(e.element,e.entry),i.forEach(({})=>{}))})()}).observe({type:"largest-contentful-paint",buffered:!0});const l=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)},100)});if(0===l.length)return{script:"LCP-Trail",status:"error",error:"No LCP entries yet"};const c=n(),m=new Map;for(const e of l){const t=s(e);if(m.has(t))continue;const n=e.element,{type:i,url:l}=o(n,e);m.set(t,{index:m.size+1,selector:n?r(n):null,...n?{}:{removed:!0},...!n&&e.id?{id:e.id}:{},time:Math.round(Math.max(0,e.startTime-c)),elementType:i,sizePixels:e.size,...l?{url:a(l)}:{}})}const u=[...m.values()],p=l.at(-1),d=m.get(s(p)),f=Math.round(Math.max(0,p.startTime-c));return{script:"LCP-Trail",status:"ok",metric:"LCP",value:f,unit:"ms",rating:t(f),thresholds:{good:2500,needsImprovement:4e3},details:{candidateCount:u.length,finalElement:d.selector,candidates:u}}})();