Files
hugo_cookie_consent/assets/js/hugo_cookie_consent/cookie_consent.js
T
danielandClaude Sonnet 5 3f511f5e6f Log a clear console error when the cookie banner is missing
Silently returning made a missing banner.html integration
indistinguishable from a working page, which cost real debugging
time. Fail loud instead, pointing at the README.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-08 19:26:04 +02:00

59 lines
1.7 KiB
JavaScript

(function () {
var KEY = 'cookie_consent';
var banner = document.getElementById('cookie-banner');
if (!banner) {
console.error(
'[hugo_cookie_consent] #cookie-banner not found in the DOM. ' +
'Did you include the hugo_cookie_consent/banner.html partial in your site\'s layout? ' +
'See the module README ("Installation in your Hugo site").'
);
return;
}
updateStatus();
var consent = localStorage.getItem(KEY);
if (!consent) banner.style.display = 'flex';
function dismiss() {
banner.classList.add('fade-out');
setTimeout(function () { banner.style.display = 'none'; }, 500);
}
function updateStatus() {
var el = document.getElementById('cookie-status');
if (!el) return;
var consent = localStorage.getItem(KEY);
el.textContent = consent === 'accepted' ? el.dataset.accepted
: consent === 'declined' ? el.dataset.declined
: el.dataset.none;
}
document.getElementById('cookie-accept').onclick = function () {
localStorage.setItem(KEY, 'accepted');
updateStatus();
dismiss();
window.dispatchEvent(new Event('cookieAccepted'));
};
document.getElementById('cookie-decline').onclick = function () {
localStorage.setItem(KEY, 'declined');
updateStatus();
dismiss();
};
window.reopenCookieBanner = function () {
localStorage.removeItem(KEY);
updateStatus();
banner.classList.remove('fade-out');
banner.style.display = 'flex';
};
window.addEventListener('cookieAccepted', function () {
if (typeof window.trackVisit === 'function') window.trackVisit();
});
if (localStorage.getItem(KEY) === 'accepted') {
if (typeof window.trackVisit === 'function') window.trackVisit();
}
})();