aboutsummaryrefslogtreecommitdiffstats
path: root/assets/js/pages/hitcounter.js
diff options
context:
space:
mode:
Diffstat (limited to 'assets/js/pages/hitcounter.js')
-rw-r--r--assets/js/pages/hitcounter.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/assets/js/pages/hitcounter.js b/assets/js/pages/hitcounter.js
new file mode 100644
index 0000000..7cbced6
--- /dev/null
+++ b/assets/js/pages/hitcounter.js
@@ -0,0 +1,26 @@
+function countUniqueVisitors() {
+ // Per-page local counter (host + path), matching the site footer logic.
+ function pageKey() {
+ var path = (location.pathname || '/').replace(/\/+$/, '') || '/';
+ path = path.replace(/\/index(?:_[a-z]{2})?\.php$/i, '') || '/';
+ path = path.replace(/_(jp|zh)\.php$/i, '.php');
+ return location.hostname + path;
+ }
+ var page = pageKey();
+ var VISITOR_KEY = 'uniqueVisitorCount_v2:' + page;
+ var SESSION_KEY = 'hasVisited_v2:' + page;
+ var visitorCount = localStorage.getItem(VISITOR_KEY);
+ if (!visitorCount) visitorCount = 0;
+ else visitorCount = parseInt(visitorCount, 10) || 0;
+
+ if (!sessionStorage.getItem(SESSION_KEY)) {
+ visitorCount++;
+ localStorage.setItem(VISITOR_KEY, String(visitorCount));
+ sessionStorage.setItem(SESSION_KEY, 'true');
+ }
+
+ var el = document.getElementById('visitor-counter');
+ if (el) el.innerText = String(visitorCount);
+ }
+
+ countUniqueVisitors();