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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); | |
} | |
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}})(); |
Drag this link to the Bookmarks bar or Favorites bar in Chrome or Edge - IP Lookup
Comments
Post a Comment