From f75edbcc48061143e0fa57e071313d3ad636390b Mon Sep 17 00:00:00 2001 From: fruworg Date: Wed, 2 Aug 2023 20:30:29 +0600 Subject: [PATCH] ip --- static/js/ip.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/static/js/ip.js b/static/js/ip.js index 0519ecb..e18ee5e 100644 --- a/static/js/ip.js +++ b/static/js/ip.js @@ -1 +1,47 @@ - \ No newline at end of file + // JavaScript +function fetchIPDetails(ip) { + const url = `http://ip-api.com/json/${ip}`; + + return fetch(url) + .then((response) => response.json()) + .then((data) => { + return { + ip: data.query, + isp: data.as, + countryCode: data.countryCode, + city: data.city, + }; + }) + .catch((error) => { + console.error("Error fetching IP details:", error); + return null; + }); +} + +function updateDetailsOnPage(ipDetails) { + if (ipDetails) { + const ipElement = document.getElementById("ip"); + ipElement.textContent = `IP: ${ipDetails.ip}, ISP: ${ipDetails.isp}`; + + const countryElement = document.getElementById("country"); + countryElement.textContent = `Country Code: ${ipDetails.countryCode}, City: ${ipDetails.city}`; + } +} + +// Функция для получения IP пользователя +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); + }); +} + +// Вызываем функцию для получения IP и данных по IP +getIPAddress(); \ No newline at end of file