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); // --- Configuration --- const TRACKING_API_URL = API_BASE_URL+ '/track-link-click'; // Replace with your actual API endpoint document.addEventListener('DOMContentLoaded', function() { // 1. Select all elements whose 'href' attribute starts with Amazon's domain const amazonLinks = document.querySelectorAll('a[href^="https://www.amazon.com/"]'); amazonLinks.forEach(link => { link.addEventListener('click', function(event) { const clickedURL = this.getAttribute('href'); // 2. Execute the asynchronous tracking function sendTrackingReport(clickedURL); // IMPORTANT: Since 'fetch' is non-blocking (async), the browser will // navigate immediately. The tracking request fires in the background. console.log('Tracking request initiated for:', clickedURL); // If your API needs guaranteed time to respond before navigation, // you would need to use event.preventDefault() and manage navigation // within the fetch .then() block, but this is usually not recommended // for simple click tracking. }); }); }); /** * Sends an asynchronous POST request to the tracking API. * @param {string} url The URL of the Amazon link that was clicked. */ function sendTrackingReport(url) { // The payload (data) to send to your API const payload = { clickedUrl: url, timestamp: new Date().toISOString(), // Add any other necessary context, e.g., userId, pageUrl, etc. pageSource: window.location.href }; fetch(TRACKING_API_URL, { method: 'POST', // Use POST method to send data headers: { 'Content-Type': 'application/json' // Tell the server the data format }, body: JSON.stringify(payload) // Convert JavaScript object to JSON string }) .then(response => { // Optional: Handle the API response, but usually just checking for success is enough if (!response.ok) { console.error('API Tracking failed with status:', response.status); } else { // Tracking successful! // console.log('API Tracking successful.'); } }) .catch(error => { // Log any network errors (e.g., server offline, network lost) console.error('Network or fetch error during tracking:', error); }); // Note: The function returns immediately, allowing the user to navigate. }