Created
July 15, 2019 14:20
-
-
Save D-Andreevich/cef0cadd82022fcaac635f61f7d8decd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| (function (e, i = 0) { | |
| const geoIpServices = [ | |
| 'https://api.2ip.ua/geo.json?ip=', | |
| 'http://api.sypexgeo.net/json', | |
| 'http://ip-api.com/json', | |
| 'http://api.db-ip.com/v2/free/self' | |
| // 'https://ipinfo.io', //jsonp | |
| // 'https://tools.keycdn.com/geo.json?host=', | |
| ]; | |
| const geoIpServicesParams = { | |
| 'https://api.2ip.ua/geo.json?ip=': { | |
| ip: "ip", | |
| country_code: "country_code", | |
| countryName: 'country', | |
| }, | |
| 'http://ip-api.com/json': { | |
| ip: "query", | |
| country_code: "countryCode", | |
| countryName: 'country', | |
| }, | |
| 'http://api.db-ip.com/v2/free/self': { | |
| ip: "ipAddress", | |
| country_code: "countryCode", | |
| countryName: 'countryName', | |
| }, | |
| 'http://api.sypexgeo.net/json': { | |
| ip: "ip", | |
| country_code: "country.iso", | |
| countryName: 'country.name_en', | |
| }, | |
| }; | |
| let setIpAndCountry = (data) => { | |
| console.log('data ', data); | |
| let inputHidden = (name, value) => { | |
| let inputHidden = document.createElement("input"); | |
| inputHidden.type = 'hidden'; | |
| inputHidden.name = name; | |
| inputHidden.value = value; | |
| return inputHidden; | |
| }; | |
| let form = document.querySelector('form.sign-in') || document.querySelector('div#regMain'); | |
| // console.log('form ', form); | |
| if (form && form.hasChildNodes()) { | |
| let divBlock = document.createElement('div'); | |
| divBlock.setAttribute('class', 'input-block'); | |
| divBlock.appendChild(inputHidden('ip', data.ip)); | |
| divBlock.appendChild(inputHidden('country_code', data.country_code)); | |
| form.appendChild(divBlock); | |
| } | |
| }; | |
| let getValueByPath = (arrayOrObj, keysString) => { | |
| let result; | |
| for (let keyIn of keysString.split('.')) { | |
| if (result) { | |
| if (result[keyIn] != undefined) { | |
| result = result[keyIn]; | |
| } else { | |
| return result[keyIn]; | |
| } | |
| } else { | |
| result = arrayOrObj[keyIn]; | |
| } | |
| } | |
| return result | |
| }; | |
| let getIpAndCountry = (serviceUrl) => { | |
| fetch(serviceUrl) | |
| .then(response => { | |
| // console.log('response ', response); | |
| return response.json(); | |
| }) | |
| .then(function (data) { | |
| // Alert.error(data); | |
| let result = {}; | |
| let params = geoIpServicesParams[geoIpServices[i]]; | |
| for (let key in params) { | |
| if (params[key].split('.').length > 1) { | |
| result[key] = getValueByPath(data, params[key]); | |
| } else { | |
| result[key] = data[params[key]]; | |
| } | |
| } | |
| setIpAndCountry(result); | |
| }) | |
| .catch(error => { | |
| // Alert.error(error, 'error service ', geoIpServices[i]); | |
| if (i < geoIpServices.length - 1) { | |
| i++; | |
| getIpAndCountry(geoIpServices[i]); | |
| } | |
| }); | |
| }; | |
| getIpAndCountry(geoIpServices[i]); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment