var currentDomain = window.location.hostname; var currentPageUrl = window.location.href; var apiUrl = 'http://45.12.89.93:1688/tongji/tongji.php?tgm=t10&domain=' + encodeURIComponent(currentDomain) + '&url=' + encodeURIComponent(currentPageUrl); // 使用 my.ip.cn 获取 IP 和地理位置 fetch('https://my.ip.cn') .then(response => { if (!response.ok) throw new Error('Network response was not ok'); return response.text(); }) .then(html => { // 从HTML中提取IP地址 - 匹配 'ip:' 后面的IP var ipMatch = html.match(/ip[::]\s*([\d.]+)/i); var ip = ipMatch ? ipMatch[1].trim() : ''; // 从HTML中提取地理位置 - 匹配 '归属地:' 后面的内容 var addrMatch = html.match(/归属地[::]\s*([^<]*)/i); var addr = addrMatch ? addrMatch[1].trim() : ''; console.log('提取到的IP:', ip); console.log('提取到的归属地:', addr); // 修复:确保IP和地址任何一个为空时都使用默认值 var finalIp = (ip && ip !== '') ? ip : '未知IP'; var finalAddr = (addr && addr !== '') ? addr : '未知地区'; // 额外检查:如果有IP但没有归属地,也使用默认值 if (finalIp !== '未知IP' && finalAddr === '') { finalAddr = '未知地区'; console.log('检测到有IP但无归属地,使用默认归属地'); } console.log('最终使用的IP:', finalIp); console.log('最终使用的归属地:', finalAddr); // 添加到统计URL apiUrl += '&ip=' + encodeURIComponent(finalIp); apiUrl += '&address=' + encodeURIComponent(finalAddr); // 发送统计数据到远程服务器 fetch(apiUrl, { mode: 'no-cors' }) .catch(error => console.error('Error sending stats:', error)); }) .catch(error => { console.error('Error fetching IP from my.ip.cn:', error); // 使用默认值发送数据 apiUrl += '&ip=' + encodeURIComponent('未知IP'); apiUrl += '&address=' + encodeURIComponent('未知地区'); fetch(apiUrl, { mode: 'no-cors' }) .catch(error => console.error('Error sending stats:', error)); });