-
Notifications
You must be signed in to change notification settings - Fork 91
/
foresight.min.js
executable file
·1 lines (1 loc) · 8.4 KB
/
foresight.min.js
1
(function(a,b,c,d){"use strict";a.images=[];a.options=a.options||{};var e=a.options,f=e.testConn||true,g=e.minKbpsForHighBandwidth||300,h=e.speedTestUri||"http://foresightjs.appspot.com/speed-test/50K",i=e.speedTestKB||50,j=e.speedTestExpireMinutes||30,k=e.hiResClassname||"fs-high-resolution",l=e.lowResClassname||"fs-standard-resolution",m="devicePixelRatio",n="devicePixelRatioRounded",o="bandwidth",p="connType",q="connTestResult",r="connKbps",s="requestChange",t="defaultSrc",u="highResolutionSrc",v="browserWidth",w="browserHeight",x="requestWidth",y="requestHeight",z="width",A="height",B="widthUnits",C="heightUnits",D="aspectRatio",E="appliedImageSetItem",F="scale",G="scaleRounded",H="uriTemplate",I="uriFind",J="uriReplace",K="srcModification",L="loading",M="complete",N="fsjs",O="auto",P="percent",Q="auto",R="pixel",S="display",T="auto",U=true,V=false,W=/url\((?:([a-zA-Z-_0-9{}\?=&\\/.:\s]+)|([a-zA-Z-_0-9{}\?=&\\/.:\s]+)\|([a-zA-Z-_0-9{}\?=&\\/.:\s]+))\)/g,X=/[^\d]+/,Y,Z,$=function(){if(Y)return;Y=L;ab();Y=M;eb()},_=function(a,b){if(c.createEvent){var d=c.createEvent("Event");d.initEvent("foresight-"+a,U,U);b.dispatchEvent(d)}},ab=function(){var b,d,e;for(b=0;b<c.images.length;b++){d=c.images[b];if(d.ignore)continue;if(!d.initalized){d.initalized=U;_("imageInitStart",d);d[t]=db(d,"src");d[B]=db(d,z,U);d[C]=db(d,A,U);var f=db(d,"aspect-ratio",V);d[D]=f===O?f:!isNaN(f)&&f!==null?parseFloat(f):0;if(!d[t]||(!d[B]||!d[C])&&!d[D]){d.ignore=U;continue}d[u]=db(d,"high-resolution-src");d.orgClassName=d.className?d.className:"";d.onerror=rb;_("imageInitEnd",d);a.images.push(d)}d.imageSetText=lb(d,"font-family","fontFamily");d.imageSet=[];if(d.imageSetText.length>1){bb(d,d.imageSetText.split("image-set(")[1])}}},bb=function(a,b){var c,d=b!==undefined&&b!==null?b.split(","):[],e,f;for(c=0;c<d.length;c++){e={text:d[c],weight:0};if(e.text.indexOf(" 1.5x")>-1){e.weight++;e[F]=1.5}else if(e.text.indexOf(" 2x")>-1){e[F]=2}else if(e.text.indexOf(" 1x")>-1){e[F]=1}if(e.text.indexOf(" high-bandwidth")>-1){e[o]="high"}else if(e.text.indexOf(" low-bandwidth")>-1){e[o]="low"}while(f=W.exec(e.text)){if(f[1]!=null&&f[1]!==""){e[H]=f[1];e.weight++}else if(f[2]!=null&&f[2]!==""){e[I]=f[2];e[J]=f[3];e.weight++}}if(e[F]&&e[o]){e.weight+=2}else if(e[F]||e[o]){e.weight++}a.imageSet.push(e)}a.imageSet.sort(cb)},cb=function(a,b){if(a.weight<b.weight)return 1;if(a.weight>b.weight)return-1;return 0},db=function(a,b,c,d){d=a.getAttribute("data-"+b);if(c&&d!==null){if(!isNaN(d)){return parseInt(d,10)}return 0}return d},eb=function(){if(!(Z===M&&Y===M))return;var b,c=a.images.length,d,e,f,g,h=[],i;for(b=0;b<c;b++){d=a.images[b];if(!ib(d)){continue}_("imageRebuildStart",d);f=d.orgClassName.split(" ");jb(d);if(d.unitType==R){g="fs-"+d[v]+"x"+d[w];f.push(g);if(h[g]==undefined){h[g]=U;h.push("."+g+"{width:"+d[v]+"px;height:"+d[w]+"px}")}}if(d.style.display!=="inline"){d.style.display="inline"}hb(d);fb(d);if(a.hiResEnabled&&d.src!==d[t]){f.push(k)}else{f.push(l)}f.push("fs-"+d[K]);d.className=f.join(" ");_("imageRebuildEnd",d)}if(h.length){nb(h)}if(a.updateComplete){a.updateComplete()}vb=xb()},fb=function(b){var c,d;if(b[E][F]>1&&b[E][o]==="high"){c=b[v]===undefined?T:Math.round(b[v]*b[E][F]);d=b[w]===undefined?T:Math.round(b[w]*b[E][F]);a.hiResEnabled=U}else{c=b[v]===undefined?T:b[v];d=b[w]===undefined?T:b[w];a.hiResEnabled=V}if(!b[x]||c>b[x]||b.activeImageSetText!==b.imageSetText){b[x]=c;b[y]=d;if(b[u]&&a.hiResEnabled){b.src=b[u];b[K]="src-hi-res"}else{b.src=gb(b)}b[s]=U;b.activeImageSetText=b.imageSetText}else{b[s]=V}},gb=function(a){if(a[E][H]){a[K]="src-uri-template";return ob(a)}else if(a[E][I]&&a[E][J]){a[K]="src-find-replace";return qb(a)}a[K]="src-default";return a[t]},hb=function(b){var c,d,e={};for(c=0;c<b.imageSet.length;c++){d=b.imageSet[c];if(d[F]&&d[o]){if(a[m]==d[F]&&a[o]===d[o]){e=d;break}else if(Math.round(a[m])==d[F]&&a[o]===d[o]){e=d;break}}else if(d[F]){if(a[m]==d[F]){e=d;break}else if(Math.round(a[m])==d[F]){e=d;break}}else if(d[o]){if(a[o]===d[o]){e=d;break}}else{e=d}}if(!e[F]){e[F]=a[m]}if(!e[o]){e[o]=a[o]}e[G]=Math.round(e[F]);b[E]=e},ib=function(a,b){b=a.parentNode;if(b.clientWidth){return U}if(lb(b,"display")==="inline"){return ib(b)}return V},jb=function(a,b){if(!a.unitType){var c=a.style[S];a.style[S]="none";b=lb(a,z);a.style[S]=c;if(a[D]&&b==="auto"||b.indexOf("%")>0){a.unitType=P}else{a.unitType=R;if(a[D]&&a[D]!==O){if(a[C]){a[v]=Math.round(a[HEIGHT_UNTIS]/a[D]);a[w]=a[C]}else{a[v]=a[B]||b.replace(X,"");a[w]=Math.round(a[v]/a[D])}}else{a[v]=a[B];a[w]=a[C]}}}if(a.unitType===P||a[D]){a.computedWidth=kb(a);a[v]=a.computedWidth;if(a[D]!=O){a[w]=Math.round(a[v]/a[D])}else if(a[C]){a[w]=Math.round(a[C]*(a.computedWidth/a[B]))}if(a[w]&&d.appVersion.indexOf("MSIE")>-1){a.style.height=a[w]+"px"}}},kb=function(a){if(a.offsetWidth!==0){return a.offsetWidth}else{var b,c,d={},e={position:"absolute",visibility:"hidden",display:"block"};for(c in e){d[c]=a.style[c];a.style[c]=e[c]}b=a.offsetWidth;for(c in e){a.style[c]=d[c]}return b}},lb=function(a,b,d){if(!d){d=b}return a.currentStyle?a.currentStyle[d]:c.defaultView.getComputedStyle(a,null).getPropertyValue(b)},mb,nb=function(a,b){if(!mb){mb=c.createElement("style");mb.setAttribute("type","text/css")}b=a.join("");try{mb.innerHTML=b}catch(d){mb.styleSheet.cssText=b}if(mb.parentElement==null){c.getElementsByTagName("head")[0].appendChild(mb)}},ob=function(a){var b,c=["src","protocol","host","port","directory","file","filename","ext","query",x,y,F,G],d=a[E][H];a.uri=pb(a[t]);a.uri.src=a[t];a.uri[x]=a[x];a.uri[y]=a[y];a.uri[F]=a[E][F];a.uri[G]=a[E][G];for(b=0;b<c.length;b++){d=d.replace("{"+c[b]+"}",a.uri[c[b]])}return d},pb=function(a){var b={key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},c=b.parser.exec(a),d={},e=14;while(e--)d[b.key[e]]=c[e]||"";d[b.q.name]={};d[b.key[12]].replace(b.q.parser,function(a,c,e){if(c)d[b.q.name][c]=e});var f=d.file.split(".");d.filename=f[0];d.ext=f.length>1?f[f.length-1]:"";return d},qb=function(a){var b=a[E][I].replace("{browserWidth}",a[B]).replace("{browserHeight}",a[C]);var c,d=a[t].replace(b,a[E][J]),e=[x,y,F,G];for(c=0;c<e.length;c++){d=d.replace("{"+e[c]+"}",a[e[c]])}return d},rb=function(a){a=this;a.className=a.className.replace(k,l);a[K]="response-error";if(a.hasError||a.src===a[t])return;a.hasError=U;a.src=a[t]},sb=function(){if(Z)return;if(e.forcedBandwidth){a[o]=e.forcedBandwidth;a[q]="forced";Z=M;return}if(a[m]===1){a[q]="skip";Z=M;return}var b=d.connection||{type:"unknown"};var f=b.type==3||b.type==4||/^[23]g$/.test(b.type);a[p]=b.type;if(f){a[q]="connTypeSlow";Z=M;return}try{var j=JSON.parse(localStorage.getItem(N));if(j!==null){if((new Date).getTime()<j.exp){a[o]=j.bw;a[r]=j.kbps;a[q]="localStorage";Z=M;return}}}catch(k){}var l=c.createElement("img"),n,s,t;l.onload=function(){n=(new Date).getTime();var b=(n-s)/1e3;b=b>1?b:1;a[r]=i*1024*8/b/1024;a[o]=a[r]>=g?"high":"low";tb("networkSuccess")};l.onerror=function(){tb("networkError",5)};l.onabort=function(){tb("networkAbort",5)};s=(new Date).getTime();Z=L;if(c.location.protocol==="https:"){h=h.replace("http:","https:")}l.src=h+"?r="+Math.random();t=i*8/g*1e3+350;setTimeout(function(){tb("networkSlow")},t)},tb=function(b,c){if(Z===M)return;Z=M;a[q]=b;try{if(!c){c=j}var d={kbps:a[r],bw:a[o],exp:(new Date).getTime()+c*6e4};localStorage.setItem(N,JSON.stringify(d))}catch(e){}eb()},ub=function(){if(b.addEventListener){b.addEventListener("resize",wb,V)}else if(b.attachEvent){b.attachEvent("onresize",wb)}},vb=0,wb=function(){if(vb!==xb()){a.reload()}},xb=function(){return c.documentElement.clientWidth||c.body&&c.body.clientWidth||1024},yb,zb=function(){if(Y!==M||Z!==M)return;ab();eb()};a.resolve=function(a,b){b.imageSet=[];bb(b,a);hb(b);fb(b);b.src=gb(b)};a.reload=function(){b.clearTimeout(yb);yb=b.setTimeout(zb,250)};a.ready=function(){if(!c.body){return b.setTimeout(a.ready,1)}$()};if(c.readyState===M){setTimeout(a.ready,1)}else{if(c.addEventListener){c.addEventListener("DOMContentLoaded",a.ready,V);b.addEventListener("load",a.ready,V)}else if(c.attachEvent){c.attachEvent("onreadystatechange",a.ready);b.attachEvent("onload",a.ready)}}a[m]=b[m]?b[m]:1;if(e.forcedPixelRatio){a[m]=e.forcedPixelRatio}a[n]=Math.round(a[m]);sb();ub()})(this.foresight=this.foresight||{},this,document,navigator)