ip-checker/assets/static/script.js

50 lines
1.4 KiB
JavaScript

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);
}
}
async function fetchAsync(url) {
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;
}
window.onload = main;