PluginProbe
CookieAdmin – Cookie Consent Banner / trunk
CookieAdmin – Cookie Consent Banner vtrunk
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 +271 -142 1.0.2 → trunk View file →
@@ -1,10 +1,7 @@
1 -const days = cookieadmin_policy.cookieadmin_days;
1 +// This file handle the cookie banner and stuff related to that.
2 2
3 -var cookieadmin_show_reconsent = 0;
4 -if(cookieadmin_policy.is_pro != 0){
5 - var cookieadmin_show_reconsent = 1;
6 -}
3 +var days = cookieadmin_policy.cookieadmin_days;
7 4
8 5 if(typeof cookieadmin_is_consent === 'undefined'){
9 6 window.cookieadmin_is_consent = {};
10 7 }
@@ -11,15 +8,24 @@
11 8 var cookieadmin_allcookies = cookieadmin_policy.categorized_cookies;
12 9 //setInterval(cookieadmin_categorize_cookies, 5000);
13 10
14 11 function cookieadmin_is_obj(consentObj){
15 - 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 + }
16 22 }
17 23
18 24
19 25 // function cookieadmin_cookie_interceptor(){
20 -
21 - const originalCookieDescriptor =
26 +
27 + var originalCookieDescriptor =
22 28 Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
23 29 Object.getOwnPropertyDescriptor(document, 'cookie');
24 30 var allowed_cookies = '';
25 31
@@ -30,48 +36,62 @@
30 36 get: function(){
31 37 return originalCookieDescriptor.get.call(document);
32 38 },
33 39 set: function(val){
40 +
41 + if (!val) return;
42 +
43 + var separatorIndex = val.indexOf('=');
44 + if(separatorIndex === -1) {
45 + return;
46 + }
34 47
35 - val_name = val.split("=")[0].trim();
36 - rm_val = val_name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
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 + }
37 55
38 - if(val_name === "cookieadmin_consent"){
39 - return originalCookieDescriptor.set.call(document, val);
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;
40 60 }
61 +
62 + var cookieInfo = cookieadmin_allcookies[cookieName] || {};
63 + var category = (cookieInfo.category || 'uncategorized').toLowerCase();
41 64
42 - if(cookieadmin_is_obj(cookieadmin_is_consent) && (val_name in cookieadmin_allcookies) ){
65 + // Set necessary cookies
66 + if(category == "necessary"){
67 + originalCookieDescriptor.set.call(document, val);
68 + return;
69 + }
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;
43 74
44 - switch(cookieadmin_is_consent.action){
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{
45 82
46 - case "reject":
47 - if(cookieadmin_allcookies[val_name].sync){
48 - cookieadmin_allcookies[val_name].sync = false;
49 - return originalCookieDescriptor.set.call(document, rm_val);
50 - }
51 - return true;
52 -
53 - case "accept":
54 - if(!cookieadmin_allcookies[val_name].sync){
55 - cookieadmin_allcookies[val_name].sync = true;
56 - return originalCookieDescriptor.set.call(document, val);
57 - }
58 - return true;
59 -
60 - default:
61 - if(cookieadmin_is_consent.action[cookieadmin_allcookies[val_name].category] && !cookieadmin_allcookies[val_name].sync){
62 - cookieadmin_allcookies[val_name].sync = true;
63 - return originalCookieDescriptor.set.call(document, val);
64 - }
65 - else if(!(cookieadmin_is_consent.action[cookieadmin_allcookies[val_name].category]) && cookieadmin_allcookies[val_name].sync){
66 - cookieadmin_allcookies[val_name].sync = false;
67 - return originalCookieDescriptor.set.call(document, rm_val);
68 - }
69 - return true;
70 - }
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());
90 + }
71 91
72 92 }else{
73 - (cookieadmin_allcookies[val_name] = cookieadmin_allcookies[val_name] || {}).string = val.trim();
93 + (cookieadmin_allcookies[cookieName] = cookieadmin_allcookies[cookieName] || {}).string = val.trim();
74 94 return false;
75 95 }
76 96
77 97 }
@@ -83,18 +103,28 @@
83 103 function cookieadmin_is_cookie(name){
84 104
85 105 if(!document.cookie) return false;
86 106
87 - let coki = document.cookie.split(";") ;
107 + var coki = document.cookie.split(";") ;
88 108
89 109 if(name == "all"){
90 110 return coki ? coki : [];
91 111 }
92 - let nam = name + "=";
112 + var nam = name + "=";
93 113
94 - for(let i=0; i < coki.length; i++){
114 + for(var i=0; i < coki.length; i++){
95 115 if(coki[i].trim().indexOf(nam) == 0){
96 - 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 + }
97 127 }
98 128 }
99 129
100 130 return false;
@@ -100,15 +130,15 @@
100 130 return false;
101 131 }
102 132
103 133 function cookieadmin_check_consent(){
104 -
105 - if(cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent")){
106 - cookieadmin_cookie = JSON.parse(cookieadmin_cookie.split("=")[1]);
134 + var cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent");
135 + if(!!cookieadmin_cookie){
107 136 if(!!cookieadmin_cookie.consent){
108 137 cookieadmin_is_consent.consent = cookieadmin_cookie.consent;
109 138 delete cookieadmin_cookie.consent;
110 139 }
140 +
111 141 cookieadmin_is_consent.action = cookieadmin_cookie;
112 142 }
113 143 }
114 144 cookieadmin_check_consent();
@@ -129,9 +159,9 @@
129 159
130 160 }else if(update.reject && update.reject == "true"){
131 161 return true;
132 162 }else{
133 - for (const [key, value] of Object.entries(update)) {
163 + for (var [key, value] of Object.entries(update)) {
134 164 if(key != "consent"){
135 165 cookieadmin_accepted_categories.push(key);
136 166 }
137 167 }
@@ -136,14 +166,19 @@
136 166 }
137 167 }
138 168 }
139 169
170 +
171 + for(cookie in cookieadmin_allcookies){
172 + document.cookie = cookieadmin_allcookies[cookie].string;
173 + };
174 +
140 175 cookieadmin_accepted_categories.forEach(function(category) {
141 176
142 177 document.querySelectorAll(
143 178 'script[type="text/plain"][data-cookieadmin-category="' + category + '"]'
144 179 ).forEach(function(el) {
145 - const newScript = document.createElement('script');
180 + var newScript = document.createElement('script');
146 181
147 182 // Copy attributes
148 183 if (el.src) {
149 184 newScript.src = el.src;
@@ -165,19 +200,30 @@
165 200 });
166 201 });
167 202 }
168 203
169 -function cookieadmin_set_cookie(name, value, days = 365, domain = "", path = cookieadmin_get_base_path()) {
204 +function cookieadmin_set_cookie(name, value, days = 365, domain = "") {
170 205 if (!name || !value) return false;
171 206
172 - 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();
173 217 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); // default 1 year
174 218
175 - let cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
219 + var cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
176 220 cookieString += ` expires=${date.toUTCString()};`;
177 - cookieString += ` path=${path};`;
221 + cookieString += ` path=${!domain ? cookieadmin_policy.base_path : '/'};`;
178 222 cookieString += ` SameSite=Lax;`;
179 - cookieString += ` Secure;`; // Only sent over HTTPS
223 + if(cookieadmin_policy.is_ssl || window.location.protocol === 'https:'){
224 + cookieString += ` Secure;`;
225 + }
180 226
181 227 // Add domain if explicitly passed
182 228 if (domain) {
183 229 cookieString += ` domain=${domain};`;
@@ -186,9 +232,9 @@
186 232 document.cookie = cookieString;
187 233 return true;
188 234 }
189 235
190 -//Populates modal with consent if selected
236 +//Populates modal with consent if selected & adds found cookies
191 237 function cookieadmin_populate_preference(){
192 238
193 239 consent = cookieadmin_is_consent.action;
194 240
@@ -210,34 +256,58 @@
210 256 btn_ele.checked = true;
211 257 }
212 258 }
213 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 + });
214 268 }
215 269
216 - cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
270 + var cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
217 271
218 272 if(cookieadmin_allcookies){
219 273
220 - 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));
221 275
222 276 for(c_info of cookieadmin_filtrd){
223 277
224 - if(e = document.querySelector(".cookieadmin-" + cookieadmin_allcookies[c_info].category?.toLowerCase())){
225 - 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;
226 292
227 - exp = 'Session';
228 -
229 - if(!!cookieadmin_allcookies[c_info].expires){
230 - exp = Math.round((cookieadmin_allcookies[c_info].expires - Date.now())/86400);
231 - if(exp < 1 && !!res['Max-Age']){
232 - exp = res['Max-Age'];
233 - }else{
234 - exp = '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;
235 305 }
236 306 }
237 -
238 - 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>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>';
239 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>';
240 310 cookieadmin_shown.push(c_info);
241 311 }
242 312 }
243 313
@@ -273,9 +343,9 @@
273 343 if(!cookieadmin_is_obj(cookieadmin_chk_cookies)){
274 344 return;
275 345 }
276 346
277 - /* const xhttp2 = new XMLHttpRequest();
347 + /* var xhttp2 = new XMLHttpRequest();
278 348
279 349 var data = 'action=cookieadmin_ajax_handler&cookieadmin_act=categorize_cookies&cookieadmin_security=' + cookieadmin_policy.nonce + "&cookieadmin_cookies=" + JSON.stringify(cookieadmin_chk_cookies);
280 350
281 351 xhttp2.onload = function() {
@@ -301,8 +371,13 @@
301 371 }
302 372
303 373
304 374 document.addEventListener("DOMContentLoaded", function() {
375 +
376 + var cookieadmin_show_reconsent = 0;
377 + if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars !== 'undefined' && cookieadmin_pro_vars.reconsent != 0){
378 + var cookieadmin_show_reconsent = 1;
379 + }
305 380
306 381 //Create overlay
307 382 var cookieadmin_ovrlay = document.createElement("div");
308 383 cookieadmin_ovrlay.className = "cookieadmin_modal_overlay";
@@ -307,13 +382,23 @@
307 382 var cookieadmin_ovrlay = document.createElement("div");
308 383 cookieadmin_ovrlay.className = "cookieadmin_modal_overlay";
309 384 document.body.appendChild(cookieadmin_ovrlay);
310 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 +
311 392 //Show notice or re-consent icon as needed
312 - if(!cookieadmin_is_obj(cookieadmin_is_consent)){
393 + if(!cookieadmin_is_obj(cookieadmin_is_consent) && !cookieadmin_policy.hide_banner){
313 394
314 395 if(cookieadmin_policy.cookieadmin_layout !== "popup"){
315 - 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 + }
316 401 }else{
317 402 cookieadmin_toggle_overlay();
318 403 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
319 404 }
@@ -337,9 +422,8 @@
337 422
338 423 }
339 424
340 425 //Edit Notice and Modal contents
341 - document.getElementsByClassName("cookieadmin_reconsent_img")[0].src = cookieadmin_policy.plugin_url + "/assets/images/cookieadmin_icon.svg";
342 426
343 427 cookieadmin_populate_preference();
344 428
345 429 for(data in cookieadmin_policy){
@@ -384,9 +468,9 @@
384 468 }
385 469 }
386 470
387 471 //Add layout as class
388 - if(!!cookieadmin_policy.cookieadmin_position){
472 + if(!!cookieadmin_policy.cookieadmin_position && cookieadmin_policy.cookieadmin_layout !== "popup"){
389 473 cookieadmin_policy.cookieadmin_position.split("_").forEach(function(clas){
390 474 clas = "cookieadmin_" + clas;
391 475 document.getElementsByClassName("cookieadmin_law_container")[0].classList.add(clas);
392 476 });
@@ -392,9 +476,13 @@
392 476 });
393 477 }
394 478
395 479 // Change consent layout dynamically
396 - 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 + }
397 485
398 486 // Change Modal layout dynamically
399 487 document.getElementsByClassName("cookieadmin_cookie_modal")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_modal);
400 488
@@ -402,9 +490,9 @@
402 490
403 491 }*/
404 492
405 493 if(cookieadmin_policy.cookieadmin_modal == "side"){
406 - document.getElementsByClassName("cookieadmin_footer")[0].style.flexDirection = "column";
494 + document.getElementsByClassName("cookieadmin_modal_footer")[0].style.flexDirection = "column";
407 495 }
408 496
409 497 // Remove modal close Button
410 498 if(cookieadmin_policy.cookieadmin_layout == "popup"){
@@ -410,8 +498,10 @@
410 498 if(cookieadmin_policy.cookieadmin_layout == "popup"){
411 499 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
412 500 }
413 501
502 + var cookieadmin_modal_el = document.getElementsByClassName("cookieadmin_cookie_modal")[0];
503 +
414 504 //show preference modal
415 505 cookieadmin_show_modal_elemnts = document.querySelectorAll(".cookieadmin_re_consent, .cookieadmin_customize_btn");
416 506 cookieadmin_show_modal_elemnts.forEach(function(e){
417 507
@@ -425,10 +515,17 @@
425 515 });*/
426 516
427 517 cookieadmin_toggle_overlay();
428 518 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
429 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "none";
430 - 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 + }
431 528
432 529 if(cookieadmin_policy["cookieadmin_modal"] == "side"){
433 530 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "grid";
434 531 }
@@ -437,8 +534,10 @@
437 534 document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_re_consent";
438 535 }else{
439 536 document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_law_container";
440 537 }
538 +
539 + cookieadmin_modal_el.focus();
441 540 });
442 541 });
443 542
444 543 //Save preference
@@ -443,8 +542,13 @@
443 542
444 543 //Save preference
445 544 document.querySelector(".cookieadmin_save_btn").addEventListener("click", function(){
446 545
546 + document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
547 + if(cookieadmin_show_reconsent){
548 + document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
549 + }
550 +
447 551 var prefer = {};
448 552
449 553 document.querySelectorAll(".cookieadmin_toggle").forEach(function(e){
450 554 if(!!e.children[0].checked){
@@ -452,31 +556,36 @@
452 556 }
453 557 });
454 558
455 559 if(Object.keys(prefer).length !== 0){
456 - if(Object.keys(prefer).length === 3){
457 - 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 +
458 573 return;
459 574 }
460 575 }else{
461 - 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 +
462 582 return;
463 583 }
464 584
465 585 cookieadmin_toggle_overlay();
466 - document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
467 -
468 - if(cookieadmin_show_reconsent){
469 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
470 - }
471 -
586 +
472 587 cookieadmin_set_consent(prefer, days);
473 -
474 - if(!!cookieadmin_policy.reload_on_consent){
475 - location.reload();
476 - }else{
477 - cookieadmin_restore_cookies(prefer);
478 - }
479 588 });
480 589
481 590
482 591 //Accept or reject all cookies
@@ -485,58 +594,58 @@
485 594 cookieadmin_save_all_cookie_elemnts.forEach(function(e){
486 595
487 596 e.addEventListener("click", function(){
488 597 // console.log(e);
598 +
599 + document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
600 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
601 + if(cookieadmin_law_container){
602 + cookieadmin_law_container.style.display = "none";
603 + }
489 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 +
490 610 if(e.id.includes("modal")){
491 611 cookieadmin_toggle_overlay();
492 612 }
493 613
494 - document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
495 - if(cookieadmin_show_reconsent){
496 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
497 - }
498 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
499 614 var prefer2 = e.classList.contains("cookieadmin_reject_btn") ? {reject: "true"} : {accept: "true"};
500 -
615 +
501 616 cookieadmin_set_consent(prefer2, days);
502 -
503 - if(!!cookieadmin_policy.reload_on_consent){
504 - location.reload();
505 - }else{
506 - cookieadmin_restore_cookies(prefer2);
507 - }
508 617 });
509 618 });
510 619
511 - //showmore modal btn
512 - document.getElementsByClassName("cookieadmin_showmore")[0]?.addEventListener("click", function(){
513 - if(this.innerHTML.includes("more")){
514 - document.getElementsByClassName("cookieadmin_preference")[0].style.height = "auto";
515 - this.innerHTML = "show less";
516 - }else{
517 - document.getElementsByClassName("cookieadmin_preference")[0].style.height = "";
518 - this.innerHTML = "show more";
519 - }
520 - });
620 + document.querySelectorAll(".cookieadmin_show_pref_cookies").forEach(function(btn){
521 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);
522 628
523 - document.querySelectorAll(".show_pref_cookies").forEach(function(e){
524 - e.addEventListener("click", function(el){
525 -
526 - var tgt = el.target.id;
527 - tgt = tgt.replace(/-container$/, "");
528 -
529 - if(el.target.classList.contains("dwn")){
530 - el.target.innerHTML = "&#128898;";
531 - el.target.classList.remove("dwn");
532 - 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";
533 638 }else{
534 - el.target.innerHTML = "&#128899;";
535 - el.target.classList.add("dwn");
536 - 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";
537 643 }
538 - });
644 + }
645 +
646 + // 2. Native buttons fire click on mouse, Enter and Space
647 + btn.addEventListener("click", toggleAccordion);
539 648 });
540 649
541 650 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
542 651 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
@@ -541,11 +650,17 @@
541 650 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
542 651 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
543 652 cookieadmin_toggle_overlay();
544 653 if(!cookieadmin_is_obj(cookieadmin_is_consent)){
545 - document.getElementsByClassName(e.target.id)[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 + }
546 658 }else if(cookieadmin_show_reconsent){
547 - 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 + }
548 663 }
549 664 });
550 665
551 666 });
@@ -551,17 +666,22 @@
551 666 });
552 667
553 668 function cookieadmin_set_consent(prefrenc, days){
554 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 +
555 679 var cookieadmin_consent = prefrenc;
556 -
557 - if (typeof cookieadmin_pro_set_consent === "function") {
558 - consent_id = cookieadmin_pro_set_consent(prefrenc, days);
559 680
560 - if(consent_id){
561 - cookieadmin_is_consent.consent = consent_id;
562 - cookieadmin_consent['consent'] = consent_id;
563 - }
681 + if(consent_id){
682 + cookieadmin_is_consent.consent = consent_id;
683 + cookieadmin_consent['consent'] = consent_id;
564 684 }
565 685
566 686 if(!cookieadmin_is_consent.consent){
567 687 cookieadmin_is_consent.consent = "";
@@ -571,10 +691,19 @@
571 691 cookieadmin_is_consent.action = prefrenc;
572 692 cookieadmin_populate_preference();
573 693 cookieadmin_set_cookie('cookieadmin_consent', cookieadmin_consent, days);
574 694
695 + if (typeof cookieadmin_update_gcm === "function") {
696 + cookieadmin_update_gcm(1);
697 + }
698 +
699 + if (typeof cookieadmin_pro_update_clarity_cookie === "function") {
700 + cookieadmin_pro_update_clarity_cookie();
701 + }
702 +
703 + if(!!cookieadmin_policy.reload_on_consent){
704 + location.reload();
705 + }else{
706 + cookieadmin_restore_cookies(prefrenc);
707 + }
575 708 }
576 709
577 -function cookieadmin_get_base_path() {
578 - const parts = window.location.pathname.split('/');
579 - return '/' + (parts[1] || '') + '/';
580 -}