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

45 lines
1.1 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:40:25 +03:00
ipElement.textContent = `${ipDetails.ip}, ${ipDetails.asn}`;
2023-08-02 17:30:29 +03:00
const countryElement = document.getElementById("country");
2023-08-02 17:40:25 +03:00
countryElement.textContent = `${ipDetails.countryCode}, ${ipDetails.city}, ${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();