1- var _ipgeolocation = function ( ) {
2- var useSessionStorage = false ;
3- var asyncCall = true ;
4- var hostname = false ;
5- var liveHostname = false ;
6- var hostnameFallbackLive = false ;
7- var security = false ;
8- var userAgent = false ;
9- var ipAddress = "" ;
10- var excludes = "" ;
11- var fields = "" ;
12- var lang = "en" ;
13- var tz = "" ;
14- var latitude = "" ;
15- var longitude = "" ;
16- var location = "" ;
17- var geolocationEndpoint = "ipgeo" ;
18- var timezoneEndpoint = "timezone" ;
19- var useragentEndpoint = "user-agent" ;
20- var geolocationResponseName = "_ipgeolocation_geolocation" ;
21- var timezoneResponseName = "_ipgeolocation_timezone" ;
22- var useragentResponseName = "_ipgeolocation_useragent" ;
23- var ipGeolocationServerStatusName = "_ipgeolocation_server_status" ;
24-
25- function request ( subUrl , callback , apiKey = "" ) {
1+ const _ipgeolocation = function ( ) {
2+ let useSessionStorage = false ;
3+ let asyncCall = true ;
4+ let hostname = false ;
5+ let liveHostname = false ;
6+ let hostnameFallbackLive = false ;
7+ let security = false ;
8+ let userAgent = false ;
9+ let ipAddress = "" ;
10+ let excludes = "" ;
11+ let fields = "" ;
12+ let lang = "en" ;
13+ let tz = "" ;
14+ let latitude = "" ;
15+ let longitude = "" ;
16+ let location = "" ;
17+ const geolocationEndpoint = "ipgeo" ;
18+ const timezoneEndpoint = "timezone" ;
19+ const useragentEndpoint = "user-agent" ;
20+ const geolocationResponseName = "_ipgeolocation_geolocation" ;
21+ const timezoneResponseName = "_ipgeolocation_timezone" ;
22+ const useragentResponseName = "_ipgeolocation_useragent" ;
23+ const ipGeolocationServerStatusName = "_ipgeolocation_server_status" ;
24+
25+ async function request ( subUrl , callback , apiKey = "" ) {
2626 if ( useSessionStorage ) {
27- if ( subUrl == geolocationEndpoint && sessionStorage . getItem ( geolocationResponseName ) && callback ) {
27+ if ( subUrl === geolocationEndpoint && sessionStorage . getItem ( geolocationResponseName ) && callback ) {
2828 callback ( JSON . parse ( sessionStorage . getItem ( geolocationResponseName ) ) ) ;
2929 return ;
30- } else if ( subUrl == timezoneEndpoint && sessionStorage . getItem ( timezoneResponseName ) && callback ) {
30+ } else if ( subUrl === timezoneEndpoint && sessionStorage . getItem ( timezoneResponseName ) && callback ) {
3131 callback ( JSON . parse ( sessionStorage . getItem ( timezoneResponseName ) ) ) ;
3232 return ;
33- } else if ( subUrl == useragentEndpoint && sessionStorage . getItem ( useragentResponseName ) && callback ) {
33+ } else if ( subUrl === useragentEndpoint && sessionStorage . getItem ( useragentResponseName ) && callback ) {
3434 callback ( JSON . parse ( sessionStorage . getItem ( useragentResponseName ) ) ) ;
3535 return ;
3636 }
3737 }
3838
39- var urlParameters = "" ;
39+ let urlParameters = "" ;
4040
4141 if ( ! subUrl ) {
42- callback ( JSON . parse ( "{'status': 401, message: 'Given path to IP Geolocation API is not valid'}" ) ) ;
42+ callback ( JSON . parse ( "{'status': 401, ' message' : 'Given path to IP Geolocation API is not valid'}" ) ) ;
4343 return ;
4444 }
4545
@@ -60,8 +60,8 @@ var _ipgeolocation = function() {
6060 }
6161
6262 if ( hostname || security || userAgent ) {
63- var parameterValue = "" ;
64- var hostnameSelected = false ;
63+ let parameterValue = "" ;
64+ let hostnameSelected = false ;
6565
6666 if ( hostname ) {
6767 parameterValue = "hostname" ;
@@ -106,61 +106,64 @@ var _ipgeolocation = function() {
106106 urlParameters = addUrlParameter ( urlParameters , "long" , longitude ) ;
107107 }
108108
109- if ( location ) {
109+ if ( location ) {
110110 urlParameters = addUrlParameter ( urlParameters , "location" , location ) ;
111111 }
112-
113- var httpRequest ;
114-
115- if ( window . XMLHttpRequest ) {
116- httpRequest = new XMLHttpRequest ( ) ;
117- } else if ( window . ActiveXObject ) {
118- httpRequest = new ActiveXObject ( "Microsoft.XMLHTTP" ) ;
119- }
120-
112+
121113 try {
122- if ( ! sessionStorage . getItem ( ipGeolocationServerStatusName ) ) {
123- var httpRequestForStatus ;
124-
125- if ( window . XMLHttpRequest ) {
126- httpRequestForStatus = new XMLHttpRequest ( ) ;
127- } else if ( window . ActiveXObject ) {
128- httpRequestForStatus = new ActiveXObject ( "Microsoft.XMLHTTP" ) ;
129- }
130-
131- httpRequestForStatus . onreadystatechange = function ( ) {
132- if ( this . readyState === 4 && this . status === 200 ) {
133- sessionStorage . setItem ( ipGeolocationServerStatusName , true ) ;
114+ if ( ! sessionStorage . getItem ( ipGeolocationServerStatusName ) ) {
115+ fetch ( "https://us-central1-ipgeolocation-414906.cloudfunctions.net/task" , {
116+ method : "GET" ,
117+ redirect : 'follow' ,
118+ headers : {
119+ "Accept" : "application/json"
134120 }
135- } ;
136- httpRequestForStatus . open ( "GET" , "https://us-central1-ipgeolocation-414906.cloudfunctions.net/task" , true ) ;
137- httpRequestForStatus . send ( ) ;
138-
121+ } )
122+ . then ( response => {
123+ if ( response . status === 200 ) {
124+ sessionStorage . setItem ( ipGeolocationServerStatusName , true )
125+ }
126+ } )
127+ . catch ( error => { } ) ;
139128 }
140- } catch ( error ) {
141- }
129+ } catch ( error ) { }
142130
143- httpRequest . onreadystatechange = function ( ) {
144- if ( this . readyState == 4 ) {
145- if ( useSessionStorage && this . status == 200 ) {
146- key = geolocationResponseName ;
131+ const url = "https://api.ipgeolocation.io/" . concat ( subUrl , urlParameters , "" ) ;
132+ const requestOptions = {
133+ method : "GET" ,
134+ headers : {
135+ "Accept" : "application/json"
136+ }
137+ } ;
147138
148- if ( subUrl == timezoneEndpoint ) {
149- key = timezoneResponseName ;
139+ try {
140+ if ( asyncCall ) {
141+ fetch ( url , requestOptions )
142+ . then ( ( response ) => {
143+ return response . json ( ) ;
144+ } )
145+ . then ( ( json ) => {
146+ if ( useSessionStorage && ! json . message ) {
147+ cacheInSessionStorage ( subUrl , json ) ;
150148 }
151149
152- sessionStorage . setItem ( key , this . responseText ) ;
150+ callback ( json ) ;
151+ } ) ;
152+ } else {
153+ const response = await fetch ( url , requestOptions ) ;
154+ const json = await response . json ( ) ;
155+
156+ if ( response . ok && useSessionStorage ) {
157+ cacheInSessionStorage ( subUrl , json ) ;
153158 }
154159
155- if ( callback ) {
156- callback ( JSON . parse ( this . responseText ) ) ;
157- }
160+ callback ( json ) ;
158161 }
159- } ;
160-
161- httpRequest . open ( "GET" , "https://api.ipgeolocation.io/" . concat ( subUrl , urlParameters , "" ) , asyncCall ) ;
162- httpRequest . setRequestHeader ( "Accept" , "application/json" ) ;
163- httpRequest . send ( ) ;
162+ } catch ( error ) {
163+ console . error ( error ) ;
164+
165+ callback ( JSON . parse ( "{'status': 400, 'message': 'Something went wrong while querying ipgeolocation.io API. If the error persists, contact us at support@ipgeolocation.io'}" ) ) ;
166+ }
164167 }
165168
166169 function addUrlParameter ( parameters , parameterName , parameterValue ) {
@@ -173,6 +176,18 @@ var _ipgeolocation = function() {
173176 return parameters ;
174177 }
175178
179+ function cacheInSessionStorage ( endpoint , json ) {
180+ let key = geolocationResponseName ;
181+
182+ if ( endpoint === timezoneEndpoint ) {
183+ key = timezoneResponseName ;
184+ } else if ( endpoint === useragentEndpoint ) {
185+ key = useragentResponseName ;
186+ }
187+
188+ sessionStorage . setItem ( key , JSON . stringify ( json ) ) ;
189+ }
190+
176191 return {
177192 enableSessionStorage : function ( e = false ) {
178193 useSessionStorage = e ;
0 commit comments