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();