IP Lookup Bookmarklet that calls a REST API to fetch geolocation details for a given IP address

I found a neat bookmarklet that calls a REST API to fetch geolocation details for a given IP address. The original source used an external service that serves JSON output over HTTP only. 

As Chrome is more stringent about mixed content, the HTTP call was blocked and the desired output wasn't achieved.

I changed just one line of the bookmarklet code to use the IPAPI lookup service to get it working again.

//IP LOCATION TRACKER BOOKMARKLET
//Original source: https://funbutlearn.com/2016/06/ip-location-tracker-bookmarklet-created.html
//modified API service from http://ip-api.com which doesn't support HTTPS to https://ipapi.co/ to have a secure endpoint & avoid Mixed Content blocking issue
javascript: (function() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
var ipPattern = new RegExp("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
var res = ipPattern.test(text);
if (res) {
showIP(text);
} else {
var ip = prompt("Please provide an IP address");
showIP(ip);
}
function showIP(ipAddr) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var ipDetails = JSON.parse(xhttp.responseText);
var ipAddr = "";
for (key in ipDetails) {
ipAddr += key.toUpperCase() + " : " + ipDetails[key] + "\n";
}
alert(ipAddr);
}
};
xhttp.open("GET", "https://ipapi.co/" + ipAddr + "/json/", true);
xhttp.send();
}
})()
view raw IPLookup.js hosted with ❤ by GitHub
javascript:(function(){var e="";if(window.getSelection){e=window.getSelection().toString()}else{if(document.selection&&document.selection.type!="Control"){e=document.selection.createRange().text}}var a=new RegExp("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");var b=a.test(e);if(b){c(e)}else{var d=prompt("Please provide an IP address");c(d)}function c(f){var g=new XMLHttpRequest();g.onreadystatechange=function(){if(g.readyState==4&&g.status==200){var i=JSON.parse(g.responseText);var h="";for(key in i){h+=key.toUpperCase()+" : "+i[key]+"\n"}alert(h)}};g.open("GET","https://ipapi.co/"+f+"/json/",true);g.send()}})();
view raw iplookup.min.js hosted with ❤ by GitHub
Drag this link to the Bookmarks bar or Favorites bar in Chrome or Edge - IP Lookup

To view geolocation details, selecting an IP address on any web page & then click on the bookmarklet or provide the IP address through prompt box that shows up.

Comments

Popular posts from this blog

The Mercurial Grok AI Assistant Understands & Speaks Indian Languages

Things Near Me – Find & Learn About Landmarks Nearby