/////////////////////////////////////////////////////////////////////////////// /* qdlogstats_geolocate_geoipdb.js https://geolocation-db.com/ This is a free service and does not require registration. This module caches geo data and so only accesses the service once for each unique host begin resolved. This service only accepts IP adresses. COPYRIGHT --------- Copyright (C) 2018-2021 Mark G.Daniel This program, comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under the conditions of the GNU GENERAL PUBLIC LICENSE, version 3, or any later version. http://www.gnu.org/licenses/gpl.txt VERSION ------- 05-MAY-2021 MGD moves https://geoip-db.com/ to https://geolocation-db.com/ (leave as the original JavaScript file name though) make xhr.open() asynchronous 06-NOV-2018 MGD initial */ /////////////////////////////////////////////////////////////////////////////// var geoLocateAddr = []; var geoLocateData = []; var geoLocateWait = []; var geoLocateURL = 'https://geolocation-db.com/jsonp/'; /////////////////////////////////////////////////////////////////////////////// function geoLocate (id, addr) { var idx; for (idx = 0; idx < geoLocateAddr.length; idx++) if (geoLocateAddr[idx] == addr) break; if (!geoLocateData[idx]) { geoLocateAddr[idx] = addr; geoLocateData[idx] = ''; if (!geoLocateWait[idx]) { geoLocateGeoIpDb(id,idx,addr); geoLocateWait[idx] = ''; } else geoLocateWait[idx] += '|'; geoLocateWait[idx] += id; return; } document.getElementById(id).innerHTML = geoLocateData[idx]; } /////////////////////////////////////////////////////////////////////////////// function geoLocateGeoIpDb(id,idx,addr) { var url = geoLocateURL + addr; var xhr = new XMLHttpRequest(); // add the address as a private attribute xhr._addr = addr; xhr.open('GET', url, true); xhr.onreadystatechange = function() { if (this.readyState == 4) { var gld; if (this.status == 200) { var jsnp = this.responseText; var jsn = JSON.parse(jsnp.substr(9,jsnp.length-10)); var country = jsn['country_name']; var state = jsn['state']; var city = jsn['city']; gld = '{'; if (country != null) { if (country != "") { gld += country; if (state && state != "" && state != country) gld += ', ' + jsn['state']; if (city && city != "" && city != state) gld += ', ' + jsn['city']; } else gld += '?'; } else gld += 'bugcheck'; gld += '}'; } else if (this.status) { gld = '{HTTP ' + this.status + ' error}'; } else { console.log(this); gld = '{see console}'; } // populate those waiting for asynchronous completion for (var idx1 = 0; idx1 < geoLocateAddr.length; idx1++) { if (!geoLocateWait[idx1]) continue; if (this._addr != geoLocateAddr[idx1]) continue; geoLocateData[idx1] = gld; var wait = geoLocateWait[idx1].split('|'); for (var idx2 = 0; idx2 < wait.length; idx2++) document.getElementById(wait[idx2]).innerHTML = gld; geoLocateWait[idx1] = null; } } }; xhr.send(); } ///////////////////////////////////////////////////////////////////////////////