Last active
November 29, 2017 18:06
-
-
Save franzenzenhofer/f6cb4ff73016e8a25a11cf049f4d0a31 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
; | |
window.shop_links_changed = false; | |
let doTheChange = (cc) => | |
{ | |
let o = 'https://shop.veganblatt.com'; | |
let ccs =['AT','DE','CH']; | |
if(ccs.includes(cc)) | |
{ | |
for(let c of ccs) | |
{ | |
if(c!==cc) | |
{ | |
let s = o +'/de-'+c; | |
let r = o +'/de-'+cc; | |
let elems = document.querySelectorAll('a[href^="'+s+'"]'); | |
for(let e of elems) | |
{ | |
e.href = e.href.replace(s,r); | |
} | |
} | |
} | |
let r = o +'/de-'+cc; | |
let only_domain = document.querySelectorAll('a[href="'+o+'"]'); | |
for (let e of only_domain) | |
{ | |
e.href = e.href.replace(o,r); | |
} | |
let only_domain_slash = document.querySelectorAll('a[href="'+o+'/"]'); | |
for (let e of only_domain_slash) | |
{ | |
e.href = e.href.replace(o+"/",r); | |
} | |
} | |
removeEventListeners(); | |
return; | |
} | |
let changeShopLinks = () => | |
{ | |
if(window.shop_links_changed===true){return;} | |
window.shop_links_changed = true; | |
let session_cc = window.sessionStorage.getItem('cc'); | |
if(session_cc) | |
{ | |
doTheChange(session_cc); | |
return; | |
} | |
fetch('https://freegeoip.net/json/') | |
.then( | |
function(response) { | |
if (response.status !== 200) { | |
return; | |
} | |
response.json().then(function(data) { | |
let cc = data.country_code; | |
if(cc){window.sessionStorage.setItem('cc',cc);} | |
doTheChange(cc); | |
return; | |
}); | |
} | |
) | |
.catch(function(err) { | |
console.log('Fetch Error :-S', err); | |
removeEventListeners(); | |
}); | |
} | |
["mousemove","touchdown","scrollstart", "focus", "mousedown"].forEach(function(e){ | |
window.addEventListener(e,changeShopLinks,{once:true, passive:true}); | |
}); | |
var removeEventListeners = function(){ | |
["mousemove","touchdown","scrollstart", "focus", "mousedown"].forEach(function(e){ | |
console.log(window.removeEventListener(e,changeShopLinks)); | |
}); | |
} | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
done, save cc in sessionstorage