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 = "" ) {
26
26
if ( useSessionStorage ) {
27
- if ( subUrl == geolocationEndpoint && sessionStorage . getItem ( geolocationResponseName ) && callback ) {
27
+ if ( subUrl === geolocationEndpoint && sessionStorage . getItem ( geolocationResponseName ) && callback ) {
28
28
callback ( JSON . parse ( sessionStorage . getItem ( geolocationResponseName ) ) ) ;
29
29
return ;
30
- } else if ( subUrl == timezoneEndpoint && sessionStorage . getItem ( timezoneResponseName ) && callback ) {
30
+ } else if ( subUrl === timezoneEndpoint && sessionStorage . getItem ( timezoneResponseName ) && callback ) {
31
31
callback ( JSON . parse ( sessionStorage . getItem ( timezoneResponseName ) ) ) ;
32
32
return ;
33
- } else if ( subUrl == useragentEndpoint && sessionStorage . getItem ( useragentResponseName ) && callback ) {
33
+ } else if ( subUrl === useragentEndpoint && sessionStorage . getItem ( useragentResponseName ) && callback ) {
34
34
callback ( JSON . parse ( sessionStorage . getItem ( useragentResponseName ) ) ) ;
35
35
return ;
36
36
}
37
37
}
38
38
39
- var urlParameters = "" ;
39
+ let urlParameters = "" ;
40
40
41
41
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'}" ) ) ;
43
43
return ;
44
44
}
45
45
@@ -60,8 +60,8 @@ var _ipgeolocation = function() {
60
60
}
61
61
62
62
if ( hostname || security || userAgent ) {
63
- var parameterValue = "" ;
64
- var hostnameSelected = false ;
63
+ let parameterValue = "" ;
64
+ let hostnameSelected = false ;
65
65
66
66
if ( hostname ) {
67
67
parameterValue = "hostname" ;
@@ -106,61 +106,64 @@ var _ipgeolocation = function() {
106
106
urlParameters = addUrlParameter ( urlParameters , "long" , longitude ) ;
107
107
}
108
108
109
- if ( location ) {
109
+ if ( location ) {
110
110
urlParameters = addUrlParameter ( urlParameters , "location" , location ) ;
111
111
}
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
+
121
113
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"
134
120
}
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 => { } ) ;
139
128
}
140
- } catch ( error ) {
141
- }
129
+ } catch ( error ) { }
142
130
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
+ } ;
147
138
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 ) ;
150
148
}
151
149
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 ) ;
153
158
}
154
159
155
- if ( callback ) {
156
- callback ( JSON . parse ( this . responseText ) ) ;
157
- }
160
+ callback ( json ) ;
158
161
}
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
+ }
164
167
}
165
168
166
169
function addUrlParameter ( parameters , parameterName , parameterValue ) {
@@ -173,6 +176,18 @@ var _ipgeolocation = function() {
173
176
return parameters ;
174
177
}
175
178
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
+
176
191
return {
177
192
enableSessionStorage : function ( e = false ) {
178
193
useSessionStorage = e ;
0 commit comments