chore(ui): refactor js again

This commit is contained in:
Guanran Wang 2024-10-27 12:28:54 +08:00
parent 503aedbf37
commit ebbd49cce5
Signed by: nyancat
GPG key ID: 91F97D9ED12639CF

View file

@ -1,85 +1,76 @@
async function main() { async function main() {
const [ny4, ipsb, speedtestcn, ipapiis] = await Promise.allSettled([ fetch("https://ip.ny4.dev/api/v1/ip")
fetchAsync("https://ip.ny4.dev/api/v1/ip"), .then((response) => {
fetchAsync("https://api.ip.sb/geoip"), if (!response.ok) {
fetchAsync("https://api-v3.speedtest.cn/ip"), throw new Error(`HTTP error! Status: ${response.status}`);
fetchAsync("https://api.ipapi.is/"), }
]); return response.json();
})
.then((data) => {
document.getElementById("check-ny4-ip")!.innerHTML = data.ip;
document.getElementById("check-ny4-location")!.innerHTML = [
data.city,
data.region,
data.country,
data.organization,
].join(", ");
})
.catch((error) => console.log("Error fetching IP SB data:", error));
if (ny4.status === "fulfilled") { fetch("https://api.ip.sb/geoip")
updateDom("check-ny4-ip", ny4.value.ip); .then((response) => {
updateDom("check-ny4-location", [ if (!response.ok) {
ny4.value.city, throw new Error(`HTTP error! Status: ${response.status}`);
ny4.value.region, }
ny4.value.country, return response.json();
ny4.value.organization, })
]); .then((data) => {
} else { document.getElementById("check-ipsb-ip")!.innerHTML = data.ip;
updateDom("check-ny4-ip", "Fetch Failed"); document.getElementById("check-ipsb-location")!.innerHTML = [
updateDom("check-ny4-location", "Fetch Failed"); data.city,
console.log("Fetch failed:", ny4.reason); data.region,
} data.country,
data.isp,
].join(", ");
})
.catch((error) => console.log("Error fetching IP SB data:", error));
if (ipsb.status === "fulfilled") { fetch("https://api.ipapi.is/")
updateDom("check-ipsb-ip", ipsb.value.ip); .then((response) => {
updateDom("check-ipsb-location", [ if (!response.ok) {
ipsb.value.city, throw new Error(`HTTP error! Status: ${response.status}`);
ipsb.value.region, }
ipsb.value.country, return response.json();
ipsb.value.isp, })
]); .then((data) => {
} else { document.getElementById("check-ipapiis-ip")!.innerHTML = data.ip;
updateDom("check-ipsb-ip", "Fetch Failed"); document.getElementById("check-ipapiis-location")!.innerHTML = [
updateDom("check-ipsb-location", "Fetch Failed"); data.location.city,
console.log("Fetch failed:", ipsb.reason); data.location.state,
} data.location.country,
data.company.name,
].join(", ");
})
.catch((error) => console.log("Error fetching IP API IS data:", error));
if (speedtestcn.status === "fulfilled") { fetch("https://api-v3.speedtest.cn/ip")
updateDom("check-speedtestcn-ip", speedtestcn.value.data.ip); .then((response) => {
updateDom("check-speedtestcn-location", [ if (!response.ok) {
speedtestcn.value.data.city, throw new Error(`HTTP error! Status: ${response.status}`);
speedtestcn.value.data.province, }
speedtestcn.value.data.country, return response.json();
speedtestcn.value.data.isp, })
]); .then((data) => {
} else { const v = data.data;
updateDom("check-speedtestcn-ip", "Fetch Failed"); document.getElementById("check-speedtestcn-ip")!.innerHTML = v.ip;
updateDom("check-speedtestcn-location", "Fetch Failed"); document.getElementById("check-speedtestcn-location")!.innerHTML = [
console.log("Fetch failed:", speedtestcn.reason); v.city,
} v.province,
v.country,
if (ipapiis.status === "fulfilled") { v.isp,
updateDom("check-ipapiis-ip", ipapiis.value.ip); ].join(", ");
updateDom("check-ipapiis-location", [ })
ipapiis.value.location.city, .catch((error) => console.log("Error fetching Speedtest CN data:", error));
ipapiis.value.location.state,
ipapiis.value.location.country,
ipapiis.value.company.name,
]);
} else {
updateDom("check-ipapiis-ip", "Fetch Failed");
updateDom("check-ipapiis-location", "Fetch Failed");
console.log("Fetch failed:", ipapiis.reason);
}
}
async function fetchAsync(url: string) {
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: string, content: string | Array<string>) {
document.getElementById(elementId)!.innerHTML = Array.isArray(content)
? content.join(", ")
: content;
} }
window.onload = main; window.onload = main;