ip-checker/assets/static/script.js

51 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-10-19 04:11:20 +00:00
async function main() {
try {
const [geoIP, speedTest, ipApiIs] = await Promise.all([
fetchAsync("https://api.ip.sb/geoip"),
fetchAsync("https://api-v3.speedtest.cn/ip"),
fetchAsync("https://api.ipapi.is/"),
]);
// Update DOM for IP.sb
updateDom("check-ipsb-ip", geoIP.ip);
updateDom("check-ipsb-location", [geoIP.city, geoIP.country, geoIP.isp]);
// Update DOM for SpeedTest.cn
const { ip, city, country, isp } = speedTest.data;
updateDom("check-speedtestcn-ip", ip);
updateDom("check-speedtestcn-location", [city, country, isp]);
// Update DOM for IPAPI.is
const {
ip: ipapiIP,
location: { city: ipapiCity, country: ipapiCountry },
company: { name: companyName },
} = ipApiIs;
updateDom("check-ipapiis-ip", ipapiIP);
updateDom("check-ipapiis-location", [ipapiCity, ipapiCountry, companyName]);
} catch (error) {
console.log("Error fetching data:", error);
}
2024-08-26 22:01:24 +00:00
}
async function fetchAsync(url) {
2024-10-19 04:11:20 +00:00
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.log("Fetch error:", error);
throw error;
}
}
function updateDom(elementId, content) {
document.getElementById(elementId).innerHTML = Array.isArray(content)
? content.join(", ")
: content;
2024-08-26 22:01:24 +00:00
}
2024-10-19 04:11:20 +00:00
window.onload = main;