- busStop.html: added the third tab (data-mode=near) and its own panel with the location button, hint and result area; removed the near button from the toolbar and deleted the old on-screen numpad markup - busStop.search.js: MODE_NEAR in setMode, disables the other panels, hides the stop list while ranked results are shown, skips filtering in that mode, and removed the dead initNumpad code - security gate: the tab is revealed only when isSecureContext and navigator.geolocation exist; otherwise it stays hidden and the mode is refused - map.js: re-enable the button after a successful lookup - i18n: stops.modeNear / nearCta / nearHint - bump SW version to 2026.09.27.98 Verified on localhost:8090 - three tabs, near mode shows a map with 8 ranked stops, button re-enables and works again, switching back restores the list; with isSecureContext forced false the tab is hidden and unselectable.
231 lines
11 KiB
JavaScript
231 lines
11 KiB
JavaScript
/* visitors.js — นับผู้เข้าชมจริง (ฝั่งเบราว์เซอร์ ไม่มี backend) + รีเซ็ตด้วยรหัส
|
|
|
|
แนวคิด
|
|
แสดงผล = ฐาน + จำนวนผู้เข้าชมจริง (unique)
|
|
ฐานเริ่มต้น = 12,834 (แสดงเมื่อยังไม่เคยรีเซ็ต)
|
|
รีเซ็ต (รหัส) = ล้างฐานทิ้งด้วย → เริ่มนับใหม่จาก 0
|
|
|
|
กันจำนวนเฟ้อ 3 ชั้น
|
|
1. sessionStorage → 1 รอบการเข้าชม (เปิดแท็บใหม่) นับ 1 · กด F5 ไม่นับ
|
|
2. localStorage → 1 ครั้ง/วัน/เครื่อง (วันเดียวกันนับครั้งเดียว)
|
|
3. dayKey กำกับ → ล้าง session/incognito เปิดซ้ำวันเดิม = ไม่นับ (เพดาน/วัน)
|
|
|
|
ประหยัดพื้นที่
|
|
เก็บแค่ {"v": <จำนวนจริง>, "d": <วัน YYYY-MM-DD>} ≈ 35 ไบต์ · ไม่เก็บ log/IP/UA
|
|
|
|
รีเซ็ต
|
|
กดที่ตัวเลขผู้เข้าชม 5 ครั้งติด → เปิดช่องกรอกรหัส (ซ่อน ไม่มีปุ่มให้คนอื่นกด)
|
|
รหัสตรวจแบบ SHA-256 (ไม่เก็บรหัสจริงในโค้ด — เปิด view-source ไม่เจอ)
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
var KEY = 'at_visitors'; /* { v: จำนวนจริง, d: 'YYYY-MM-DD' } */
|
|
var SESSION_KEY = 'at_visitors_seen';
|
|
var BASE = 12834; /* ฐานตั้งต้น — รีเซ็ตแล้วกลับมาที่นี่ */
|
|
var TAPS = 5; /* จำนวนครั้งที่ต้องกดตัวเลข */
|
|
var TAP_WINDOW = 2000; /* ต้องกดรัวภายใน 2 วิต่อครั้ง */
|
|
|
|
/* แปลข้อความ (ถ้าโหลดคำแปลไม่ทัน/ไม่มีคีย์ → ใช้ข้อความไทยที่ฝังไว้) */
|
|
function vt(key, fallback) {
|
|
var v = (window.I18N && I18N.t) ? I18N.t(key) : key;
|
|
return (!v || v === key) ? fallback : v;
|
|
}
|
|
|
|
/* SHA-256 ของรหัสรีเซ็ต — เก็บเฉพาะ hash (ไม่เก็บรหัสจริงในไฟล์นี้) */
|
|
var PASS_HASH = '4f23cd6ac8e7da56b92974cd0cf146eee9d995208a6645ed17eba91813c1d323';
|
|
|
|
function today() {
|
|
var d = new Date();
|
|
var m = String(d.getMonth() + 1);
|
|
var day = String(d.getDate());
|
|
if (m.length < 2) m = '0' + m;
|
|
if (day.length < 2) day = '0' + day;
|
|
return d.getFullYear() + '-' + m + '-' + day;
|
|
}
|
|
|
|
function read() {
|
|
try {
|
|
var raw = localStorage.getItem(KEY);
|
|
var d = raw ? JSON.parse(raw) : null;
|
|
if (!d || typeof d !== 'object') return { b: null, v: 0, d: '' };
|
|
return {
|
|
b: (d.b === undefined || d.b === null) ? null : Math.max(0, Number(d.b) || 0),
|
|
v: Math.max(0, Number(d.v) || 0),
|
|
d: String(d.d || '')
|
|
};
|
|
} catch (e) { return { b: null, v: 0, d: '' }; }
|
|
}
|
|
|
|
function write(d) {
|
|
try { localStorage.setItem(KEY, JSON.stringify(d)); } catch (e) { /* เงียบ */ }
|
|
}
|
|
|
|
/* ตรวจว่ารอบการเข้าชมนี้ "ควรนับ" — ผ่านครบ 3 ชั้น */
|
|
function shouldCount(store) {
|
|
/* ชั้น 1: sessionStorage — รอบนี้เคยนับแล้ว? (F5 ไม่นับ) */
|
|
var seen = null;
|
|
try { seen = sessionStorage.getItem(SESSION_KEY); } catch (e) { /* เงียบ */ }
|
|
if (seen) return false;
|
|
|
|
/* ชั้น 2+3: วันเดียวกันห้ามเพิ่ม (แม้ sessionStorage ถูกล้าง) */
|
|
if (store.d === today()) return false;
|
|
return true;
|
|
}
|
|
|
|
function fmt(n) { return Number(n).toLocaleString('en-US'); }
|
|
|
|
/* ── รหัส: SHA-256 ผ่าน SubtleCrypto (มีในทุกเบราว์เซอร์สมัยใหม่) ── */
|
|
function hash(text, cb) {
|
|
if (window.crypto && crypto.subtle && crypto.subtle.digest) {
|
|
crypto.subtle.digest('SHA-256', new TextEncoder().encode(text))
|
|
.then(function (buf) {
|
|
var b = new Uint8Array(buf), s = '';
|
|
for (var i = 0; i < b.length; i++) s += ('0' + b[i].toString(16)).slice(-2);
|
|
cb(s);
|
|
})
|
|
.catch(function () { cb(null); });
|
|
} else {
|
|
cb(null); /* เบราว์เซอร์เก่า/ไม่มี https → กรุณาใช้ http://localhost */
|
|
}
|
|
}
|
|
|
|
/* เทียบแบบ constant-time (กัน timing attack) */
|
|
function safeEqual(a, b) {
|
|
if (typeof a !== 'string' || typeof b !== 'string' || a.length !== b.length) return false;
|
|
var diff = 0;
|
|
for (var i = 0; i < a.length; i++) diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
return diff === 0;
|
|
}
|
|
|
|
var Visitors = {
|
|
BASE: BASE, /* ฐานตั้งต้น (ค่าคงที่) — ดู Visitors._base สำหรับฐานที่ใช้จริง */
|
|
_base: BASE,
|
|
|
|
/* เรียกครั้งเดียวตอนโหลดหน้า — นับ (ถ้าควรนับ) แล้วคืนค่าที่จะแสดง */
|
|
init: function () {
|
|
var store = read();
|
|
if (shouldCount(store)) {
|
|
store.v = (Number(store.v) || 0) + 1;
|
|
store.d = today();
|
|
write(store);
|
|
try { sessionStorage.setItem(SESSION_KEY, '1'); } catch (e) { /* เงียบ */ }
|
|
}
|
|
/* ฐาน: ยังไม่เคยรีเซ็ต → BASE · เคยรีเซ็ต → ค่าที่บันทึกไว้ (0) */
|
|
var base = (store.b === null) ? BASE : store.b;
|
|
var el = document.getElementById('visitor-count');
|
|
if (el) el.textContent = fmt(base + store.v);
|
|
Visitors._base = base;
|
|
Visitors._store = store;
|
|
return base + store.v;
|
|
},
|
|
|
|
/* จำนวนจริง (ไม่รวม BASE) */
|
|
actual: function () { return read().v; },
|
|
|
|
/* รีเซ็ต: ล้างฐานทิ้งด้วย → เริ่มนับใหม่จาก 0 (ตามที่เลือก) */
|
|
reset: function () {
|
|
write({ b: 0, v: 0, d: today() });
|
|
/* ไม่ล้าง sessionStorage — กันการนับซ้ำทันทีหลังรีเซ็ตในรอบเดียวกัน */
|
|
Visitors._base = 0;
|
|
var el = document.getElementById('visitor-count');
|
|
if (el) el.textContent = fmt(0);
|
|
return 0;
|
|
},
|
|
|
|
/* ตรวจรหัสแล้วรีเซ็ต (async — SHA-256) */
|
|
resetWith: function (pass, done) {
|
|
hash(String(pass == null ? '' : pass), function (h) {
|
|
if (!h) { done(false, 'unsupported'); return; }
|
|
if (!safeEqual(h, PASS_HASH)) { done(false, 'wrong'); return; }
|
|
Visitors.reset();
|
|
done(true, 'ok');
|
|
});
|
|
},
|
|
|
|
_hash: hash,
|
|
_key: KEY
|
|
};
|
|
|
|
window.Visitors = Visitors;
|
|
|
|
/* ── ช่องทางลับ: กดที่ตัวเลข 5 ครั้งติด → เปิดช่องกรอกรหัส ── */
|
|
function mountTap() {
|
|
var el = document.getElementById('visitor-count');
|
|
if (!el) return;
|
|
|
|
var taps = 0, timer = null;
|
|
el.style.cursor = 'pointer';
|
|
el.addEventListener('click', function () {
|
|
taps++;
|
|
if (timer) clearTimeout(timer);
|
|
timer = setTimeout(function () { taps = 0; }, TAP_WINDOW);
|
|
if (taps >= TAPS) {
|
|
taps = 0;
|
|
clearTimeout(timer);
|
|
openDialog();
|
|
}
|
|
});
|
|
}
|
|
|
|
function openDialog() {
|
|
if (document.querySelector('.mu-vreset')) return;
|
|
|
|
var wrap = document.createElement('div');
|
|
wrap.className = 'mu-vreset';
|
|
wrap.innerHTML =
|
|
'<div class="mu-vreset-card" role="dialog" aria-modal="true" aria-labelledby="vreset-title">' +
|
|
'<h4 id="vreset-title">' + vt('visitors.resetTitle', 'รีเซ็ตจำนวนผู้เข้าชม') + '</h4>' +
|
|
'<label for="vreset-pass">' + vt('visitors.resetPass', 'รหัสรีเซ็ต') + '</label>' +
|
|
'<input id="vreset-pass" type="password" autocomplete="off" inputmode="text">' +
|
|
'<p class="mu-vreset-msg" role="status"></p>' +
|
|
'<div class="mu-vreset-row">' +
|
|
'<button type="button" class="mu-vreset-cancel">' + vt('visitors.cancel', 'ยกเลิก') + '</button>' +
|
|
'<button type="button" class="mu-vreset-ok">' + vt('visitors.confirm', 'รีเซ็ต') + '</button>' +
|
|
'</div>' +
|
|
'</div>';
|
|
|
|
var input = wrap.querySelector('#vreset-pass');
|
|
var msg = wrap.querySelector('.mu-vreset-msg');
|
|
|
|
function close() { wrap.remove(); }
|
|
function submit() {
|
|
msg.textContent = '';
|
|
Visitors.resetWith(input.value, function (ok, why) {
|
|
if (ok) {
|
|
msg.className = 'mu-vreset-msg is-ok';
|
|
msg.textContent = vt('visitors.done', 'รีเซ็ตแล้ว — เริ่มนับใหม่จาก 0');
|
|
input.value = '';
|
|
setTimeout(close, 1200);
|
|
} else if (why === 'unsupported') {
|
|
msg.textContent = vt('visitors.unsupported', 'เบราว์เซอร์ไม่รองรับ');
|
|
} else {
|
|
msg.className = 'mu-vreset-msg is-bad';
|
|
msg.textContent = vt('visitors.badPass', 'รหัสไม่ถูกต้อง');
|
|
input.value = '';
|
|
input.focus();
|
|
}
|
|
});
|
|
}
|
|
|
|
wrap.querySelector('.mu-vreset-ok').addEventListener('click', submit);
|
|
wrap.querySelector('.mu-vreset-cancel').addEventListener('click', close);
|
|
wrap.addEventListener('click', function (e) { if (e.target === wrap) close(); });
|
|
input.addEventListener('keydown', function (e) {
|
|
if (e.key === 'Enter') submit();
|
|
if (e.key === 'Escape') close();
|
|
});
|
|
|
|
document.body.appendChild(wrap);
|
|
input.focus();
|
|
}
|
|
|
|
/* ── nับตอนโหลด + ติดช่องทางลับ ── */
|
|
function boot() {
|
|
Visitors.init();
|
|
mountTap();
|
|
}
|
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot);
|
|
else boot();
|
|
})();
|