You could do a nslookup on location.hostname
!
Notes in the comments
Scroll down till One perfect snippet if you want something to copy paste without modifications.
Method 1 (use this, it is better)
Use DoHjs!
Add this to the HTML:
<script src="https://cdn.jsdelivr.net/npm/dohjs@latest/dist/doh.min.js"></script>
and then in JS:
const resolver = new doh.DohResolver('https://1.1.1.1/dns-query');
resolver.query(location.hostname, 'A')
.then(response => {
response.answers.forEach(ans => console.log(ans.data));
})
.catch(err => console.error(err));
See DoHjs for more information on how this works.
Method 2 (now broken)
Use the enclout API:
const oReq = new XMLHttpRequest();
oReq.onload = function () {
const response = JSON.parse(this.responseText);
alert(JSON.stringify(response.dns_entries));
}
oReq.open("get", "https://www.enclout.com/api/v1/dns/show.json?auth_token=rN4oqCyJz9v2RRNnQqkx&url=" + encodeURIComponent(location.hostname), true);
oReq.send();
If you do not trust the snippet, how it works is described in the comments and earlier in this answer.
This is method 1 + comments
One perfect snippet (if you just want to copy paste)
function getIP() {
return new Promise((resolve, reject) => {
const resolver = new doh.DohResolver("https://1.1.1.1/dns-query");
resolver
.query(location.hostname, "A")
.then((response) => {
response.answers.forEach((ans) => {
resolve(ans.data)
});
if (response.answers.length == 0) {
resolve(location.hostname)
}
})
.catch((err) => reject(err));
});
}
getIP().then((res) => {
document.getElementById("ip").innerText = res;
});
<script src="https://cdn.jsdelivr.net/npm/dohjs@latest/dist/doh.min.js"></script>
<p>The IP this is running on will be displayed below.</p>
<p id="ip"></p>