|
|
| 第1行: |
第1行: |
| // 访客计数:页脚显示「本站累计访问 X 次 · 今日 Y 次(Z 位访客)」
| | |
| // 数据来自 /stats.json(服务器每 5 分钟由日志生成),浏览器缓存 5 分钟,开销可忽略。
| |
| (function () {
| |
| function fmt(n) {
| |
| try { return Number(n).toLocaleString('en-US'); }
| |
| catch (e) { return n; }
| |
| }
| |
| // 只在页面完全加载后请求一次,不阻塞渲染
| |
| function run() {
| |
| fetch('/stats.json', { credentials: 'omit' }).then(function (r) {
| |
| return r.ok ? r.json() : null;
| |
| }).then(function (d) {
| |
| if (!d || typeof d.total_views === 'undefined') return;
| |
| var ul = document.getElementById('footer-info') ||
| |
| document.querySelector('#footer ul');
| |
| if (!ul || document.getElementById('footer-site-stats')) return;
| |
| var li = document.createElement('li');
| |
| li.id = 'footer-site-stats';
| |
| li.textContent = '本站累计访问 ' + fmt(d.total_views) + ' 次 · 今日 ' +
| |
| fmt(d.today_views) + ' 次(' + fmt(d.today_visitors) + ' 位访客)';
| |
| ul.appendChild(li);
| |
| }).catch(function () { /* 统计失败不影响页面 */ });
| |
| }
| |
| if (document.readyState === 'complete') run();
| |
| else window.addEventListener('load', run);
| |
| })();
| |