-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo-ip.js
72 lines (70 loc) · 2.81 KB
/
geo-ip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
if (self.fetch) {
const shouldOpen = sessionStorage.getItem("redirect-popup-open") || 'open';
const isAdmin = window.location.href.includes("/admin");
const isRootUrl = window.location.href.includes("myshopify.com");
const isPreview = window.location.href.includes("shopifypreview");
const previewBar = document.getElementById("preview-bar-iframe"); // some previews don't have myshopify.com or shopifypreview on the url, but have the preview bar added by Shopify
if (!isPreview && !isAdmin && !isRootUrl && !previewBar && shouldOpen === 'open') {
fetch("https://geoip.appforge.ca/country/", {
method: "GET",
mode: "cors",
headers: {
"Content-Type": "application/json",
},
})
.then((resp) => resp.json())
.then(function (response) {
let currentSiteCountry = 'CANADA'
if(window.location.href.includes('https://ua.stlthvape.com')){
currentSiteCountry = 'UKRAINE'
}
else if(window.location.href.includes('https://pe.stlthvape.com')){
currentSiteCountry = 'PERU'
}
else if(window.location.href.includes('https://ma.stlthvape.com')){
currentSiteCountry = 'MOROCCO'
}
else if(window.location.href.includes('https://co.stlthvape.com')){
currentSiteCountry = 'COLOMBIA'
}
let urlTo = "https://stlthvape.com";
let countryTo = "CANADA";
if(response.continent_code === 'SA' && response.country_iso_code !== 'BR' && response.country_iso_code !== 'CO'){
urlTo = "https://pe.stlthvape.com";
countryTo = "PERU";
}
else{
switch (response.country_iso_code) {
case "UA":
urlTo = "https://ua.stlthvape.com";
countryTo = "UKRAINE";
break;
case "MA":
urlTo = "https://ma.stlthvape.com";
countryTo = "MOROCCO";
break;
case "CO":
urlTo = "https://co.stlthvape.com";
countryTo = "COLOMBIA";
break;
}
}
const isTheRightOne = window.location.href.includes(urlTo);
if (!isTheRightOne) {
var myDiv = document.createElement("div");
myDiv.innerHTML = '<redirect-popup></redirect-popup>';
document.body.appendChild(myDiv);
document.querySelector('redirect-popup').flag = response.country_iso_code.toLowerCase()
document.querySelector('redirect-popup').countryFrom = currentSiteCountry
document.querySelector('redirect-popup').countryTo = countryTo
document.querySelector('redirect-popup').urlTo = urlTo
document.querySelector('redirect-popup').countryCurrent = response.country_name.toUpperCase()
document.querySelector('redirect-popup').open = shouldOpen === 'open';
}
})
.catch(function (error) {
// handle error
console.log(error);
});
}
}