1234567891011121314151617181920212223 |
- window.regions = document.querySelectorAll('link[rel=region]');
- window.regionMap = {};
- window.regions.forEach(function(item) {
- window.regionMap[item.attributes.regionflag.value] = item.attributes.href.value;
- });
- let xhr = new XMLHttpRequest();
- xhr.open("GET", "https://api.ipify.org/?format=json");
- xhr.addEventListener("loadend", async function (e){
- let responseObj = JSON.parse(this.response);
- let ip = responseObj.ip;
- let regionData = await ipToRegion(ip);
- let country = window.location.protocol === 'https:' ? regionData.country_name : regionData.geoplugin_countryName
- if (window.regionMap[country]) {
- window.location.href = window.regionMap[country]
- }
- });
- xhr.send();
- async function ipToRegion(ip) {
- let url = window.location.protocol === 'https:' ? `/ipdata.php?ip=${ip}` : `http://www.geoplugin.net/json.gp?ip=${ip}`
- const response = await fetch(url);
- const data = await response.json();
- return data;
- }
|