ip-checker/ui/script.js

86 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-10-25 12:55:03 +00:00
async function main() {
2024-10-25 16:44:56 +00:00
const [ny4, ipsb, speedtestcn, ipapiis] = await Promise.allSettled([
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/"),
]);
2024-10-25 12:55:03 +00:00
2024-10-25 16:44:56 +00:00
if (ny4.status === "fulfilled") {
updateDom("check-ny4-ip", ny4.value.ip);
2024-10-25 12:55:03 +00:00
updateDom("check-ny4-location", [
2024-10-25 16:44:56 +00:00
ny4.value.city,
ny4.value.region,
ny4.value.country,
ny4.value.organization,
2024-10-25 12:55:03 +00:00
]);
2024-10-25 16:44:56 +00:00
} else {
updateDom("check-ny4-ip", "Fetch Failed");
updateDom("check-ny4-location", "Fetch Failed");
console.log("Fetch failed:", ny4.reason);
}
2024-10-25 12:55:03 +00:00
2024-10-25 16:44:56 +00:00
if (ipsb.status === "fulfilled") {
updateDom("check-ipsb-ip", ipsb.value.ip);
2024-10-25 12:55:03 +00:00
updateDom("check-ipsb-location", [
2024-10-25 16:44:56 +00:00
ipsb.value.city,
ipsb.value.region,
ipsb.value.country,
ipsb.value.isp,
2024-10-25 12:55:03 +00:00
]);
2024-10-25 16:44:56 +00:00
} else {
updateDom("check-ipsb-ip", "Fetch Failed");
updateDom("check-ipsb-location", "Fetch Failed");
console.log("Fetch failed:", ipsb.reason);
}
2024-10-25 12:55:03 +00:00
2024-10-25 16:44:56 +00:00
if (speedtestcn.status === "fulfilled") {
updateDom("check-speedtestcn-ip", speedtestcn.value.data.ip);
2024-10-25 12:55:03 +00:00
updateDom("check-speedtestcn-location", [
2024-10-25 16:44:56 +00:00
speedtestcn.value.data.city,
speedtestcn.value.data.province,
speedtestcn.value.data.country,
speedtestcn.value.data.isp,
2024-10-25 12:55:03 +00:00
]);
2024-10-25 16:44:56 +00:00
} else {
updateDom("check-speedtestcn-ip", "Fetch Failed");
updateDom("check-speedtestcn-location", "Fetch Failed");
console.log("Fetch failed:", speedtestcn.reason);
}
2024-10-25 12:55:03 +00:00
2024-10-25 16:44:56 +00:00
if (ipapiis.status === "fulfilled") {
updateDom("check-ipapiis-ip", ipapiis.value.ip);
2024-10-25 12:55:03 +00:00
updateDom("check-ipapiis-location", [
2024-10-25 16:44:56 +00:00
ipapiis.value.location.city,
ipapiis.value.location.state,
ipapiis.value.location.country,
ipapiis.value.company.name,
2024-10-25 12:55:03 +00:00
]);
2024-10-25 16:44:56 +00:00
} else {
updateDom("check-ipapiis-ip", "Fetch Failed");
updateDom("check-ipapiis-location", "Fetch Failed");
console.log("Fetch failed:", ipapiis.reason);
2024-10-25 12:55:03 +00:00
}
}
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;