kreuzberg-theme/js/cookie-consent.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-09-28 10:56:58 +00:00
/*===============================================
Cookie Consent
===============================================*/
const cookieStorage = {
getItem: (key) => {
const cookies = document.cookie
.split(';')
.map(cookie => cookie.split('='))
.reduce((acc, [key, value]) => ({ ...acc, [key.trim()]: value }), {});
return cookies[key];
},
setItem: (key, value) => {
document.cookie = `${key}=${value}`;
},
};
const storageType = cookieStorage;
const consentName = 'cookie_consent';
const ShowConsent = () => !storageType.getItem(consentName);
window.onload = () => {
const consentPopup = document.getElementById('cookie-consent');
const acceptBtn = document.getElementById('accept-consent');
const acceptFn = event => {
date = new Date();
date.setTime(date.getTime()+(1000*60*60*24*30)); /* 30 days */
expires = date.toGMTString();
document.cookie = consentName+"=accepted; expires="+expires+"; path=/";
consentPopup.classList.add('consent-hidden');
event.preventDefault();
}
acceptBtn.addEventListener('click', acceptFn);
if (ShowConsent(storageType)) {
setTimeout(() => {
consentPopup.classList.remove('consent-hidden');
}, 2000); /* 2 seconds */
}
};