feat(analytics): 更新不蒜子统计脚本并移除本地格式化逻辑
- 将不蒜子统计脚本源地址更新为官方 CDN 地址 - 移除本地实现的访问量格式化脚本 - 依赖外部库自带的格式化功能替代原有手动实现 - 简化代码结构,减少冗余逻辑 - 提升页面加载性能与维护性 - 统一使用外部库的标准格式化规则
This commit is contained in:
65
about.html
65
about.html
@@ -363,70 +363,5 @@
|
|||||||
|
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- 自动格式化脚本 -->
|
|
||||||
<script>
|
|
||||||
// 核心格式化函数:支持 K / W / M,保留最多两位小数,去除尾随零
|
|
||||||
function formatWithUnit(num) {
|
|
||||||
num = Number(num);
|
|
||||||
if (isNaN(num) || num < 0) return '0';
|
|
||||||
|
|
||||||
// 小于 1000:直接显示
|
|
||||||
if (num < 1_000) {
|
|
||||||
return Math.floor(num).toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1K ~ 9.99K
|
|
||||||
if (num < 10_000) {
|
|
||||||
let val = (num / 1_000).toFixed(2);
|
|
||||||
return parseFloat(val) + 'K';
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1W ~ 99.99W (1W = 10,000)
|
|
||||||
if (num < 1_000_000) {
|
|
||||||
let val = (num / 10_000).toFixed(2);
|
|
||||||
return parseFloat(val) + 'W';
|
|
||||||
}
|
|
||||||
|
|
||||||
// ≥ 1M
|
|
||||||
let val = (num / 1_000_000).toFixed(2);
|
|
||||||
return parseFloat(val) + 'M';
|
|
||||||
}
|
|
||||||
|
|
||||||
// 监听不蒜子数据更新并格式化
|
|
||||||
function initFormatter() {
|
|
||||||
const pvEl = document.getElementById(SiteConfig.analytics.busuanzi.site_pv_id);
|
|
||||||
const uvEl = document.getElementById(SiteConfig.analytics.busuanzi.site_uv_id);
|
|
||||||
|
|
||||||
if (!pvEl && !uvEl) return;
|
|
||||||
|
|
||||||
const observer = new MutationObserver(() => {
|
|
||||||
if (pvEl?.textContent) {
|
|
||||||
const raw = pvEl.textContent.trim().replace(/[,,]/g, '');
|
|
||||||
const num = parseFloat(raw);
|
|
||||||
if (!isNaN(num)) {
|
|
||||||
pvEl.textContent = formatWithUnit(num);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (uvEl?.textContent) {
|
|
||||||
const raw = uvEl.textContent.trim().replace(/[,,]/g, '');
|
|
||||||
const num = parseFloat(raw);
|
|
||||||
if (!isNaN(num)) {
|
|
||||||
uvEl.textContent = formatWithUnit(num);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (pvEl) observer.observe(pvEl, { childList: true, subtree: true });
|
|
||||||
if (uvEl) observer.observe(uvEl, { childList: true, subtree: true });
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动
|
|
||||||
if (document.readyState === 'loading') {
|
|
||||||
document.addEventListener('DOMContentLoaded', initFormatter);
|
|
||||||
} else {
|
|
||||||
initFormatter();
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -133,7 +133,7 @@ const SiteConfig = {
|
|||||||
// 站点统计配置
|
// 站点统计配置
|
||||||
analytics: {
|
analytics: {
|
||||||
busuanzi: {
|
busuanzi: {
|
||||||
src: 'https://events.vercount.one/js',
|
src: '//cdn.busuanzi.cc/busuanzi/3.6.9/busuanzi.abbr.min.js',
|
||||||
site_pv_id: 'busuanzi_value_site_pv',
|
site_pv_id: 'busuanzi_value_site_pv',
|
||||||
site_uv_id: 'busuanzi_value_site_uv',
|
site_uv_id: 'busuanzi_value_site_uv',
|
||||||
formatter: true
|
formatter: true
|
||||||
|
|||||||
Reference in New Issue
Block a user