myip.coffee/script.js
Kumi 14b7d088a0
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.
2024-04-24 08:08:57 +02:00

23 lines
723 B
JavaScript

function getMyIP() {
const domain = window.location.hostname.replace(/^www\./, '');
fetch('https://ipv4.' + domain)
.then(response => response.text())
.then(ipv4 => document.getElementById('ipv4').textContent = ipv4)
.catch(err => {
console.log(err);
document.getElementById('ipv6').textContent = `Not available!`
});
fetch('https://ipv6.' + domain)
.then(response => response.text())
.then(ipv6 => document.getElementById('ipv6').textContent = ipv6)
.catch(err => {
console.log(err);
document.getElementById('ipv6').textContent = `Not available!`
});
}
document.addEventListener('DOMContentLoaded', () => {
getMyIP();
});