function setCookie(name, value, days = 365) { const d = new Date(); d.setTime(d.getTime() + days * 86400000); document.cookie = `${name}=${encodeURIComponent(value)};expires=${d.toUTCString()};path=/`; } function getCookie(name) { return document.cookie .split('; ') .find(row => row.startsWith(name + '=')) ?.split('=')[1]; } function getConsent() { try { return JSON.parse(decodeURIComponent(getCookie('cookie_consent'))); } catch { return null; } } function syncConsentToUI() { const consent = getConsent(); if (!consent) return; const statistics = document.getElementById('consent-statistics'); const marketing = document.getElementById('consent-marketing'); if (statistics) statistics.checked = !!consent.statistics; if (marketing) marketing.checked = !!consent.marketing; } /* ---------- Banner / Modal ---------- */ function openCookieModal() { syncConsentToUI(); // 🔥 Entscheidung laden document.getElementById('cookie-modal').classList.remove('hidden'); } function closeCookieModal() { document.getElementById('cookie-modal').classList.add('hidden'); } function hideBanner() { document.getElementById('cookie-banner').classList.add('hidden'); } /* ---------- Entscheidungen ---------- */ function acceptAllCookies() { saveConsent({ functional: true, statistics: true, marketing: true }); } function acceptNecessaryCookies() { saveConsent({ functional: true, statistics: false, marketing: false }); } function saveCookieSettings() { saveConsent({ functional: true, statistics: document.getElementById('consent-statistics').checked, marketing: document.getElementById('consent-marketing').checked, }); } function saveConsent(consent) { setCookie('cookie_consent', JSON.stringify(consent)); hideBanner(); closeCookieModal(); renderExternalEmbeds(); if (consent.statistics) { activateScripts('statistics'); } } function activateScripts(category) { document .querySelectorAll(`script[type="text/plain"][data-cookie-category="${category}"]`) .forEach(oldScript => { const script = document.createElement('script'); if (oldScript.src) { // Externe Scripts script.src = oldScript.src; script.async = true; } else { // 🔥 INLINE-SCRIPTS korrekt ausführen script.textContent = oldScript.textContent; } // Attribute übernehmen for (const attr of oldScript.attributes) { if (!['type', 'data-cookie-category', 'src'].includes(attr.name)) { script.setAttribute(attr.name, attr.value); } } oldScript.parentNode.replaceChild(script, oldScript); }); } /* ---------- Analytics ---------- */ function loadAnalytics() { if (window.__GA_CODE__ && !window.__GA_LOADED__) { window.__GA_LOADED__ = true; const div = document.createElement('div'); div.innerHTML = window.__GA_CODE__; document.head.appendChild(div); } } /* ---------- Init ---------- */ document.addEventListener('DOMContentLoaded', () => { const consent = getConsent(); if (!consent) { document.getElementById('cookie-banner').classList.remove('hidden'); return; } if (consent.statistics) { activateScripts('statistics'); } });