var script = document.createElement('script'); script.src = '/resource/CookieControl'; script.onload = function() { console.log('[Civic] CookieControl script loaded successfully'); }; script.onerror = function() { console.error('[Civic] CookieControl script FAILED to load — check /resource/CookieControl exists'); }; document.head.appendChild(script); var cookieControlReady = false; window.addEventListener('civiccommand', function(event) { if (!event.detail || !event.detail.type) return; handleCivicCommand(event.detail.type); }); var cookieControlReady = false; window.addEventListener('message', function(event) { if (!event.data || typeof event.data !== 'object') return; if (event.data.type === 'CIVIC_CONFIG') { var config = event.data.config; if (config && config.enabled) { waitForCookieControl(function() { const alreadyReady = !!window.CookieControl && typeof CookieControl.config === 'function'; CookieControl.load({ apiKey: config.apiKey, product: config.product, initialState: config.initialState, necessaryCookies: config.necessaryCookies, optionalCookies: config.optionalCookies, notifyOnce: config.notifyOnce, acceptBehaviour: config.acceptBehaviour, rejectButton: config.rejectButton, onLoad: function() { cookieControlReady = true; dispatchConsentEvent({ type: 'CIVIC_READY' }); } }); if (alreadyReady) { cookieControlReady = true; dispatchConsentEvent({ type: 'CIVIC_READY', restored: true }); } }); } } }); function waitForCookieControl(callback) { if (typeof CookieControl !== 'undefined') { callback(); } else { var interval = setInterval(function() { if (typeof CookieControl !== 'undefined') { clearInterval(interval); callback(); } }, 50); } } function waitForCivicCookie(callback, maxWait, interval) { var waited = 0; var timer = setInterval(function () { var hasCivicCookie = document.cookie.split(';').some(function(c) { return c.trim().indexOf('CookieControl=') === 0; }); if (hasCivicCookie || waited >= maxWait) { clearInterval(timer); callback(hasCivicCookie); } waited += interval; }, interval); } function handleCivicCommand(type) { if (type === 'CIVIC_GET_CONSENT') { if (!cookieControlReady) return; broadcastConsentState(); } if (type === 'CIVIC_ACCEPT_ANALYTICS') { var acceptReturn = CookieControl.changeCategory(0, true); if (acceptReturn) { waitForCivicCookie(function(hasCivicCookie) { dispatchConsentEvent({ type: 'CIVIC_CONSENT_STATE', analyticsConsent: acceptReturn, showBanner: !hasCivicCookie }); }, 2000, 50); } else { broadcastConsentState(); } } if (type === 'CIVIC_REJECT_ANALYTICS') { var rejectReturn = CookieControl.changeCategory(0, false); if (rejectReturn) { waitForCivicCookie(function(hasCivicCookie) { dispatchConsentEvent({ type: 'CIVIC_CONSENT_STATE', analyticsConsent: rejectReturn, showBanner: !hasCivicCookie }); }, 2000, 50); } else { broadcastConsentState(); } } } function hasCivicCookie() { return document.cookie.split(';').some(function(c) { return c.trim().indexOf('CookieControl=') === 0; }); } function broadcastConsentState() { try { var a = CookieControl.getCategoryConsent(0); var analyticsConsent = (a === null || a === undefined) ? null : !!a; var showBanner = !hasCivicCookie(); dispatchConsentEvent({ type: 'CIVIC_CONSENT_STATE', analyticsConsent: analyticsConsent, showBanner: showBanner }); } catch(e) { console.warn('CIVIC broadcastConsentState error:', e); } } function dispatchConsentEvent(detail) { window.dispatchEvent(new CustomEvent('civicresponse', { detail: detail })); }