const API_BASE_URL = 'https://wc.claphub.com/api/v1'; // 替换为你的 Java API 地址 const COUNTER_ELEMENT_ID = 'visit-counter'; function updateVisitCounter() { const counterElement = document.getElementById(COUNTER_ELEMENT_ID); const visitorData = { url: window.location.href, referrer: document.referrer, screen: screen.width + 'x' + screen.height, timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, userAgent: navigator.userAgent, timestamp: new Date().toISOString() }; // 这里使用 POST 请求来触发计数 fetch(API_BASE_URL + '/count', { method: 'POST', headers: { 'Content-Type': 'application/json' // 浏览器会自动附加 'Origin' 头,包含当前网站的域名 }, body: JSON.stringify(visitorData) }) .then(response => { if (!response.ok) { throw new Error('Server returned error status: ' + response.status); } return response.json(); }) .then(data => { if (counterElement && data && typeof data.count === 'number') { counterElement.textContent = data.count.toLocaleString(); } }) .catch(error => { console.error('Failed to update visit count for domain:', error); if (counterElement) { counterElement.textContent = '---'; } }); } window.addEventListener('load', updateVisitCounter);