// snippets/Loading/Fonts-Preloaded-Loaded-and-used-above-the-fold.js | sha256:6308d064c5b2afb9 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Fonts-Preloaded-Loaded-and-used-above-the-fold.js (()=>{const e=[];function t(e){try{const t=new URL(e).pathname;return t.split("/").pop()||t}catch{return e}}function o(e){const t=e.replace(/\.$/,"");if(/^\d{1,3}(\.\d{1,3}){3}$/.test(t)||t.includes(":"))return t;const o=t.split(".");return o.length<=2?t:2===o[o.length-1].length&&["ac","co","com","edu","go","gob","gouv","gov","govt","mil","ne","net","nom","or","org","sch"].includes(o[o.length-2])?o.slice(-3).join("."):o.slice(-2).join(".")}function n(t){try{return!function(t){const n=o(t);return n===o(location.hostname)||e.some(e=>o(String(e).trim().toLowerCase().replace(/^[a-z]+:\/\//,"").split("/")[0])===n)}(new URL(t).hostname)}catch{return!1}}function r(e){return e.split(",")[0].trim().replace(/["']/g,"").toLowerCase()}const i=Array.from(document.querySelectorAll('link[rel="preload"][as="font"]')).map(e=>({href:e.href,name:t(e.href),crossorigin:e.crossOrigin,type:e.type||"unknown",thirdParty:n(e.href)})),l=Array.from(document.fonts.values()).filter(e=>"loaded"===e.status).map(e=>({family:e.family.replace(/["']/g,""),weight:e.weight,style:e.style,display:e.display||"unknown",key:`${e.family.replace(/["']/g,"")}-${e.weight}-${e.style}`})),s=Array.from(new Map(l.map(e=>[e.key,e])).values()),a=window.innerHeight,m=window.innerWidth,d=Array.from(document.querySelectorAll("body *:not(script):not(style):not(link):not(source)")).filter(e=>{const t=e.getBoundingClientRect();return t.top0&&t.left0&&t.width>0&&t.height>0}),c=new Map;d.forEach(e=>{const t=getComputedStyle(e),o=t.fontFamily,n=t.fontWeight,r=t.fontStyle,i=`${o}-${n}-${r}`;c.has(i)?c.get(i).elements++:c.set(i,{family:o.split(",")[0].trim().replace(/["']/g,""),fullFamily:o,weight:n,style:r,elements:1})});const g=Array.from(c.values()),y=i.map(e=>r(e.name.replace(/\.(woff2?|ttf|otf|eot)$/i,""))),f=g.map(e=>r(e.family)),u=i.filter(e=>{const t=r(e.name.replace(/\.(woff2?|ttf|otf|eot)$/i,""));return!f.some(e=>e.includes(t)||t.includes(e))}),h=g.filter(e=>{const t=r(e.family);return!["arial","helvetica","times","georgia","verdana","system-ui","-apple-system","segoe ui","roboto","sans-serif","serif","monospace"].some(e=>t.includes(e))&&!y.some(e=>e.includes(t)||t.includes(e))});return 0===i.length||(i.map(e=>({Font:e.name,Type:e.type,"Third-Party":e.thirdParty?"Yes":"No",Crossorigin:e.crossorigin||"missing ⚠️"})),i.filter(e=>!e.crossorigin)),0===s.length||(s.map(e=>({Family:e.family,Weight:e.weight,Style:e.style,Display:e.display})),s.filter(e=>"auto"===e.display||"unknown"===e.display)),0===g.length||g.sort((e,t)=>t.elements-e.elements).map(e=>({Family:e.family,Weight:e.weight,Style:e.style,"Elements Using":e.elements})),(u.length>0||h.length>0)&&(u.length>0&&u.forEach(e=>{}),h.length>0&&h.forEach(e=>{})),i.filter(e=>e.thirdParty),{script:"Fonts-Preloaded-Loaded-and-used-above-the-fold",status:"ok",count:s.length,details:{preloadedCount:i.length,loadedCount:s.length,usedAboveFoldCount:g.length,preloadedNotUsedCount:u.length,usedNotPreloadedCount:h.length,preloadedFonts:i.slice(0,20).map(e=>({family:e.name.replace(/\.(woff2?|ttf|otf|eot)$/i,""),href:e.href,fontType:e.type,crossorigin:e.crossorigin||"",thirdParty:e.thirdParty}))},items:s.slice(0,50).map(e=>({family:e.family,weight:e.weight,style:e.style,display:e.display})),usedFonts:g.slice(0,50).map(e=>({family:e.family,weight:e.weight,style:e.style,elements:e.elements})),issues:[...u.map(e=>({code:"preloaded-unused",severity:"warning",message:`Preloaded but not used above fold: ${e.name}`,target:e.name})),...h.map(e=>({code:"used-not-preloaded",severity:"warning",message:`Used above fold but not preloaded: ${e.family} (${e.weight})`,target:`${e.family} (${e.weight})`})),...i.filter(e=>!e.crossorigin).map(e=>({code:"preload-no-crossorigin",severity:"error",message:`Font preloaded without crossorigin (double fetch): ${e.name}`,target:e.name}))]}})();