1import { t } from './i18n.js'; 2import { getLegalHref } from './legal.js'; 3 4const STORAGE_KEY = 'planeta_cookie_consent'; 5const CONSENT_EVENT = 'planeta:cookie-consent'; 6 7export function getCookieConsent() { 8 try { 9 return localStorage.getItem(STORAGE_KEY); 10 } catch { 11 return null; 12 } 13} 14 15export function setCookieConsent(value) { 16 try { 17 localStorage.setItem(STORAGE_KEY, value); 18 document.documentElement.dataset.cookieConsent = value; 19 } catch { 20 // localStorage unavailable 21 } 22 try { 23 window.dispatchEvent(new CustomEvent(CONSENT_EVENT, { detail: { value } })); 24 } catch { 25 // ignore 26 } 27} 28 29function hideBanner(banner) { 30 banner.classList.add('is-hidden'); 31 banner.setAttribute('aria-hidden', 'true'); 32} 33 34export function initCookieConsent() { 35 const existing = getCookieConsent(); 36 document.documentElement.dataset.cookieConsent = existing || 'pending'; 37 38 if (existing) return; 39 40 const privacyHref = getLegalHref('privacy'); 41 const banner = document.createElement('div'); 42 banner.id = 'cookie-consent'; 43 banner.className = 'cookie-consent'; 44 banner.setAttribute('role', 'dialog'); 45 banner.setAttribute('aria-live', 'polite'); 46 banner.setAttribute('aria-label', t('cookie.ariaLabel')); 47 banner.innerHTML = ` 48 <div class="cookie-consent__panel"> 49 <div> 50 <div class="cookie-consent__title">${t('cookie.title')}</div> 51 <p class="cookie-consent__text"> 52 ${t('cookie.body')} 53 <a href="${privacyHref}">${t('cookie.privacyLink')}</a>. 54 </p> 55 </div> 56 <div class="cookie-consent__actions"> 57 <button type="button" class="btn btn-primary btn-sm" data-cookie-accept-all> 58 ${t('cookie.acceptAll')} 59 </button> 60 <button type="button" class="btn btn-secondary btn-sm" data-cookie-essential> 61 ${t('cookie.essentialOnly')} 62 </button> 63 </div> 64 </div> 65 `; 66 67 document.body.appendChild(banner); 68 69 banner.querySelector('[data-cookie-accept-all]')?.addEventListener('click', () => { 70 setCookieConsent('all'); 71 hideBanner(banner); 72 }); 73 74 banner.querySelector('[data-cookie-essential]')?.addEventListener('click', () => { 75 setCookieConsent('essential'); 76 hideBanner(banner); 77 }); 78}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.