// snippets/CoreWebVitals/INP.js | sha256:aedd064970241d69 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/CoreWebVitals/INP.js (()=>{if(!PerformanceObserver.supportedEntryTypes?.includes("event"))return{script:"INP",status:"unsupported",error:"event entries not supported in this browser"};let t=[],e=0,n=null;const a=t=>t<=200?"good":t<=500?"needs-improvement":"poor";function r(t){const e=t.getRootNode(),n=t=>{try{return 1===e.querySelectorAll(t).length}catch{return!1}},a=[];let r=t;for(;r&&1===r.nodeType;){if(r.id&&n(`#${window.CSS.escape(r.id)}`)){a.unshift(`#${window.CSS.escape(r.id)}`);break}const t=r.localName;let e=t+Array.from(r.classList).slice(0,2).map(t=>`.${window.CSS.escape(t)}`).join("");const s=r.parentNode?Array.from(r.parentNode.children):[r];if(s.filter(t=>t.matches(e)).length>1&&(e+=`:nth-of-type(${s.filter(e=>e.localName===t).indexOf(r)+1})`),a.unshift(e),n(a.join(" > ")))break;r=r.parentElement}return a.join(" > ")}const s=()=>{if(0===t.length)return{value:0,entry:null};const e=[...t].sort((t,e)=>e.duration-t.duration),n=performance.interactionCount||t.length,a=Math.min(e.length-1,Math.floor(n/50));return{value:e[a].duration,entry:e[a]}},i=t=>{const e=t.target;if(!e)return t.name;let n=e.tagName.toLowerCase();if(e.id)n+=`#${e.id}`;else if(e.className&&"string"==typeof e.className){const t=e.className.trim().split(/\s+/).slice(0,2).join(".");t&&(n+=`.${t}`)}return`${t.name} → ${n}`},o=function(){const t=new Map,e=t=>t.processingEnd-t.processingStart;return{add(n){const a=new Set;for(const e of n){if(!e.interactionId)continue;const n=t.get(e.interactionId)??{entries:[]};n.entries.push({name:e.name,target:e.target,startTime:e.startTime,duration:e.duration,processingStart:e.processingStart,processingEnd:e.processingEnd}),t.set(e.interactionId,n),a.add(e.interactionId)}return[...a].map(n=>{const a=t.get(n);return a.summary=((t,n)=>{const a=n.reduce((t,e)=>e.duration>t.duration?e:t),r=a.startTime+a.duration,s=n.filter(t=>Math.abs(t.startTime+t.duration-r)<=8),i=Math.min(...s.map(t=>t.startTime)),o=Math.min(...s.map(t=>t.processingStart)),c=Math.max(...s.map(t=>t.processingEnd)),l=s.reduce((t,n)=>e(n)>e(t)?n:t),u=s.find(t=>"click"===t.name||"keydown"===t.name),d=u&&e(l)-e(u)<1?u:l;return{interactionId:t,name:d.name,target:d.target??s.find(t=>t.target)?.target??null,startTime:i,duration:a.duration,phases:{inputDelay:o-i,processingTime:c-o,presentationDelay:Math.max(0,i+a.duration-c)}}})(n,a.entries),a.summary})},list:()=>[...t.values()].map(t=>t.summary)}}(),c=a=>{o.add(a),t=o.list().map(t=>({...t,formattedName:i(t)}));const r=s();e=r.value,n=r.entry},l=new PerformanceObserver(t=>c(t.getEntries()));l.observe({type:"event",buffered:!0,durationThreshold:16}),l.observe({type:"first-input",buffered:!0});const u=()=>{a(e);if(n){n.target&&(t=>{if(!t)return"";const e=[];let n=t;for(;n&&n!==document.body&&e.length<5;){let t=n.tagName.toLowerCase();if(n.id)t+=`#${n.id}`;else if(n.className&&"string"==typeof n.className){const e=n.className.trim().split(/\s+/).slice(0,2).join(".");e&&(t+=`.${e}`)}e.unshift(t),n=n.parentElement}e.join(" > ")})(n.target);const t=n.phases;if(t.inputDelay>0){const e=t.inputDelay+t.processingTime+t.presentationDelay,n=40;"▓".repeat(Math.round(t.inputDelay/e*n)),"█".repeat(Math.round(t.processingTime/e*n)),"░".repeat(Math.round(t.presentationDelay/e*n))}}const r=t.filter(t=>t.duration>200).sort((t,e)=>e.duration-t.duration).slice(0,10);r.length>0&&r.slice(0,3).forEach((t,e)=>{});const s={};t.forEach(t=>{const e=t.name;s[e]||(s[e]={count:0,totalDuration:0,maxDuration:0}),s[e].count++,s[e].totalDuration+=t.duration,s[e].maxDuration=Math.max(s[e].maxDuration,t.duration)}),Object.keys(s)};return window.getINP=()=>{const r=s();e=r.value,n=r.entry,u();const i=a(e),o={totalInteractions:t.length};if(n&&(o.worstEvent=n.formattedName,o.phases={inputDelay:Math.round(n.phases.inputDelay),processingTime:Math.round(n.phases.processingTime),presentationDelay:Math.round(n.phases.presentationDelay)}),0===t.length)return{script:"INP",status:"tracking",message:"No interactions yet: click, tap or type on the page, then call getINP() again.",getDataFn:"getINP",details:o};const c=Math.round(e);return{script:"INP",status:"ok",metric:"INP",value:c,unit:"ms",rating:i,thresholds:{good:200,needsImprovement:500},details:o,...e<16&&{issues:[{code:"no-paint-measured",severity:"info",message:`Worst interaction: ${c} ms, under the 16 ms of a frame, so the page most likely painted nothing after it. If it should respond, check that the click or key press reaches an element that does.`}]}}},window.getINPDetails=()=>{if(0===t.length)return{script:"INP",status:"tracking",message:"No interactions yet: click, tap or type on the page, then call getINPDetails() again.",getDataFn:"getINPDetails",count:0,items:[]};const e=[...t].sort((t,e)=>e.duration-t.duration),n=Math.min(e.length,15);return e.slice(0,n).forEach((t,e)=>{t.target&&(t=>{if(!t)return"";const e=[];let n=t;for(;n&&n!==document.body&&e.length<5;){let t=n.tagName.toLowerCase();if(n.id)t+=`#${n.id}`;else if(n.className&&"string"==typeof n.className){const e=n.className.trim().split(/\s+/).slice(0,2).join(".");e&&(t+=`.${e}`)}e.unshift(t),n=n.parentElement}e.join(" > ")})(t.target)}),{script:"INP",status:"ok",count:e.length,items:e.slice(0,n).map(t=>({formattedName:t.formattedName,eventType:t.name,target:t.target?r(t.target):null,duration:Math.round(t.duration),startTime:Math.round(t.startTime),phases:{inputDelay:Math.round(t.phases.inputDelay),processingTime:Math.round(t.phases.processingTime),presentationDelay:Math.round(t.phases.presentationDelay)}}))}},document.addEventListener("visibilitychange",()=>{"hidden"===document.visibilityState&&(c(l.takeRecords()),u())}),{script:"INP",status:"tracking",message:"INP tracking active. Interact with the page then call getINP() for results.",getDataFn:"getINP"}})();