49346fd3c6
(cherry picked from commit 0b043daa341acd446c4ea536498ac2f52c82329a)
6 lines
No EOL
2.8 KiB
JavaScript
6 lines
No EOL
2.8 KiB
JavaScript
/*!
|
|
* Chirpy v3.3.1 (https://github.com/cotes2020/jekyll-theme-chirpy/)
|
|
* © 2019 Cotes Chung
|
|
* MIT Licensed
|
|
*/
|
|
const getInitStatus=function(){let t=!1;return()=>{var e=t;return t=t||!0,e}}(),PvOpts={isEnabled(){return"true"===$("meta[name=pv-cache-enabled]").attr("content")},getProxyEndpoint(){return $("meta[name=pv-proxy-endpoint]").attr("content")},getLocalData(){return $("meta[name=pv-cache-data]").attr("content")}},PvCache=function(){const t="pv",a="pv_created_date",r="pv_source",n={ORIGIN:"origin",PROXY:"proxy"};function o(e){return localStorage.getItem(e)}function i(e,t){localStorage.setItem(e,t)}return{getData(){return JSON.parse(localStorage.getItem(t))},saveOriginCache(e){i(t,e),i(r,n.ORIGIN),i(a,(new Date).toJSON())},saveProxyCache(e){i(t,e),i(r,n.PROXY),i(a,(new Date).toJSON())},isFromOrigin(){return o(r)===n.ORIGIN},isFromProxy(){return o(r)===n.PROXY},isExpired(){if(PvCache.isFromOrigin()){let e=new Date(o(a));return e.setDate(e.getDate()+1),Date.now()>=e.getTime()}if(PvCache.isFromProxy()){let e=new Date(o(a));return e.setHours(e.getHours()+1),Date.now()>=e.getTime()}return!1},getAllPageviews(){return PvCache.getData().totalsForAllResults["ga:pageviews"]},newerThan(e){return PvCache.getAllPageviews()>e.totalsForAllResults["ga:pageviews"]},inspectKeys(){null!==localStorage.getItem(t)&&null!==localStorage.getItem(r)&&null!==localStorage.getItem(a)||localStorage.clear()}}}();function countUp(t,a,r){if(t<a){let e=new CountUp(r,t,a);e.error?console.error(e.error):e.start()}}function countPV(t,a){let r=0;if(void 0!==a)for(let e=0;e<a.length;++e)if(a[parseInt(e,10)][0]===t){r+=parseInt(a[parseInt(e,10)][1],10);break}return r}function tacklePV(e,t,a,r){let n=countPV(t,e);n=0===n?1:n,r?(r=parseInt(a.text().replace(/,/g,""),10),n>r&&countUp(r,n,a.attr("id"))):a.text((new Intl.NumberFormat).format(n))}function displayPageviews(e){if(void 0!==e){let t=getInitStatus();const a=e.rows;0<$("#post-list").length?$(".post-preview").each(function(){var e=$(this).find("a").attr("href");tacklePV(a,e,$(this).find(".pageviews"),t)}):0<$(".post").length&&(e=window.location.pathname,tacklePV(a,e,$("#pv"),t))}}function fetchProxyPageviews(){$.ajax({type:"GET",url:PvOpts.getProxyEndpoint(),dataType:"jsonp",jsonpCallback:"displayPageviews",success:(e,t,a)=>{PvCache.saveProxyCache(JSON.stringify(e))},error:(e,t,a)=>{console.log("Failed to load pageviews from proxy server: "+a)}})}function fetchPageviews(e=!0,t=!1){PvOpts.isEnabled()&&e?fetch(PvOpts.getLocalData()).then(e=>e.json()).then(e=>{t&&PvCache.newerThan(e)||(displayPageviews(e),PvCache.saveOriginCache(JSON.stringify(e)))}).then(()=>fetchProxyPageviews()):fetchProxyPageviews()}$(function(){var e;0<$(".pageviews").length&&(PvCache.inspectKeys(),(e=PvCache.getData())?(displayPageviews(e),PvCache.isExpired()?fetchPageviews(!0,PvCache.isFromProxy()):PvCache.isFromOrigin()&&fetchPageviews(!1)):fetchPageviews())}); |