-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog-count.js
71 lines (58 loc) · 2.41 KB
/
blog-count.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function () {
async function fetchVisitorStats() {
const hostname = window.location.hostname;
const target = hostname + window.location.pathname;
const baseUrl = `https://<你的域名>/visitor-count?target=${encodeURIComponent(target)}`;
console.log(`目标 API 地址: ${baseUrl}`);
try {
console.log('访问数更新');
await fetch(baseUrl, { method: 'POST', credentials: 'include' });
const visitorTracked = getCookie('visitorTracked');
if (!visitorTracked) {
await fetch(`${baseUrl}&newVisitor=true`, { method: 'POST', credentials: 'include' });
}
console.log('获取最新统计数据');
const response = await fetch(baseUrl, { method: 'GET', credentials: 'include' });
if (!response.ok) throw new Error(`统计请求失败,状态码:${response.status}`);
const data = await response.json();
console.log('统计数据解析成功:', data);
updateStatistics(data);
if (!visitorTracked) setCookie('visitorTracked', 'true', 365);
} catch (error) {
console.error('获取或更新统计数据出错:', error);
}
}
function updateStatistics(data) {
const updateElement = (id, value) => {
const element = document.getElementById(id);
if (element) {
element.textContent = value || 0;
console.log(`更新元素 ${id}: ${value || 0}`);
} else {
console.log(`元素 ${id} 不存在,跳过更新`);
}
};
updateElement('PageMeter_site_uv', data.site?.visitor_count);
updateElement('PageMeter_site_pv', data.site?.visit_count);
updateElement('PageMeter_page_uv', data.page?.visitor_count);
updateElement('PageMeter_page_pv', data.page?.visit_count);
}
function setCookie(name, value, days) {
const expires = new Date(Date.now() + days * 864e5).toUTCString();
document.cookie = `${name}=${encodeURIComponent(value)}; expires=${expires}; path=/`;
}
function getCookie(name) {
const cookies = document.cookie.split(';');
for (let c of cookies) {
const trimmed = c.trim();
if (trimmed.startsWith(`${name}=`)) return trimmed.substring(name.length + 1);
}
return null;
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fetchVisitorStats);
} else {
fetchVisitorStats();
}
// setInterval(fetchVisitorStats, 60000); // 可选,每 60 秒刷新统计数据
})();