PluginProbe
CookieAdmin – Cookie Consent Banner / 1.2.4
CookieAdmin – Cookie Consent Banner v1.2.4
1.2.4 1.2.3 1.2.2 1.2.1 1.2.0 1.1.9 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
← All changes | assets/js/consent.js +245 -122 1.1.1 → 1.2.4 View file →
@@ -1,6 +1,8 @@
1 -const days = cookieadmin_policy.cookieadmin_days;
1 +// This file handle the cookie banner and stuff related to that.
2 2
3 +var days = cookieadmin_policy.cookieadmin_days;
4 +
3 5 if(typeof cookieadmin_is_consent === 'undefined'){
4 6 window.cookieadmin_is_consent = {};
5 7 }
6 8 var cookieadmin_allcookies = cookieadmin_policy.categorized_cookies;
@@ -6,15 +8,24 @@
6 8 var cookieadmin_allcookies = cookieadmin_policy.categorized_cookies;
7 9 //setInterval(cookieadmin_categorize_cookies, 5000);
8 10
9 11 function cookieadmin_is_obj(consentObj){
10 - return (Object.keys(consentObj).length !== 0);
12 + try{
13 + return (
14 + consentObj !== null &&
15 + typeof consentObj === 'object' &&
16 + !Array.isArray(consentObj) &&
17 + Object.keys(consentObj).length > 0
18 + );
19 + }catch(e){
20 + return false;
21 + }
11 22 }
12 23
13 24
14 25 // function cookieadmin_cookie_interceptor(){
15 -
16 - const originalCookieDescriptor =
26 +
27 + var originalCookieDescriptor =
17 28 Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
18 29 Object.getOwnPropertyDescriptor(document, 'cookie');
19 30 var allowed_cookies = '';
20 31
@@ -25,42 +36,62 @@
25 36 get: function(){
26 37 return originalCookieDescriptor.get.call(document);
27 38 },
28 39 set: function(val){
29 - if (!val) return false;
40 +
41 + if (!val) return;
42 +
43 + var separatorIndex = val.indexOf('=');
44 + if(separatorIndex === -1) {
45 + return;
46 + }
30 47
31 - splited_val = val.split("=");
32 - val_name = splited_val[0].trim();
48 + var cookieName = val.substring(0, separatorIndex).trim();
49 + var cookieValue = val.substring(separatorIndex + 1).trim();
50 +
51 + if(cookieName === "cookieadmin_consent"){
52 + originalCookieDescriptor.set.call(document, val);
53 + return;
54 + }
33 55
34 - if(splited_val[1].trim().startsWith("deleted;")) return false;
56 + // Set cookies which are meant to be deleted
57 + if(val.includes('expires=Thu, 01 Jan 1970') || cookieValue.startsWith("deleted;")){
58 + originalCookieDescriptor.set.call(document, val);
59 + return;
60 + }
61 +
62 + var cookieInfo = cookieadmin_allcookies[cookieName] || {};
63 + var category = (cookieInfo.category || 'uncategorized').toLowerCase();
35 64
36 - rm_val = val_name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
37 -
38 - if(val_name === "cookieadmin_consent"){
39 - return originalCookieDescriptor.set.call(document, val);
65 + // Set necessary cookies
66 + if(category == "necessary"){
67 + originalCookieDescriptor.set.call(document, val);
68 + return;
40 69 }
41 -
42 - if(cookieadmin_is_obj(cookieadmin_is_consent) && (val_name in cookieadmin_allcookies) ){
70 +
71 + if(cookieadmin_is_obj(cookieadmin_is_consent) || (Array.isArray(cookieadmin_policy['preload']) && cookieadmin_policy['preload'].length > 0)){
72 +
73 + var consentAction = cookieadmin_is_consent.action;
74 +
75 + if(!consentAction){
76 + consentAction = cookieadmin_policy['preload'].reduce((a, val) => {a[val] = ''; return a;}, {});
77 + }
78 +
79 + if(consentAction.accept || consentAction[category]){
80 + originalCookieDescriptor.set.call(document, val);
81 + }else{
43 82
44 - if(!!cookieadmin_is_consent.action.reject){
45 - return originalCookieDescriptor.set.call(document, rm_val);
46 - }
47 - else if(!!cookieadmin_is_consent.action.accept){
48 - return originalCookieDescriptor.set.call(document, val);
83 + var pathMatch = val.match(/path=([^;]+)/i);
84 + var domainMatch = val.match(/domain=([^;]+)/i);
85 + var path = pathMatch ? pathMatch[1].trim() : '/';
86 + var domain = domainMatch ? `domain=${domainMatch[1].trim()};` : '';
87 +
88 + var deleteString = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path}; ${domain}`;
89 + originalCookieDescriptor.set.call(document, deleteString.trim());
49 90 }
50 - else if(!!cookieadmin_allcookies[val_name].category){
51 -
52 - if(cookieadmin_is_consent.action[cookieadmin_allcookies[val_name].category.toLowerCase()]){
53 - return originalCookieDescriptor.set.call(document, val);
54 - }
55 - else{
56 - return originalCookieDescriptor.set.call(document, rm_val);
57 - }
58 - return true;
59 - }
60 91
61 92 }else{
62 - (cookieadmin_allcookies[val_name] = cookieadmin_allcookies[val_name] || {}).string = val.trim();
93 + (cookieadmin_allcookies[cookieName] = cookieadmin_allcookies[cookieName] || {}).string = val.trim();
63 94 return false;
64 95 }
65 96
66 97 }
@@ -72,18 +103,28 @@
72 103 function cookieadmin_is_cookie(name){
73 104
74 105 if(!document.cookie) return false;
75 106
76 - let coki = document.cookie.split(";") ;
107 + var coki = document.cookie.split(";") ;
77 108
78 109 if(name == "all"){
79 110 return coki ? coki : [];
80 111 }
81 - let nam = name + "=";
112 + var nam = name + "=";
82 113
83 - for(let i=0; i < coki.length; i++){
114 + for(var i=0; i < coki.length; i++){
84 115 if(coki[i].trim().indexOf(nam) == 0){
85 - return coki[i].trim();
116 + try {
117 + var cookie_value = coki[i].trim().split("=");
118 + if(!cookie_value[1]){
119 + return false;
120 + }
121 +
122 + var decoded = decodeURIComponent(cookie_value[1]);
123 + return JSON.parse(decoded);
124 + } catch {
125 + return false;
126 + }
86 127 }
87 128 }
88 129
89 130 return false;
@@ -89,11 +130,10 @@
89 130 return false;
90 131 }
91 132
92 133 function cookieadmin_check_consent(){
93 -
94 - if(cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent")){
95 - cookieadmin_cookie = JSON.parse(cookieadmin_cookie.split("=")[1]);
134 + var cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent");
135 + if(!!cookieadmin_cookie){
96 136 if(!!cookieadmin_cookie.consent){
97 137 cookieadmin_is_consent.consent = cookieadmin_cookie.consent;
98 138 delete cookieadmin_cookie.consent;
99 139 }
@@ -119,9 +159,9 @@
119 159
120 160 }else if(update.reject && update.reject == "true"){
121 161 return true;
122 162 }else{
123 - for (const [key, value] of Object.entries(update)) {
163 + for (var [key, value] of Object.entries(update)) {
124 164 if(key != "consent"){
125 165 cookieadmin_accepted_categories.push(key);
126 166 }
127 167 }
@@ -128,9 +168,9 @@
128 168 }
129 169
130 170
131 171 for(cookie in cookieadmin_allcookies){
132 - document.cookie = cookie.string;
172 + document.cookie = cookieadmin_allcookies[cookie].string;
133 173 };
134 174
135 175 cookieadmin_accepted_categories.forEach(function(category) {
136 176
@@ -136,9 +176,9 @@
136 176
137 177 document.querySelectorAll(
138 178 'script[type="text/plain"][data-cookieadmin-category="' + category + '"]'
139 179 ).forEach(function(el) {
140 - const newScript = document.createElement('script');
180 + var newScript = document.createElement('script');
141 181
142 182 // Copy attributes
143 183 if (el.src) {
144 184 newScript.src = el.src;
@@ -163,14 +203,23 @@
163 203
164 204 function cookieadmin_set_cookie(name, value, days = 365, domain = "") {
165 205 if (!name || !value) return false;
166 206
167 - const date = new Date();
207 + if((cookieadmin_policy.is_pro != 0) && (cookieadmin_pro_vars !== 'undefined')){
208 + if(cookieadmin_pro_vars.shared_subdomain_consent && cookieadmin_pro_vars.base_domain){
209 + // Delete the cookie if exist with the subdomin
210 + // Previously, cookie was set without specifying domain name, we'll not specify domain while deleting.
211 + document.cookie = `cookieadmin_consent=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${cookieadmin_policy.base_path};`;
212 + domain = cookieadmin_pro_vars.base_domain;
213 + }
214 + }
215 +
216 + var date = new Date();
168 217 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); // default 1 year
169 218
170 - let cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
219 + var cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
171 220 cookieString += ` expires=${date.toUTCString()};`;
172 - cookieString += ` path=${cookieadmin_policy.base_path};`;
221 + cookieString += ` path=${!domain ? cookieadmin_policy.base_path : '/'};`;
173 222 cookieString += ` SameSite=Lax;`;
174 223 if(cookieadmin_policy.is_ssl || window.location.protocol === 'https:'){
175 224 cookieString += ` Secure;`;
176 225 }
@@ -183,9 +232,9 @@
183 232 document.cookie = cookieString;
184 233 return true;
185 234 }
186 235
187 -//Populates modal with consent if selected
236 +//Populates modal with consent if selected & adds found cookies
188 237 function cookieadmin_populate_preference(){
189 238
190 239 consent = cookieadmin_is_consent.action;
191 240
@@ -207,34 +256,58 @@
207 256 btn_ele.checked = true;
208 257 }
209 258 }
210 259 }
260 + //Load as per preloaded categories selected by admin
261 + }else if(cookieadmin_policy['preload'] && cookieadmin_policy['preload'].length > 0){
262 +
263 + cookieadmin_policy['preload'].forEach(function(val){
264 + if(btn_ele = document.querySelector("#cookieadmin-" + val)){
265 + btn_ele.checked = true;
266 + }
267 + });
211 268 }
212 269
213 - cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
270 + var cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
214 271
215 272 if(cookieadmin_allcookies){
216 273
217 - cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
274 + var cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
218 275
219 276 for(c_info of cookieadmin_filtrd){
220 277
221 - if(e = document.querySelector(".cookieadmin-" + cookieadmin_allcookies[c_info].category?.toLowerCase())){
222 - e.innerHTML = (e.innerHTML == 'None') ? '' : e.innerHTML;
278 + var category_var = cookieadmin_allcookies[c_info].category;
279 +
280 + if(!category_var){
281 + continue;
282 + }
283 + var card_container = document.querySelector(".cookieadmin-" + category_var.toLowerCase());
284 +
285 + if(!card_container){
286 + continue;
287 + }
288 +
289 + card_container.querySelector(".cookieadmin-nocookie-cat").style.display = 'none';
290 +
291 + var cookieadmin_exp = cookieadmin_policy.lang.session;
223 292
224 - exp = cookieadmin_policy.lang.session;
225 -
226 - if(!!cookieadmin_allcookies[c_info].expires){
227 - exp = Math.round((cookieadmin_allcookies[c_info].expires - Date.now())/86400);
228 - if(exp < 1 && !!res['Max-Age']){
229 - exp = res['Max-Age'];
230 - }else{
231 - exp = cookieadmin_policy.lang.session;
293 + if(
294 + !!cookieadmin_allcookies[c_info].expires
295 + && cookieadmin_allcookies[c_info].expires !== "0000-00-00 00:00:00"
296 + ){
297 +
298 + var expDate = new Date(cookieadmin_allcookies[c_info].expires.replace(' ', 'T'));
299 +
300 + if (!isNaN(expDate.getTime())) {
301 + var daysLeft = Math.ceil((expDate.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
302 +
303 + if (daysLeft > 0) {
304 + cookieadmin_exp = daysLeft +' '+ cookieadmin_policy.lang.days;
232 305 }
233 306 }
234 -
235 - e.innerHTML += '<div class="cookieadmin-cookie-card"> <div class="cookieadmin-cookie-header"> <strong class="cookieadmin-cookie-name">'+ c_info.replace(/_+$/, "") +'</strong> <span class="cookieadmin-cookie-duration"><b>'+ cookieadmin_policy.lang.duration +':</b> '+ exp +'</span> </div> <p class="cookieadmin-cookie-description">'+ cookieadmin_allcookies[c_info].description +'</p> <div class="cookieadmin-cookie-tags"> ' + (cookieadmin_allcookies[c_info].platform ? '<span class="cookieadmin-tag">' + cookieadmin_allcookies[c_info].platform + '</span>' : "") + ' </div> </div>';
236 307 }
308 +
309 + card_container.innerHTML += '<div class="cookieadmin-cookie-card"> <div class="cookieadmin-cookie-header"> <strong class="cookieadmin-cookie-name">'+ c_info.replace(/_+$/, "") +'</strong> <span class="cookieadmin-cookie-duration"><b>'+ cookieadmin_policy.lang.duration +':</b> '+ cookieadmin_exp +'</span> </div> <p class="cookieadmin-cookie-description">'+ cookieadmin_allcookies[c_info].description +'</p> <div class="cookieadmin-cookie-tags"> ' + (cookieadmin_allcookies[c_info].platform ? '<span class="cookieadmin-tag">' + cookieadmin_allcookies[c_info].platform + '</span>' : "") + ' </div> </div>';
237 310 cookieadmin_shown.push(c_info);
238 311 }
239 312 }
240 313
@@ -270,9 +343,9 @@
270 343 if(!cookieadmin_is_obj(cookieadmin_chk_cookies)){
271 344 return;
272 345 }
273 346
274 - /* const xhttp2 = new XMLHttpRequest();
347 + /* var xhttp2 = new XMLHttpRequest();
275 348
276 349 var data = 'action=cookieadmin_ajax_handler&cookieadmin_act=categorize_cookies&cookieadmin_security=' + cookieadmin_policy.nonce + "&cookieadmin_cookies=" + JSON.stringify(cookieadmin_chk_cookies);
277 350
278 351 xhttp2.onload = function() {
@@ -300,9 +373,9 @@
300 373
301 374 document.addEventListener("DOMContentLoaded", function() {
302 375
303 376 var cookieadmin_show_reconsent = 0;
304 - if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars.reconsent != 0){
377 + if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars !== 'undefined' && cookieadmin_pro_vars.reconsent != 0){
305 378 var cookieadmin_show_reconsent = 1;
306 379 }
307 380
308 381 //Create overlay
@@ -309,13 +382,23 @@
309 382 var cookieadmin_ovrlay = document.createElement("div");
310 383 cookieadmin_ovrlay.className = "cookieadmin_modal_overlay";
311 384 document.body.appendChild(cookieadmin_ovrlay);
312 385
386 + var before_consent_dispaly = new CustomEvent('cookieadmin_before_consent_display');
387 +
388 + // For anything that needs to be done before disaplying consent.
389 + cookieadmin_policy.hide_banner = false; // initializing
390 + window.dispatchEvent(before_consent_dispaly);
391 +
313 392 //Show notice or re-consent icon as needed
314 - if(!cookieadmin_is_obj(cookieadmin_is_consent)){
393 + if(!cookieadmin_is_obj(cookieadmin_is_consent) && !cookieadmin_policy.hide_banner){
315 394
316 395 if(cookieadmin_policy.cookieadmin_layout !== "popup"){
317 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
396 + var cookieadmin_banner_el = document.getElementsByClassName("cookieadmin_law_container")[0];
397 + if(cookieadmin_banner_el){
398 + cookieadmin_banner_el.style.display = "block";
399 + cookieadmin_banner_el.focus();
400 + }
318 401 }else{
319 402 cookieadmin_toggle_overlay();
320 403 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
321 404 }
@@ -339,9 +422,8 @@
339 422
340 423 }
341 424
342 425 //Edit Notice and Modal contents
343 - document.getElementsByClassName("cookieadmin_reconsent_img")[0].src = cookieadmin_policy.plugin_url + "/assets/images/cookieadmin_icon.svg";
344 426
345 427 cookieadmin_populate_preference();
346 428
347 429 for(data in cookieadmin_policy){
@@ -386,9 +468,9 @@
386 468 }
387 469 }
388 470
389 471 //Add layout as class
390 - if(!!cookieadmin_policy.cookieadmin_position){
472 + if(!!cookieadmin_policy.cookieadmin_position && cookieadmin_policy.cookieadmin_layout !== "popup"){
391 473 cookieadmin_policy.cookieadmin_position.split("_").forEach(function(clas){
392 474 clas = "cookieadmin_" + clas;
393 475 document.getElementsByClassName("cookieadmin_law_container")[0].classList.add(clas);
394 476 });
@@ -394,9 +476,13 @@
394 476 });
395 477 }
396 478
397 479 // Change consent layout dynamically
398 - document.getElementsByClassName("cookieadmin_law_container")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
480 + // TODO: There is a operator called optional chaining operator ?. something like this users?.[0]?.name; wont use it now just for reference
481 + var cookieadmin_law_container_var = document.getElementsByClassName("cookieadmin_law_container")[0];
482 + if (!!cookieadmin_law_container_var){
483 + cookieadmin_law_container_var.classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
484 + }
399 485
400 486 // Change Modal layout dynamically
401 487 document.getElementsByClassName("cookieadmin_cookie_modal")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_modal);
402 488
@@ -412,8 +498,10 @@
412 498 if(cookieadmin_policy.cookieadmin_layout == "popup"){
413 499 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
414 500 }
415 501
502 + var cookieadmin_modal_el = document.getElementsByClassName("cookieadmin_cookie_modal")[0];
503 +
416 504 //show preference modal
417 505 cookieadmin_show_modal_elemnts = document.querySelectorAll(".cookieadmin_re_consent, .cookieadmin_customize_btn");
418 506 cookieadmin_show_modal_elemnts.forEach(function(e){
419 507
@@ -427,10 +515,17 @@
427 515 });*/
428 516
429 517 cookieadmin_toggle_overlay();
430 518 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
431 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "none";
432 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
519 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
520 + if(cookieadmin_re_consent){
521 + cookieadmin_re_consent.style.display = "none";
522 + }
523 +
524 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
525 + if(cookieadmin_law_container){
526 + cookieadmin_law_container.style.display = "none";
527 + }
433 528
434 529 if(cookieadmin_policy["cookieadmin_modal"] == "side"){
435 530 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "grid";
436 531 }
@@ -439,8 +534,10 @@
439 534 document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_re_consent";
440 535 }else{
441 536 document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_law_container";
442 537 }
538 +
539 + cookieadmin_modal_el.focus();
443 540 });
444 541 });
445 542
446 543 //Save preference
@@ -459,14 +556,30 @@
459 556 }
460 557 });
461 558
462 559 if(Object.keys(prefer).length !== 0){
463 - if(Object.keys(prefer).length === 3){
464 - document.querySelectorAll(".cookieadmin_accept_btn")[1].click();
560 + var override_gpc = document.getElementById('cookieadmin-override_gpc');
561 + var is_override_gpc = false;
562 + if(override_gpc && override_gpc.checked){
563 + is_override_gpc = true;
564 + }
565 +
566 + if(Object.keys(prefer).length === 3 && !is_override_gpc){
567 + let accept_btn = document.querySelectorAll(".cookieadmin_accept_btn");
568 +
569 + if(accept_btn.length > 0){
570 + accept_btn[accept_btn.length-1].click();
571 + }
572 +
465 573 return;
466 574 }
467 575 }else{
468 - document.querySelectorAll(".cookieadmin_reject_btn")[1].click();
576 + let reject_btn = document.querySelectorAll(".cookieadmin_reject_btn");
577 +
578 + if(reject_btn.length > 0){
579 + reject_btn[reject_btn.length-1].click();
580 + }
581 +
469 582 return;
470 583 }
471 584
472 585 cookieadmin_toggle_overlay();
@@ -471,14 +584,8 @@
471 584
472 585 cookieadmin_toggle_overlay();
473 586
474 587 cookieadmin_set_consent(prefer, days);
475 -
476 - if(!!cookieadmin_policy.reload_on_consent){
477 - location.reload();
478 - }else{
479 - cookieadmin_restore_cookies(prefer);
480 - }
481 588 });
482 589
483 590
484 591 //Accept or reject all cookies
@@ -489,60 +596,56 @@
489 596 e.addEventListener("click", function(){
490 597 // console.log(e);
491 598
492 599 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
493 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
494 - if(cookieadmin_show_reconsent){
495 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
600 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
601 + if(cookieadmin_law_container){
602 + cookieadmin_law_container.style.display = "none";
496 603 }
497 604
605 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
606 + if(cookieadmin_re_consent){
607 + cookieadmin_re_consent.style.display = "block";
608 + }
609 +
498 610 if(e.id.includes("modal")){
499 611 cookieadmin_toggle_overlay();
500 612 }
501 613
502 614 var prefer2 = e.classList.contains("cookieadmin_reject_btn") ? {reject: "true"} : {accept: "true"};
503 -
615 +
504 616 cookieadmin_set_consent(prefer2, days);
505 -
506 - if(!!cookieadmin_policy.reload_on_consent){
507 - location.reload();
508 - }else{
509 - cookieadmin_restore_cookies(prefer2);
510 - }
511 617 });
512 618 });
513 619
514 - //showmore modal btn
515 - document.getElementsByClassName("cookieadmin_showmore")[0]?.addEventListener("click", function(){
516 -
517 - var cur_height = document.getElementsByClassName("cookieadmin_preference")[0].style.height;
518 -
519 - if(!cur_height){
520 - document.getElementsByClassName("cookieadmin_preference")[0].style.height = "auto";
521 - this.innerHTML = cookieadmin_policy.lang.show_less;
522 - }else{
523 - document.getElementsByClassName("cookieadmin_preference")[0].style.height = "";
524 - this.innerHTML = cookieadmin_policy.lang.show_more;
525 - }
526 - });
620 + document.querySelectorAll(".cookieadmin_show_pref_cookies").forEach(function(btn){
527 621
622 + // Shared toggle function
623 + function toggleAccordion(event){
624 + // Use currentTarget to target the element with the listener, even if child nodes are clicked
625 + var trigger = event.currentTarget;
626 + var tgt = trigger.id.replace(/-container$/, "");
627 + var targetEl = document.querySelector("." + tgt);
528 628
529 - document.querySelectorAll(".cookieadmin_show_pref_cookies").forEach(function(e){
530 - e.addEventListener("click", function(el){
531 -
532 - var tgt = el.target.id;
533 - tgt = tgt.replace(/-container$/, "");
534 -
535 - if(el.target.classList.contains("dwn")){
536 - el.target.innerHTML = "&#9658;";
537 - el.target.classList.remove("dwn");
538 - document.querySelector("."+tgt).style.display = "none";
629 + if (!targetEl){
630 + return;
631 + };
632 +
633 + if(trigger.classList.contains("dwn")){
634 + trigger.innerHTML = "&#9658;"; // Right arrow
635 + trigger.classList.remove("dwn");
636 + trigger.setAttribute("aria-expanded", "false");
637 + targetEl.style.display = "none";
539 638 }else{
540 - el.target.innerHTML = "&#9660;";
541 - el.target.classList.add("dwn");
542 - document.querySelector("."+tgt).style.display = "block";
639 + trigger.innerHTML = "&#9660;"; // Down arrow
640 + trigger.classList.add("dwn");
641 + trigger.setAttribute("aria-expanded", "true");
642 + targetEl.style.display = "block";
543 643 }
544 - });
644 + }
645 +
646 + // 2. Native buttons fire click on mouse, Enter and Space
647 + btn.addEventListener("click", toggleAccordion);
545 648 });
546 649
547 650 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
548 651 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
@@ -547,11 +650,17 @@
547 650 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
548 651 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
549 652 cookieadmin_toggle_overlay();
550 653 if(!cookieadmin_is_obj(cookieadmin_is_consent)){
551 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
654 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
655 + if(cookieadmin_law_container){
656 + cookieadmin_law_container.style.display = "block";
657 + }
552 658 }else if(cookieadmin_show_reconsent){
553 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
659 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
660 + if(cookieadmin_re_consent){
661 + cookieadmin_re_consent.style.display = "block";
662 + }
554 663 }
555 664 });
556 665
557 666 });
@@ -557,17 +666,22 @@
557 666 });
558 667
559 668 function cookieadmin_set_consent(prefrenc, days){
560 669
670 + if (typeof cookieadmin_pro_set_consent === "function") {
671 + return cookieadmin_pro_set_consent(prefrenc, days);
672 + }else{
673 + return cookieadmin_save_consent_cookie(prefrenc, days);
674 + }
675 +}
676 +
677 +function cookieadmin_save_consent_cookie(prefrenc, days, consent_id){
678 +
561 679 var cookieadmin_consent = prefrenc;
562 -
563 - if (typeof cookieadmin_pro_set_consent === "function") {
564 - consent_id = cookieadmin_pro_set_consent(prefrenc, days);
565 680
566 - if(consent_id){
567 - cookieadmin_is_consent.consent = consent_id;
568 - cookieadmin_consent['consent'] = consent_id;
569 - }
681 + if(consent_id){
682 + cookieadmin_is_consent.consent = consent_id;
683 + cookieadmin_consent['consent'] = consent_id;
570 684 }
571 685
572 686 if(!cookieadmin_is_consent.consent){
573 687 cookieadmin_is_consent.consent = "";
@@ -580,7 +694,16 @@
580 694
581 695 if (typeof cookieadmin_update_gcm === "function") {
582 696 cookieadmin_update_gcm(1);
583 697 }
698 +
699 + if (typeof cookieadmin_pro_update_clarity_cookie === "function") {
700 + cookieadmin_pro_update_clarity_cookie();
701 + }
584 702
703 + if(!!cookieadmin_policy.reload_on_consent){
704 + location.reload();
705 + }else{
706 + cookieadmin_restore_cookies(prefrenc);
707 + }
585 708 }
586 709