1const initCookies = () => { 2 // Document CLICK 3 document.addEventListener("click", onCookiesDocumentClick, false); 4} 5// ########################## 6// ### Doc event handlers ### 7// Doc click 8const onCookiesDocumentClick = (e) => { 9 const button = e.target.closest("button"); 10 if (button && button.hasAttribute("data-cookieterms-decline")) { 11 handleCookiesDecline(button); 12 } else if (button && button.hasAttribute("data-cookieterms-accept")) { 13 handleCookiesAccept(button); 14 } else if (button && button.hasAttribute("data-cookieterms-customize")) { 15 handleCookiesCustomize(button); 16 } else if (button && button.hasAttribute("data-cookieterms-save")) { 17 handleCookiesSave(button); 18 } else if (button && button.hasAttribute("data-cookieterms-show")) { 19 handleCookiesShow(button); 20 } 21} 22// ### Event handlers ### 23// accept all cookies 24const handleCookiesAccept = (trigger) => { 25 const cookieName = getCookieName(); 26 const cookieValue = { 27 necessary: true, 28 functional: true, 29 analytics: true, 30 performance: true, 31 advertisement: true 32 }; 33 if (cookieName) { 34 cookieValue.privacySettingsSaved = getPrivacySettingsSavedCookieValue();
35 setCookie(cookieName, cookieValue, 30); 36 } 37 // update scripts 38 onUserSettingsChanged(cookieValue); 39 // hide box 40 document.body.classList.add("cookieterms-hidden"); 41} 42// decline all cookies 43const handleCookiesDecline = (trigger) => { 44 const cookieName = getCookieName(); 45 if (cookieName) { 46 unsetCookie(cookieName); 47 } 48 const cookieValue = { 49 necessary: false, 50 functional: false, 51 analytics: false, 52 performance: false, 53 advertisement: false 54 }; 55 // update scripts 56 onUserSettingsChanged(cookieValue); 57 // hide box 58 document.body.classList.add("cookieterms-hidden"); 59} 60// save custom cookies settings 61const handleCookiesSave = (trigger) => { 62 const cookieName = getCookieName(); 63 const cookieValue = {}; 64 const checkboxes = [...document.querySelectorAll("input[data-cookieterms-group]")]; 65 checkboxes.forEach(checkbox => { 66 if (checkbox.checked) { 67 cookieValue[checkbox.dataset.cookietermsGroup] = true; 68 } else { 69 cookieValue[checkbox.dataset.cookietermsGroup] = false; 70 } 71 }); 72 if (cookieName) { 73 cookieValue.privacySettingsSaved = getPrivacySettingsSavedCookieValue();
74 setCookie(cookieName, cookieValue, 30); 75 } 76 // update scripts 77 onUserSettingsChanged(cookieValue); 78 // hide box 79 document.body.classList.add("cookieterms-hidden"); 80} 81// show (unhide) cookieterms box 82const handleCookiesShow = (trigger) => { 83 const cookieName = trigger.dataset.cookietermsShow; 84 if (cookieName) { 85 const cookieValueStr = getCookie(cookieName); 86 const cookieValue = cookieValueStr ? JSON.parse(cookieValueStr) : {}; 87 if (cookieValue && cookieValue.privacySettingsSaved) { 88 cookieValue.privacySettingsSaved = false; 89 setCookie(cookieName, cookieValue, 30); 90 window.location.reload(); 91 } 92 } 93} 94// customize 95const handleCookiesCustomize = (trigger) => { 96 const boxAccept = document.querySelector(".box-cookieterms-accept"); 97 const boxSettings = document.querySelector(".box-cookieterms-settings"); 98 if (boxAccept && boxSettings) { 99 boxAccept.style.display = "none"; 100 boxSettings.style.display = "block"; 101 } 102} 103// update SCRIPTS 104const onUserSettingsChanged = (cookieValue) => { 105 // check gtag consent mode 106 if (typeof gtag !== 'undefined') { 107 gtag('consent', 'update', { 108 'ad_storage': cookieValue.advertisement ? 'granted' : 'denied', 109 'ad_user_data': cookieValue.advertisement ? 'granted' : 'denied', 110 'ad_personalization': cookieValue.advertisement ? 'granted' : 'denied', 111 'analytics_storage': cookieValue.analytics ? 'granted' : 'denied', 112 'functionality_storage': cookieValue.functional ? 'granted' : 'denied', 113 'personalization_storage': cookieValue.performance ? 'granted' : 'denied', 114 'security_storage': cookieValue.performance ? 'granted' : 'denied' 115 }); 116 } 117 // update all scripts 118 // Update scripts based on cookie values 119 Object.keys(cookieValue).forEach(groupName => { 120 if (groupName !== 'privacySettingsSaved') { 121 const scripts = document.querySelectorAll(`script[data-tracking-code-group="${groupName}"]`); 122 scripts.forEach(script => { 123 if (cookieValue[groupName]) { 124 // If group is accepted, execute script content 125 const newScript = document.createElement('script'); 126 Array.from(script.attributes).forEach(attr => { 127 if (attr.name !== 'type' && attr.name !== 'data-tracking-code-group') { 128 newScript.setAttribute(attr.name, attr.value); 129 } 130 }); 131 newScript.textContent = script.textContent; 132 script.parentNode.replaceChild(newScript, script); 133 } 134 }); 135 } 136 }); 137} 138 139// ### Helpers ### 140const getCookieName = () => { 141 const box = document.querySelector("[data-cookieterms-cookie]"); 142 if (box) { 143 return box.dataset.cookietermsCookie; 144 } 145 return null; 146} 147const getCookie = (name) => { 148 const nameEQ = name + "="; 149 const ca = document.cookie.split(';'); 150 for(let i=0;i < ca.length;i++) {
151 let c = ca[i]; 152 while (c.charAt(0)==' ') c = c.substring(1,c.length); 153 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 154 } 155 return null; 156} 157const setCookie = (name, value, days) => { 158 const valueStr = JSON.stringify(value); 159 let expires = "" 160 if (days) { 161 let date = new Date() 162 date.setTime(date.getTime() + (days*24*60*60*1000)) 163 expires = "; expires=" + date.toGMTString() 164 } 165 document.cookie = name + "=" + valueStr + expires + "; path=/;" 166 return true 167} 168const unsetCookie = (name) => { 169 document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;" 170} 171// returns timestamp 172const getPrivacySettingsSavedCookieValue = () => { 173 return Math.floor(Date.now() / 1000); 174} 175 176 177// init cookies 178initCookies();
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.