跳转到内容

MediaWiki:Common.js:修订间差异

来自中文精神分析知识库
Maintenance script​(留言 | 贡献)
加访客统计页脚
Maintenance script​(留言 | 贡献)
移除页脚访客统计显示
标签:替换
 
(未显示同一用户的1个中间版本)
第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.querySelector('#footer-info ul') ||
              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);
})();

2026年10月11日 (日) 14:46的最新版本