ip-geolocation-api-jquery-sdk
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -1,33 +0,33 @@ | ||
var _ipgeolocation = function() { | ||
var useSessionStorage = false; | ||
var asyncCall = true; | ||
var hostname = false; | ||
var liveHostname = false; | ||
var hostnameFallbackLive = false; | ||
var security = false; | ||
var userAgent = false; | ||
var ipAddress = ""; | ||
var excludes = ""; | ||
var fields = ""; | ||
var lang = "en"; | ||
var tz = ""; | ||
var latitude = ""; | ||
var longitude = ""; | ||
var location = ""; | ||
var geolocationEndpoint = "ipgeo"; | ||
var timezoneEndpoint = "timezone"; | ||
var useragentEndpoint = "user-agent"; | ||
var geolocationResponseName = "_ipgeolocation_geolocation"; | ||
var timezoneResponseName = "_ipgeolocation_timezone"; | ||
var useragentResponseName = "_ipgeolocation_useragent"; | ||
const _ipgeolocation = function() { | ||
let useSessionStorage = false; | ||
let asyncCall = true; | ||
let hostname = false; | ||
let liveHostname = false; | ||
let hostnameFallbackLive = false; | ||
let security = false; | ||
let userAgent = false; | ||
let ipAddress = ""; | ||
let excludes = ""; | ||
let fields = ""; | ||
let lang = "en"; | ||
let tz = ""; | ||
let latitude = ""; | ||
let longitude = ""; | ||
let location = ""; | ||
const geolocationEndpoint = "ipgeo"; | ||
const timezoneEndpoint = "timezone"; | ||
const useragentEndpoint = "user-agent"; | ||
const geolocationResponseName = "_ipgeolocation_geolocation"; | ||
const timezoneResponseName = "_ipgeolocation_timezone"; | ||
const useragentResponseName = "_ipgeolocation_useragent"; | ||
function request(subUrl, callback, apiKey = "") { | ||
async function request(subUrl, callback, apiKey = "") { | ||
if (useSessionStorage) { | ||
if (subUrl == geolocationEndpoint && sessionStorage.getItem(geolocationResponseName) && callback) { | ||
if (subUrl === geolocationEndpoint && sessionStorage.getItem(geolocationResponseName) && callback) { | ||
callback(JSON.parse(sessionStorage.getItem(geolocationResponseName))); | ||
return; | ||
} else if (subUrl == timezoneEndpoint && sessionStorage.getItem(timezoneResponseName) && callback) { | ||
} else if (subUrl === timezoneEndpoint && sessionStorage.getItem(timezoneResponseName) && callback) { | ||
callback(JSON.parse(sessionStorage.getItem(timezoneResponseName))); | ||
return; | ||
} else if (subUrl == useragentEndpoint && sessionStorage.getItem(useragentResponseName) && callback) { | ||
} else if (subUrl === useragentEndpoint && sessionStorage.getItem(useragentResponseName) && callback) { | ||
callback(JSON.parse(sessionStorage.getItem(useragentResponseName))); | ||
@@ -38,6 +38,6 @@ return; | ||
var urlParameters = ""; | ||
let urlParameters = ""; | ||
if (!subUrl) { | ||
callback(JSON.parse("{'status': 401, message: 'Given path to IP Geolocation API is not valid'}")); | ||
callback(JSON.parse("{'status': 401, 'message': 'Given path to IP Geolocation API is not valid'}")); | ||
return; | ||
@@ -63,4 +63,4 @@ } | ||
if (hostname || security || userAgent) { | ||
var parameterValue = ""; | ||
var hostnameSelected = false; | ||
let parameterValue = ""; | ||
let hostnameSelected = false; | ||
@@ -110,35 +110,42 @@ if (hostname) { | ||
if (location){ | ||
if (location) { | ||
urlParameters = addUrlParameter(urlParameters, "location", location); | ||
} | ||
var httpRequest; | ||
const url = "https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""); | ||
const requestOptions = { | ||
method: "GET", | ||
headers: { | ||
"Accept": "application/json" | ||
} | ||
}; | ||
if (window.XMLHttpRequest) { | ||
httpRequest = new XMLHttpRequest(); | ||
} else if (window.ActiveXObject) { | ||
httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); | ||
} | ||
try { | ||
if (asyncCall) { | ||
fetch(url, requestOptions) | ||
.then((response) => { | ||
return response.json(); | ||
}) | ||
.then((json) => { | ||
if (useSessionStorage && !json.message) { | ||
cacheInSessionStorage(subUrl, json); | ||
} | ||
httpRequest.onreadystatechange = function() { | ||
if (this.readyState == 4) { | ||
if (useSessionStorage && this.status == 200) { | ||
key = geolocationResponseName; | ||
callback(json); | ||
}); | ||
} else { | ||
const response = await fetch(url, requestOptions); | ||
const json = await response.json(); | ||
if (subUrl == timezoneEndpoint) { | ||
key = timezoneResponseName; | ||
} | ||
sessionStorage.setItem(key, this.responseText); | ||
if (response.ok && useSessionStorage) { | ||
cacheInSessionStorage(subUrl, json); | ||
} | ||
if (callback) { | ||
callback(JSON.parse(this.responseText)); | ||
} | ||
callback(json); | ||
} | ||
}; | ||
httpRequest.open("GET", "https://api.ipgeolocation.io/".concat(subUrl, urlParameters, ""), asyncCall); | ||
httpRequest.setRequestHeader("Accept", "application/json"); | ||
httpRequest.send(); | ||
} catch (error) { | ||
console.error(error); | ||
callback(JSON.parse("{'status': 400, 'message': 'Something went wrong while querying ipgeolocation.io API. If the error persists, contact us at support@ipgeolocation.io'}")); | ||
} | ||
} | ||
@@ -156,2 +163,14 @@ | ||
function cacheInSessionStorage(endpoint, json) { | ||
let key = geolocationResponseName; | ||
if (endpoint === timezoneEndpoint) { | ||
key = timezoneResponseName; | ||
} else if (endpoint === useragentEndpoint) { | ||
key = useragentResponseName; | ||
} | ||
sessionStorage.setItem(key, JSON.stringify(json)); | ||
} | ||
return { | ||
@@ -158,0 +177,0 @@ enableSessionStorage: function(e = false) { |
@@ -25,3 +25,3 @@ { | ||
}, | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"repository": { | ||
@@ -28,0 +28,0 @@ "type": "git", |
@@ -39,3 +39,3 @@ # IP Geolocation API JQuery SDK | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.2/ipgeolocation.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.3/ipgeolocation.min.js"></script> | ||
``` | ||
@@ -159,3 +159,3 @@ | ||
```javascript | ||
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.1/ipgeolocation.min.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/ip-geolocation-api-jquery-sdk@1.1.3/ipgeolocation.min.js"></script> | ||
@@ -166,5 +166,5 @@ <script> | ||
var ip = sessionStorage.getItem("ip"); | ||
var country_name = sessionStorage.getItem("country_name"); | ||
var country_code2 = sessionStorage.getItem("country_code2"); | ||
let ip = sessionStorage.getItem("ip"); | ||
let country_name = sessionStorage.getItem("country_name"); | ||
let country_code2 = sessionStorage.getItem("country_code2"); | ||
@@ -171,0 +171,0 @@ if (!ip || !country_name || !country_code2) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16882
197
2