-
Notifications
You must be signed in to change notification settings - Fork 2
/
ecoindex.min.js
1 lines (1 loc) · 2.55 KB
/
ecoindex.min.js
1
function computeEcoIndex(e,n,r){const t=computeQuantile(quantiles_dom,e),o=computeQuantile(quantiles_req,n),i=computeQuantile(quantiles_size,r);return 100-5*(3*t+2*o+i)/6}function computeQuantile(e,n){for(i=1;i<e.length;i++)if(n<e[i])return i+(n-e[i-1])/(e[i]-e[i-1]);return e.length}function getEcoIndexGrade(e){return e>75?"A":e>65?"B":e>50?"C":e>35?"D":e>20?"E":e>5?"F":"G"}function computeGreenhouseGasesEmissionfromEcoIndex(e){return(2+2*(50-e)/100).toFixed(2)}function computeWaterConsumptionfromEcoIndex(e){return(3+3*(50-e)/100).toFixed(2)}quantiles_dom=[0,47,75,159,233,298,358,417,476,537,603,674,753,843,949,1076,1237,1459,1801,2479,594601],quantiles_req=[0,2,15,25,34,42,49,56,63,70,78,86,95,105,117,130,147,170,205,281,3920],quantiles_size=[0,1.37,144.7,319.53,479.46,631.97,783.38,937.91,1098.62,1265.47,1448.32,1648.27,1876.08,2142.06,2465.37,2866.31,3401.59,4155.73,5400.08,8037.54,223212.26],ecoindex=function(e,n){var r=req=error=0;n.forEach(function(e){req++;var n=0;0==e.transferSize?error++:n=e.transferSize,n=Math.round(n/1e3),r+=n});var t=100*error/req,o=computeEcoIndex(e,req,r),i=getEcoIndexGrade(o),a=computeGreenhouseGasesEmissionfromEcoIndex(o),c=computeWaterConsumptionfromEcoIndex(o),u="ecoIndex: "+o.toFixed(2)+(t>0?" (*"+Math.round(t)+"% d'erreur)":"")+" | GES: "+a+" gCO2e | eau: "+c+" cl | Nombre de requêtes: "+req+" | Taille de la page: "+r+" Ko | Taille du DOM: "+e,d=document.createElement("style");document.head.appendChild(d),d.sheet.insertRule("#ecoindex { background: #ffffffcc; padding: 5px 8px; border-radius: 10px; position: fixed; z-index: 10; left: 10px; bottom: 10px;}"),d.sheet.insertRule("#ecoindex span { display: inline-block; width: 18px; height: 18px; font-size: 12px; line-height: 18px; margin: 0.1rem 0 0 0.5rem; text-align: center; border-radius: 50%; }");var s="#ED2124",f="#fff";switch(i){case"A":s="#349A47";break;case"B":s="#51B84B";break;case"C":s="#CADB2A",f="#000";break;case"D":s="#F6EB15",f="#000";break;case"E":s="#FECD06",f="#000";break;case"F":s="#F99839";break;default:s="#ED2124"}d.sheet.insertRule("#ecoindex span { background-color: "+s+"; color: "+f+" }"),document.body.insertAdjacentHTML("beforeend",'<a href="http://www.ecoindex.fr/quest-ce-que-ecoindex/" id="ecoindex" target="_blank" rel="noreferrer" title="'+u+'">ecoIndex<span>'+i+(t>0?"*":"")+"</span></a>")},window.onload=function(e){setTimeout(function(){var e=document.getElementsByTagName("*").length,n=window.performance.getEntriesByType("resource");n.push({name:"Page HTML",transferSize:window.performance.getEntriesByType("navigation")[0].transferSize}),ecoindex(e,n)},1e3)};