@@ -18,30 +18,42 @@ const getInitStatus = (function () {
1818} ( ) ) ;
1919
2020const PvOpts = ( function ( ) {
21+ function getContent ( selector ) {
22+ return $ ( selector ) . attr ( "content" ) ;
23+ }
24+
2125 function hasContent ( selector ) {
22- let content = $ ( selector ) . attr ( "content" ) ;
26+ let content = getContent ( selector ) ;
2327 return ( typeof content !== "undefined" && content !== false ) ;
2428 }
2529
2630 return {
27- getProxyEndpoint ( ) {
28- return $ ( "meta[name=pv-proxy-endpoint]" ) . attr ( "content ") ;
31+ getProxyMeta ( ) {
32+ return getContent ( "meta[name=pv-proxy-endpoint]" ) ;
2933 } ,
30- getLocalData ( ) {
31- return $ ( "meta[name=pv-cache-path]" ) . attr ( "content ") ;
34+ getLocalMeta ( ) {
35+ return getContent ( "meta[name=pv-cache-path]" ) ;
3236 } ,
33- hasProxyEndpoint ( ) {
37+ hasProxyMeta ( ) {
3438 return hasContent ( "meta[name=pv-proxy-endpoint]" ) ;
3539 } ,
36- hasLocalData ( ) {
40+ hasLocalMeta ( ) {
3741 return hasContent ( "meta[name=pv-cache-path]" ) ;
3842 }
3943 }
4044} ( ) ) ;
4145
4246const PvStorage = ( function ( ) {
43- const KEY_PV = "pv" ;
44- const KEY_CREATION = "pv_created_date" ;
47+ const Keys = {
48+ KEY_PV : "pv" ,
49+ KEY_PV_SRC : "pv_src" ,
50+ KEY_CREATION : "pv_created_date"
51+ } ;
52+
53+ const Source = {
54+ LOCAL : "same-origin" ,
55+ PROXY : "cors"
56+ } ;
4557
4658 function get ( key ) {
4759 return localStorage . getItem ( key ) ;
@@ -51,35 +63,54 @@ const PvStorage = (function () {
5163 localStorage . setItem ( key , val ) ;
5264 }
5365
66+ function saveCache ( pv , src ) {
67+ set ( Keys . KEY_PV , pv ) ;
68+ set ( Keys . KEY_PV_SRC , src ) ;
69+ set ( Keys . KEY_CREATION , new Date ( ) . toJSON ( ) ) ;
70+ }
71+
5472 return {
73+ keysCount ( ) {
74+ return Object . keys ( Keys ) . length ;
75+ } ,
5576 hasCache ( ) {
56- return ( localStorage . getItem ( KEY_PV ) !== null ) ;
77+ return ( localStorage . getItem ( Keys . KEY_PV ) !== null ) ;
5778 } ,
5879 getCache ( ) {
59- // get data from browser cache
60- return JSON . parse ( localStorage . getItem ( KEY_PV ) ) ;
80+ return JSON . parse ( localStorage . getItem ( Keys . KEY_PV ) ) ;
6181 } ,
62- saveCache ( pv ) {
63- set ( KEY_PV , pv ) ;
64- set ( KEY_CREATION , new Date ( ) . toJSON ( ) ) ;
82+ saveLocalCache ( pv ) {
83+ saveCache ( pv , Source . LOCAL ) ;
84+ } ,
85+ saveProxyCache ( pv ) {
86+ saveCache ( pv , Source . PROXY ) ;
6587 } ,
6688 isExpired ( ) {
67- let date = new Date ( get ( KEY_CREATION ) ) ;
68- date . setHours ( date . getHours ( ) + 1 ) ; // per hour
89+ let date = new Date ( get ( Keys . KEY_CREATION ) ) ;
90+ date . setHours ( date . getHours ( ) + 1 ) ; // per hour
6991 return Date . now ( ) >= date . getTime ( ) ;
7092 } ,
71- getAllPageviews ( ) {
72- return PvStorage . getCache ( ) . totalsForAllResults [ "ga:pageviews" ] ;
93+ isFromLocal ( ) {
94+ return get ( Keys . KEY_PV_SRC ) === Source . LOCAL ;
95+ } ,
96+ isFromProxy ( ) {
97+ return get ( Keys . KEY_PV_SRC ) === Source . PROXY ;
7398 } ,
7499 newerThan ( pv ) {
75- return PvStorage . getAllPageviews ( ) > pv . totalsForAllResults [ "ga:pageviews" ] ;
100+ return PvStorage . getCache ( ) . totalsForAllResults [ "ga:pageviews" ] > pv . totalsForAllResults [ "ga:pageviews" ] ;
76101 } ,
77102 inspectKeys ( ) {
103+ if ( localStorage . length !== PvStorage . keysCount ( ) ) {
104+ localStorage . clear ( ) ;
105+ return ;
106+ }
107+
78108 for ( let i = 0 ; i < localStorage . length ; i ++ ) {
79109 const key = localStorage . key ( i ) ;
80110 switch ( key ) {
81- case KEY_PV :
82- case KEY_CREATION :
111+ case Keys . KEY_PV :
112+ case Keys . KEY_PV_SRC :
113+ case Keys . KEY_CREATION :
83114 break ;
84115 default :
85116 localStorage . clear ( ) ;
@@ -152,14 +183,14 @@ function displayPageviews(data) {
152183}
153184
154185function fetchProxyPageviews ( ) {
155- if ( PvOpts . hasProxyEndpoint ( ) ) {
186+ if ( PvOpts . hasProxyMeta ( ) ) {
156187 $ . ajax ( {
157188 type : "GET" ,
158- url : PvOpts . getProxyEndpoint ( ) ,
189+ url : PvOpts . getProxyMeta ( ) ,
159190 dataType : "jsonp" ,
160191 jsonpCallback : "displayPageviews" ,
161- success : ( data , textStatus , jqXHR ) => {
162- PvStorage . saveCache ( JSON . stringify ( data ) ) ;
192+ success : ( data ) => {
193+ PvStorage . saveProxyCache ( JSON . stringify ( data ) ) ;
163194 } ,
164195 error : ( jqXHR , textStatus , errorThrown ) => {
165196 console . log ( "Failed to load pageviews from proxy server: " + errorThrown ) ;
@@ -168,26 +199,19 @@ function fetchProxyPageviews() {
168199 }
169200}
170201
171- function loadPageviews ( hasCache = false ) {
172- if ( PvOpts . hasLocalData ( ) ) {
173- fetch ( PvOpts . getLocalData ( ) )
174- . then ( ( response ) => response . json ( ) )
175- . then ( ( data ) => {
202+ function fetchLocalPageviews ( hasCache = false ) {
203+ return fetch ( PvOpts . getLocalMeta ( ) )
204+ . then ( response => response . json ( ) )
205+ . then ( data => {
206+ if ( hasCache ) {
176207 // The cache from the proxy will sometimes be more recent than the local one
177- if ( hasCache && PvStorage . newerThan ( data ) ) {
208+ if ( PvStorage . isFromProxy ( ) && PvStorage . newerThan ( data ) ) {
178209 return ;
179210 }
180- displayPageviews ( data ) ;
181- PvStorage . saveCache ( JSON . stringify ( data ) ) ;
182- } )
183- . then ( ( ) => {
184- fetchProxyPageviews ( ) ;
185- } ) ;
186-
187- } else {
188- fetchProxyPageviews ( ) ;
189- }
190-
211+ }
212+ displayPageviews ( data ) ;
213+ PvStorage . saveLocalCache ( JSON . stringify ( data ) ) ;
214+ } ) ;
191215}
192216
193217$ ( function ( ) {
@@ -199,11 +223,27 @@ $(function() {
199223
200224 if ( PvStorage . hasCache ( ) ) {
201225 displayPageviews ( PvStorage . getCache ( ) ) ;
202- if ( ! PvStorage . isExpired ( ) ) {
203- return ;
226+
227+ if ( PvStorage . isExpired ( ) ) {
228+ if ( PvOpts . hasLocalMeta ( ) ) {
229+ fetchLocalPageviews ( true ) . then ( fetchProxyPageviews ) ;
230+ } else {
231+ fetchProxyPageviews ( ) ;
232+ }
233+
234+ } else {
235+ if ( PvStorage . isFromLocal ( ) ) {
236+ fetchProxyPageviews ( ) ;
237+ }
204238 }
205- }
206239
207- loadPageviews ( PvStorage . hasCache ( ) ) ;
240+ } else { // no cached
241+
242+ if ( PvOpts . hasLocalMeta ( ) ) {
243+ fetchLocalPageviews ( ) . then ( fetchProxyPageviews ) ;
244+ } else {
245+ fetchProxyPageviews ( ) ;
246+ }
247+ }
208248
209249} ) ;
0 commit comments