From 381ff5158d935001933dd3fc4ab2804ef05ba61b Mon Sep 17 00:00:00 2001 From: Evan Luo Date: Sat, 30 Nov 2024 13:38:39 -0500 Subject: [PATCH] refactor: simplify visitor count data handling and display logic #14 - Remove caching mechanism for visitor count data - Fetch and update visitor count on every page load - Adjust display logic to hide counters before fetching and show after - Remove immediate display of stored data on page load --- public/js/client.min.js | 2 +- src/lib/client.js | 19 ++++++------------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/public/js/client.min.js b/public/js/client.min.js index 2b1a6f4..29c423c 100644 --- a/public/js/client.min.js +++ b/public/js/client.min.js @@ -1 +1 @@ -var t,e;!function(){var n,o=[],c=!1;function a(){c=!0,document.removeEventListener("DOMContentLoaded",a),o.forEach((t=>t.call(document))),o=[]}let r=!1;t={fetch:async function(t){if(r)return;r=!0;const o=localStorage.getItem("visitorCountData");let c=!0;if(o)try{const a=JSON.parse(o);a.lastUrl!==window.location.href||(c=!1,n((()=>{t(a),e.showAll()})))}catch(t){console.error("Error parsing stored data:",t)}if(c)try{const o=await fetch("https://events.vercount.one/log?jsonpCallback=VisitorCountCallback",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:window.location.href})}),c=await o.json(),a={...c,lastUrl:window.location.href};n((()=>{t(c),localStorage.setItem("visitorCountData",JSON.stringify(a))})),e.showAll()}catch(t){console.error("Error fetching visitor count:",t),e.hideAll(),r=!1}}},e={counterIds:["site_pv","page_pv","site_uv"],updateText:function(t){this.counterIds.forEach((e=>{const n=document.getElementById("busuanzi_value_"+e);n&&(n.textContent=t[e]||"0");const o=document.getElementById("vercount_value_"+e);o&&(o.textContent=t[e]||"0")}))},hideAll:function(){this.counterIds.forEach((t=>{const e=document.getElementById("busuanzi_container_"+t);e&&(e.style.display="none");const n=document.getElementById("vercount_container_"+t);n&&(n.style.display="none")}))},showAll:function(){this.counterIds.forEach((t=>{const e=document.getElementById("busuanzi_container_"+t);e&&(e.style.display="inline");const n=document.getElementById("vercount_container_"+t);n&&(n.style.display="inline")}))}},(n=function(t){c||"interactive"===document.readyState||"complete"===document.readyState?t.call(document):(o.push(t),document.addEventListener("DOMContentLoaded",a))})((()=>{const t=localStorage.getItem("visitorCountData");if(t)try{const n=JSON.parse(t);e.updateText(n),e.showAll()}catch(t){console.error("Error loading stored data:",t)}})),t.fetch(e.updateText.bind(e))}(); \ No newline at end of file +var t,e;!function(){var n,o=[],c=!1;function i(){c=!0,document.removeEventListener("DOMContentLoaded",i),o.forEach((t=>t.call(document))),o=[]}t={fetch:async function(t){try{e.hideAll();const o=await fetch("https://events.vercount.one/log?jsonpCallback=VisitorCountCallback",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:window.location.href})}),c=await o.json();n((()=>{t(c),localStorage.setItem("visitorCountData",JSON.stringify(c)),e.showAll()}))}catch(t){console.error("Error fetching visitor count:",t),e.hideAll()}}},e={counterIds:["site_pv","page_pv","site_uv"],updateText:function(t){this.counterIds.forEach((e=>{const n=document.getElementById("busuanzi_value_"+e);n&&(n.textContent=t[e]||"0");const o=document.getElementById("vercount_value_"+e);o&&(o.textContent=t[e]||"0")}))},hideAll:function(){this.counterIds.forEach((t=>{const e=document.getElementById("busuanzi_container_"+t);e&&(e.style.display="none");const n=document.getElementById("vercount_container_"+t);n&&(n.style.display="none")}))},showAll:function(){this.counterIds.forEach((t=>{const e=document.getElementById("busuanzi_container_"+t);e&&(e.style.display="inline");const n=document.getElementById("vercount_container_"+t);n&&(n.style.display="inline")}))}},(n=function(t){c||"interactive"===document.readyState||"complete"===document.readyState?t.call(document):(o.push(t),document.addEventListener("DOMContentLoaded",i))})((()=>{t.fetch(e.updateText.bind(e))}))}(); \ No newline at end of file diff --git a/src/lib/client.js b/src/lib/client.js index bb44033..ca95851 100644 --- a/src/lib/client.js +++ b/src/lib/client.js @@ -33,6 +33,8 @@ var visitorCounterCaller, visitorCounterDisplay; const baseUrl = getBaseUrl(); const apiUrl = `${baseUrl}/log?jsonpCallback=VisitorCountCallback`; try { + visitorCounterDisplay.hideAll(); + const response = await fetch(apiUrl, { method: "POST", headers: { @@ -43,10 +45,9 @@ var visitorCounterCaller, visitorCounterDisplay; const data = await response.json(); documentReady(() => { callback(data); - // Store new values in localStorage localStorage.setItem("visitorCountData", JSON.stringify(data)); + visitorCounterDisplay.showAll(); }); - visitorCounterDisplay.showAll(); } catch (error) { console.error("Error fetching visitor count:", error); visitorCounterDisplay.hideAll(); @@ -111,17 +112,9 @@ var visitorCounterCaller, visitorCounterDisplay; }, }; - // Load and display stored data immediately documentReady(() => { - const storedData = localStorage.getItem("visitorCountData"); - if (storedData) { - visitorCounterDisplay.updateText(JSON.parse(storedData)); - visitorCounterDisplay.showAll(); - } + visitorCounterCaller.fetch( + visitorCounterDisplay.updateText.bind(visitorCounterDisplay) + ); }); - - // Fetch and update visitor count data - visitorCounterCaller.fetch( - visitorCounterDisplay.updateText.bind(visitorCounterDisplay), - ); })();