fruworg.github.io/static/js/ip.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-08-02 17:30:29 +03:00
function fetchIPDetails(ip) {
2023-08-02 17:38:30 +03:00
const url = `https://ipapi.co/${ip}/json`;
2023-08-02 17:30:29 +03:00
return fetch(url)
.then((response) => response.json())
.then((data) => {
return {
2023-08-02 17:38:30 +03:00
ip: data.ip,
asn: data.asn,
countryCode: data.country_code,
2023-08-02 17:30:29 +03:00
city: data.city,
2023-08-02 17:38:30 +03:00
org: data.org,
2023-08-02 17:30:29 +03:00
};
})
.catch((error) => {
console.error("Error fetching IP details:", error);
return null;
});
}
function updateDetailsOnPage(ipDetails) {
if (ipDetails) {
const ipElement = document.getElementById("ip");
2023-08-02 17:38:30 +03:00
ipElement.textContent = `IP: ${ipDetails.ip}, ASN: ${ipDetails.asn}`;
2023-08-02 17:30:29 +03:00
const countryElement = document.getElementById("country");
2023-08-02 17:38:30 +03:00
countryElement.textContent = `Country Code: ${ipDetails.countryCode}, City: ${ipDetails.city}, Org: ${ipDetails.org}`;
2023-08-02 17:30:29 +03:00
}
}
function getIPAddress() {
fetch("https://api.ipify.org?format=json")
.then((response) => response.json())
.then((data) => {
const userIP = data.ip;
fetchIPDetails(userIP).then((ipDetails) => {
updateDetailsOnPage(ipDetails);
});
})
.catch((error) => {
console.error("Error fetching IP:", error);
});
}
getIPAddress();