From 14b7d088a089e73f12a39c27e98b99abfe2caf18 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 24 Apr 2024 08:08:57 +0200 Subject: [PATCH] feat: dynamic IP service domain resolution Replaced static IP service URLs with dynamic hostname-based URLs in `getMyIP` function to ensure the IP service domain matches the current website's domain, excluding the 'www' prefix. This change enhances flexibility and adaptability, allowing the IP fetching functionality to work seamlessly across different deployments without needing manual URL updates for each domain. - Removes hard-coded IP service domain. - Utilizes `window.location.hostname` for dynamic domain resolution. - Improves service compatibility across varying deployments. This adjustment addresses potential issues with cross-origin requests and simplifies the deployment process for websites with multiple or changing domains. --- script.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/script.js b/script.js index 9f4af3b..bb0c5d8 100644 --- a/script.js +++ b/script.js @@ -1,5 +1,7 @@ function getMyIP() { - fetch('https://ipv4.myip.coffee') + const domain = window.location.hostname.replace(/^www\./, ''); + + fetch('https://ipv4.' + domain) .then(response => response.text()) .then(ipv4 => document.getElementById('ipv4').textContent = ipv4) .catch(err => { @@ -7,7 +9,7 @@ function getMyIP() { document.getElementById('ipv6').textContent = `Not available!` }); - fetch('https://ipv6.myip.coffee') + fetch('https://ipv6.' + domain) .then(response => response.text()) .then(ipv6 => document.getElementById('ipv6').textContent = ipv6) .catch(err => {