forked from ToX82/cookie-bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookiebar-latest.min.js
1 lines (1 loc) · 7.48 KB
/
cookiebar-latest.min.js
1
var CookieLanguages=['ca','cs','da','de','en','es','fr','hu','it','nl','pl','pt','ro','ru','se','sk','sl'];var cookieLawStates=['AT','BE','BG','CY','CZ','DE','DK','EE','EL','ES','FI','FR','GB','HR','HU','IE','IT','LT','LU','LV','MT','NL','PL','PT','RO','SE','SI','SK'];function setupCookieBar(){var scriptPath=getScriptPath();var cookieBar;var button;var buttonNo;var prompt;var promptBtn;var promptClose;var promptContent;var promptNoConsent;var startup=false;var shutup=false;var currentCookieSelection=getCookie();if(currentCookieSelection=='CookieDisallowed'){removeCookies();setCookie('cookiebar','CookieDisallowed')}if(currentCookieSelection!==undefined){return}if(getURLParameter('noGeoIp')){startup=true;initCookieBar()}else{var checkEurope=new XMLHttpRequest();checkEurope.open('GET','https://freegeoip.app/json/',true);checkEurope.onreadystatechange=function(){if(checkEurope.readyState!==4){return}clearTimeout(xmlHttpTimeout);if(checkEurope.status===200){var country=JSON.parse(checkEurope.responseText).country_code;if(cookieLawStates.indexOf(country)>-1){startup=true}else{shutup=true;setCookie('cookiebar','CookieAllowed');if(getURLParameter('refreshPage')){window.location.reload();}}}else{startup=true}initCookieBar()};var xmlHttpTimeout=setTimeout(function(){console.log('cookieBAR - Timeout for ip geolocation');checkEurope.onreadystatechange=function(){};checkEurope.abort();startup=true;initCookieBar()},1500);checkEurope.send()}function initCookieBar(){if(document.cookie.length>0||window.localStorage.length>0){var accepted=getCookie();if(accepted===undefined){startup=true}else{shutup=true}}else{startup=false}if(getURLParameter('always')){startup=true}if(startup===true&&shutup===false){startCookieBar()}}function startCookieBar(){var userLang=detectLang();var theme='';if(getURLParameter('theme')){theme='-'+getURLParameter('theme')}var path=scriptPath.replace(/[^\/]*$/,'');var minified=(scriptPath.indexOf('.min')>-1)?'.min':'';var stylesheet=document.createElement('link');stylesheet.setAttribute('rel','stylesheet');stylesheet.setAttribute('href',path+'themes/cookiebar'+theme+minified+'.css');document.head.appendChild(stylesheet);var request=new XMLHttpRequest();request.open('GET',path+'lang/'+userLang+'.html',true);request.onreadystatechange=function(){if(request.readyState===4&&request.status===200){var element=document.createElement('div');element.innerHTML=request.responseText;document.getElementsByTagName('body')[0].appendChild(element);cookieBar=document.getElementById('cookie-bar');button=document.getElementById('cookie-bar-button');buttonNo=document.getElementById('cookie-bar-button-no');prompt=document.getElementById('cookie-bar-prompt');promptBtn=document.getElementById('cookie-bar-prompt-button');promptClose=document.getElementById('cookie-bar-prompt-close');promptContent=document.getElementById('cookie-bar-prompt-content');promptNoConsent=document.getElementById('cookie-bar-no-consent');thirdparty=document.getElementById('cookie-bar-thirdparty');tracking=document.getElementById('cookie-bar-tracking');scrolling=document.getElementById('cookie-bar-scrolling');privacyPage=document.getElementById('cookie-bar-privacy-page');privacyLink=document.getElementById('cookie-bar-privacy-link');if(!getURLParameter('showNoConsent')){promptNoConsent.style.display='none';buttonNo.style.display='none'}if(getURLParameter('blocking')){fadeIn(prompt,500);promptClose.style.display='none'}if(getURLParameter('thirdparty')){thirdparty.style.display='block'}if(getURLParameter('tracking')){tracking.style.display='block'}if(getURLParameter('hideDetailsBtn')){promptBtn.style.display='none'}if(getURLParameter('scrolling')){scrolling.style.display='inline-block'}if(getURLParameter('top')){cookieBar.style.top=0;setBodyMargin('top')}else{cookieBar.style.bottom=0;setBodyMargin('bottom')}if(getURLParameter('privacyPage')){var url=decodeURIComponent(getURLParameter('privacyPage'));privacyLink.href=url;privacyPage.style.display='inline-block'}setEventListeners();fadeIn(cookieBar,250);setBodyMargin()}};request.send()}function getScriptPath(){var scripts=document.getElementsByTagName('script');for(i=0;i<scripts.length;i+=1){if(scripts[i].hasAttribute('src')){path=scripts[i].src;if(path.indexOf('cookiebar')>-1){return path}}}}function detectLang(){var userLang=getURLParameter('forceLang');if(userLang===false){userLang=navigator.language||navigator.userLanguage}userLang=userLang.substr(0,2);if(CookieLanguages.indexOf(userLang)<0){userLang='en'}return userLang}function getCookie(){var cookieValue=document.cookie.match(/(;)?cookiebar=([^;]*);?/);if(cookieValue==null){return undefined}else{return decodeURI(cookieValue[2])}}function setCookie(name,value){var exdays=30;if(getURLParameter('remember')){exdays=getURLParameter('remember')}var exdate=new Date();exdate.setDate(exdate.getDate()+parseInt(exdays));var cValue=encodeURI(value)+((exdays===null)?'':'; expires='+exdate.toUTCString()+';path=/');document.cookie=name+'='+cValue}function removeCookies(){document.cookie.split(';').forEach(function(c){document.cookie=c.replace(/^\ +/,'').replace(/\=.*/,'=;expires='+new Date().toUTCString()+';path=/')});localStorage.clear()}function fadeIn(el,speed){var s=el.style;s.opacity=0;s.display='block';(function fade(){(s.opacity-=-0.1)>0.9?null:setTimeout(fade,(speed/10))})()}function fadeOut(el,speed){var s=el.style;s.opacity=1;(function fade(){(s.opacity-=0.1)<0.1?s.display='none':setTimeout(fade,(speed/10))})()}function setBodyMargin(where){setTimeout(function(){var height=document.getElementById('cookie-bar').clientHeight;var bodyEl=document.getElementsByTagName('body')[0];var bodyStyle=bodyEl.currentStyle||window.getComputedStyle(bodyEl);switch(where){case 'top':bodyEl.style.marginTop=(parseInt(bodyStyle.marginTop)+height)+'px';break;case 'bottom':bodyEl.style.marginBottom=(parseInt(bodyStyle.marginBottom)+height)+'px';break}},300)}function clearBodyMargin(){var height=document.getElementById('cookie-bar').clientHeight;if(getURLParameter('top')){var currentTop=parseInt(document.getElementsByTagName('body')[0].style.marginTop);document.getElementsByTagName('body')[0].style.marginTop=currentTop-height+'px'}else{var currentBottom=parseInt(document.getElementsByTagName('body')[0].style.marginBottom);document.getElementsByTagName('body')[0].style.marginBottom=currentBottom-height+'px'}}function getURLParameter(name){var set=scriptPath.split(name+'=');if(set[1]){return set[1].split(/[&?]+/)[0]}else{return false}}function setEventListeners(){button.addEventListener('click',function(){setCookie('cookiebar','CookieAllowed');clearBodyMargin();fadeOut(prompt,250);fadeOut(cookieBar,250);if(getURLParameter('refreshPage')){window.location.reload()}});buttonNo.addEventListener('click',function(){var txt=promptNoConsent.textContent.trim();var confirm=window.confirm(txt);if(confirm===true){removeCookies();setCookie('cookiebar','CookieDisallowed');clearBodyMargin();fadeOut(prompt,250);fadeOut(cookieBar,250)}});promptBtn.addEventListener('click',function(){fadeIn(prompt,250)});promptClose.addEventListener('click',function(){fadeOut(prompt,250)});if(getURLParameter('scrolling')){var scrollPos=document.body.getBoundingClientRect().top;var scrolled=false;window.addEventListener('scroll',function(){if(scrolled===false){if(document.body.getBoundingClientRect().top-scrollPos>250||document.body.getBoundingClientRect().top-scrollPos<-250){setCookie('cookiebar','CookieAllowed');clearBodyMargin();fadeOut(prompt,250);fadeOut(cookieBar,250);scrolled=true;if(getURLParameter('refreshPage')){window.location.reload()}}}})}}}document.addEventListener('DOMContentLoaded',function(){setupCookieBar()});