// snippets/Loading/Inline-Script-Info-and-Size.js | sha256:b1e137e379ae0e3f | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Inline-Script-Info-and-Size.js (()=>{function e(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const t=["B","KB","MB","GB"],s=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),t.length-1));return(e/Math.pow(1024,s)).toFixed(1)+" "+t[s]}const t=["__NEXT_DATA__","__NUXT_DATA__","__remixContext"],s=/^\s*\(?\s*(?:self|window|globalThis)\.(__\w+)\s*(?:=\s*(?:self|window|globalThis)\.\1\s*\|\|\s*\[\s*\]\s*\)\s*)?\.push\s*\(/,i=/^\s*(?:self|window|globalThis)\.(?:__NUXT__|__remixContext|___\w+|__\w*(?:STATE|DATA)\w*)\s*=/,a=/\b(?:gtag|fbq)\s*\(|\b(?:_gaq|dataLayer)\.push\s*\(|['"]gtm\.(?:start|js)['"]|\banalytics\.(?:load|page|track|identify)\s*\(/,n=Array.from(document.querySelectorAll("script:not([src])")).filter(e=>e.innerHTML.trim().length>0);if(0===n.length)return{script:"Inline-Script-Info-and-Size",status:"ok",count:0,items:[],issues:[]};const o=n.map((e,n)=>{const o=e.innerHTML,r=new Blob([o]).size,l=e.type||"text/javascript",c=null!==e.closest("head");let g="classic";"module"===l?g="module":"application/ld+json"===l?g="json-ld":l.includes("template")||"text/html"===l?g="template":"text/javascript"!==l&&""!==l&&(g="other");const d="classic"===g&&!e.hasAttribute("async")&&!e.hasAttribute("defer"),p=o.replace(/\s+/g," ").trim().slice(0,80),h=((e,a,n)=>a.includes("json")||t.includes(e.id)||s.test(n)||i.test(n))(e,l,o),u=!h&&/^(var|let|const|window\.)/.test(o.trim()),y=!h&&/config|settings|options|__/i.test(o.slice(0,200)),m=!h&&a.test(o);return{index:n+1,element:e,size:r,sizeKB:(f=r,(f/1024).toFixed(2)),type:l,category:g,inHead:c,isParserBlocking:d,hasGlobalVars:u,isConfig:y,isAnalytics:m,isDataPayload:h,preview:p+(o.length>80?"...":""),content:o};var f}),r=o.filter(e=>"classic"===e.category||"module"===e.category),l=o.filter(e=>"json-ld"===e.category),c=o.filter(e=>"template"===e.category||"other"===e.category),g=r.reduce((e,t)=>e+t.size,0),d=r.filter(e=>e.isParserBlocking),p=d.filter(e=>e.inHead),h=new Map;r.forEach(e=>{const t=e.content.replace(/\s+/g," ").trim();t.length>50&&(h.has(t)||h.set(t,[]),h.get(t).push(e.index))});const u=Array.from(h.entries()).filter(([,e])=>e.length>1).map(([,e])=>e);let y,m;g<=3072&&0===p.length?(y="Good",m="#22c55e"):g<=6144||p.length<=2?(y="Needs Review",m="#f59e0b"):(y="Needs Optimization",m="#ef4444"),r.length>0&&(r.sort((e,t)=>t.size-e.size).map(t=>({"#":t.index,Location:t.inHead?"head":"body",Type:t.category,Size:e(t.size),"Parser-Blocking":t.isParserBlocking?"⚠️ Yes":"No",Pattern:t.isDataPayload?"Data":t.isAnalytics?"Analytics":t.isConfig?"Config":t.hasGlobalVars?"Globals":"-",Preview:t.preview})),r.sort((e,t)=>t.size-e.size).forEach(e=>{const t=[];e.isParserBlocking&&e.inHead&&t.push("blocking"),e.size>2048&&t.push("large"),t.length>0&&t.join(", ")})),l.length>0&&l.map(t=>{let s="-";try{const e=JSON.parse(t.content);s=e["@type"]||(Array.isArray(e)?"Array":"-")}catch{}return{"#":t.index,"Schema Type":s,Size:e(t.size)}});const f=[];p.length>0&&f.push({code:"parser-blocking-in-head",type:"blocking",message:`${p.length} parser-blocking script(s) in `,details:p.map(t=>`Script #${t.index}: ${e(t.size)}`),suggestion:"Move to end of or add 'defer' attribute"});const b=r.filter(e=>e.size>2048);b.length>0&&f.push({code:"large-inline",type:"large",message:`${b.length} script(s) over 2 KB`,details:b.map(t=>`Script #${t.index}: ${e(t.size)}`),suggestion:"Consider moving large scripts to external cacheable files"}),u.length>0&&f.push({code:"duplicate-scripts",type:"duplicate",message:`${u.length} duplicated script(s) detected`,details:u.map(e=>`Scripts #${e.join(", #")} have identical content`),suggestion:"Remove duplicate scripts or consolidate into one"});const _=r.filter(e=>e.isAnalytics&&e.inHead&&e.isParserBlocking);_.length>0&&f.push({code:"analytics-in-head",type:"analytics",message:`${_.length} analytics script(s) blocking in `,details:_.map(e=>`Script #${e.index}`),suggestion:"Load analytics scripts with 'async' or move to "}),f.length>0&&f.forEach(e=>{e.details&&e.details.forEach(e=>{})});const z=[...r].sort((e,t)=>Number(t.isParserBlocking)-Number(e.isParserBlocking)||t.size-e.size);return{script:"Inline-Script-Info-and-Size",status:"ok",count:o.length,details:{executableCount:r.length,jsonLdCount:l.length,otherCount:c.length,totalSizeBytes:g,parserBlockingCount:d.length,parserBlockingInHeadCount:p.length,dataPayloadCount:o.filter(e=>e.isDataPayload).length},items:z.slice(0,50).map(e=>({index:e.index,sizeBytes:e.size,category:e.category,inHead:e.inHead,isParserBlocking:e.isParserBlocking,isAnalytics:e.isAnalytics,isConfig:e.isConfig,isDataPayload:e.isDataPayload})),issues:f.map(e=>({code:e.code,severity:"blocking"===e.type?"error":"warning",message:e.message}))}})();