ip-checker/ui/script.js

65 lines
1.6 KiB
JavaScript

async function main() {
try {
const [ny4, ipsb, speedtestcn, ipapiis] = await Promise.all([
fetchAsync("https://ip.ny4.dev/api/v1/ip"),
fetchAsync("https://api.ip.sb/geoip"),
fetchAsync("https://api-v3.speedtest.cn/ip"),
fetchAsync("https://api.ipapi.is/"),
]);
updateDom("check-ny4-ip", ny4.ip);
updateDom("check-ny4-location", [
ny4.city,
ny4.region,
ny4.country,
ny4.organization,
]);
updateDom("check-ipsb-ip", ipsb.ip);
updateDom("check-ipsb-location", [
ipsb.city,
ipsb.region,
ipsb.country,
ipsb.isp,
]);
updateDom("check-speedtestcn-ip", speedtestcn.data.ip);
updateDom("check-speedtestcn-location", [
speedtestcn.data.city,
speedtestcn.data.province,
speedtestcn.data.country,
speedtestcn.data.isp,
]);
updateDom("check-ipapiis-ip", ipapiis.ip);
updateDom("check-ipapiis-location", [
ipapiis.location.city,
ipapiis.location.state,
ipapiis.location.country,
ipapiis.company.name,
]);
} 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;