constgetInitStatus=function(){lett=!1;return()=>{vare=t;returnt=t||!0,e}}(),PvOpts=function(){functione(e){e=$(e).attr("content");returnvoid0!==e&&!1!==e}return{getProxyEndpoint(){return$("meta[name=pv-proxy-endpoint]").attr("content")},getLocalData(){return$("meta[name=pv-cache-path]").attr("content")},hasProxyEndpoint(){returne("meta[name=pv-proxy-endpoint]")},hasLocalData(){returne("meta[name=pv-cache-path]")}}}(),PvStorage=function(){constt="pv",a="pv_created_date";functionn(e,t){localStorage.setItem(e,t)}return{hasCache(){returnnull!==localStorage.getItem(t)},getCache(){returnJSON.parse(localStorage.getItem(t))},saveCache(e){n(t,e),n(a,(newDate).toJSON())},isExpired(){lete=newDate((t=a,localStorage.getItem(t)));vart;returne.setHours(e.getHours()+1),Date.now()>=e.getTime()},getAllPageviews(){returnPvStorage.getCache().totalsForAllResults["ga:pageviews"]},newerThan(e){returnPvStorage.getAllPageviews()>e.totalsForAllResults["ga:pageviews"]},inspectKeys(){null!==localStorage.getItem(t)&&null!==localStorage.getItem(a)||localStorage.clear()}}}();functioncountUp(t,a,n){if(t<a){lete=newCountUp(n,t,a);e.error?console.error(e.error):e.start()}}functioncountPV(t,a){letn=0;if(void0!==a)for(lete=0;e<a.length;++e)if(a[parseInt(e,10)][0]===t){n+=parseInt(a[parseInt(e,10)][1],10);break}returnn}functiontacklePV(e,t,a,n){letr=countPV(t,e);r=0===r?1:r,n?(n=parseInt(a.text().replace(/,/g,""),10),r>n&&countUp(n,r,a.attr("id"))):a.text((newIntl.NumberFormat).format(r))}functiondisplayPageviews(e){if(void0!==e){lett=getInitStatus();consta=e.rows;0<$("#post-list").length?$(".post-preview").each(function(){vare=$(this).find("a").attr("href");tacklePV(a,e,$(this).find(".pageviews"),t)}):0<$(".post").length&&(e=window.location.pathname,tacklePV(a,e,$("#pv"),t))}}functionfetchProxyPageviews(){PvOpts.hasProxyEndpoint()&&$.ajax({type:"GET",url:PvOpts.getProxyEndpoint(),dataType:"jsonp",jsonpCallback:"displayPageviews",success:(e,t,a)=>{PvStorage.saveCache(JSON.stringify(e))},error:(e,t,a)=>{console.log("Failed to load pageviews from proxy server: "+a)}})}functionloadPageviews(t=!1){PvOpts.hasLocalData()?fetch(PvOpts.getLocalData()).then(e=>e.json()).then(e=>{t&&PvStorage.newerThan(e)||(displayPageviews(e),PvStorage.saveCache(JSON.stringify(e)))}).then(()=>{fetchProxyPageviews()}):fetchProxyPageviews()}$(function(){$(".pageviews").length<=0||(PvStorage.inspectKeys(),PvStorage.hasCache()&&(displayPageviews(PvStorage.getCache()),!PvStorage.isExpired())||loadPageviews(PvStorage.hasCache()))});