PluginProbe
CookieAdmin – Cookie Consent Banner / 1.2.5
CookieAdmin – Cookie Consent Banner v1.2.5
1.2.5 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 +183 -76 1.1.4 → 1.2.5 View file →
@@ -1,4 +1,6 @@
1 +// This file handle the cookie banner and stuff related to that.
2 +
1 3 var days = cookieadmin_policy.cookieadmin_days;
2 4
3 5 if(typeof cookieadmin_is_consent === 'undefined'){
4 6 window.cookieadmin_is_consent = {};
@@ -6,14 +8,23 @@
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 -
26 +
16 27 var originalCookieDescriptor =
17 28 Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
18 29 Object.getOwnPropertyDescriptor(document, 'cookie');
19 30 var allowed_cookies = '';
@@ -40,9 +51,10 @@
40 51 if(cookieName === "cookieadmin_consent"){
41 52 originalCookieDescriptor.set.call(document, val);
42 53 return;
43 54 }
44 -
55 +
56 + // Set cookies which are meant to be deleted
45 57 if(val.includes('expires=Thu, 01 Jan 1970') || cookieValue.startsWith("deleted;")){
46 58 originalCookieDescriptor.set.call(document, val);
47 59 return;
48 60 }
@@ -48,17 +60,22 @@
48 60 }
49 61
50 62 var cookieInfo = cookieadmin_allcookies[cookieName] || {};
51 63 var category = (cookieInfo.category || 'uncategorized').toLowerCase();
52 -
64 +
65 + // Set necessary cookies
53 66 if(category == "necessary"){
54 67 originalCookieDescriptor.set.call(document, val);
55 68 return;
56 69 }
57 70
58 - if(cookieadmin_is_obj(cookieadmin_is_consent)){
71 + if(cookieadmin_is_obj(cookieadmin_is_consent) || (Array.isArray(cookieadmin_policy['preload']) && cookieadmin_policy['preload'].length > 0)){
59 72
60 73 var consentAction = cookieadmin_is_consent.action;
74 +
75 + if(!consentAction){
76 + consentAction = cookieadmin_policy['preload'].reduce((a, val) => {a[val] = ''; return a;}, {});
77 + }
61 78
62 79 if(consentAction.accept || consentAction[category]){
63 80 originalCookieDescriptor.set.call(document, val);
64 81 }else{
@@ -86,18 +103,28 @@
86 103 function cookieadmin_is_cookie(name){
87 104
88 105 if(!document.cookie) return false;
89 106
90 - let coki = document.cookie.split(";") ;
107 + var coki = document.cookie.split(";") ;
91 108
92 109 if(name == "all"){
93 110 return coki ? coki : [];
94 111 }
95 - let nam = name + "=";
112 + var nam = name + "=";
96 113
97 - for(let i=0; i < coki.length; i++){
114 + for(var i=0; i < coki.length; i++){
98 115 if(coki[i].trim().indexOf(nam) == 0){
99 - 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 + }
100 127 }
101 128 }
102 129
103 130 return false;
@@ -103,11 +130,10 @@
103 130 return false;
104 131 }
105 132
106 133 function cookieadmin_check_consent(){
107 -
108 - if(cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent")){
109 - cookieadmin_cookie = JSON.parse(cookieadmin_cookie.split("=")[1]);
134 + var cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent");
135 + if(!!cookieadmin_cookie){
110 136 if(!!cookieadmin_cookie.consent){
111 137 cookieadmin_is_consent.consent = cookieadmin_cookie.consent;
112 138 delete cookieadmin_cookie.consent;
113 139 }
@@ -177,14 +203,23 @@
177 203
178 204 function cookieadmin_set_cookie(name, value, days = 365, domain = "") {
179 205 if (!name || !value) return false;
180 206
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 +
181 216 var date = new Date();
182 217 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); // default 1 year
183 218
184 - let cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
219 + var cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
185 220 cookieString += ` expires=${date.toUTCString()};`;
186 - cookieString += ` path=${cookieadmin_policy.base_path};`;
221 + cookieString += ` path=${!domain ? cookieadmin_policy.base_path : '/'};`;
187 222 cookieString += ` SameSite=Lax;`;
188 223 if(cookieadmin_policy.is_ssl || window.location.protocol === 'https:'){
189 224 cookieString += ` Secure;`;
190 225 }
@@ -197,9 +232,14 @@
197 232 document.cookie = cookieString;
198 233 return true;
199 234 }
200 235
201 -//Populates modal with consent if selected
236 +//Populates modal with consent if selected & adds found cookies
237 +// Only < and > are escaped: stored values pass sanitize_text_field, which can keep entities like &lt; that must still render
238 +function cookieadmin_esc(str){
239 + return String(str).replace(/</g, '&lt;').replace(/>/g, '&gt;');
240 +}
241 +
202 242 function cookieadmin_populate_preference(){
203 243
204 244 consent = cookieadmin_is_consent.action;
205 245
@@ -221,34 +261,62 @@
221 261 btn_ele.checked = true;
222 262 }
223 263 }
224 264 }
265 + //Load as per preloaded categories selected by admin
266 + }else if(cookieadmin_policy['preload'] && cookieadmin_policy['preload'].length > 0){
267 +
268 + cookieadmin_policy['preload'].forEach(function(val){
269 + if(btn_ele = document.querySelector("#cookieadmin-" + val)){
270 + btn_ele.checked = true;
271 + }
272 + });
225 273 }
226 274
227 - cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
275 + var cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
228 276
229 277 if(cookieadmin_allcookies){
230 278
231 - cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
279 + var cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
232 280
233 281 for(c_info of cookieadmin_filtrd){
234 282
235 - if(e = document.querySelector(".cookieadmin-" + cookieadmin_allcookies[c_info].category?.toLowerCase())){
236 - e.innerHTML = (e.innerHTML == 'None') ? '' : e.innerHTML;
283 + var category_var = cookieadmin_allcookies[c_info].category;
284 +
285 + if(!category_var){
286 + continue;
287 + }
288 + var card_container = document.querySelector(".cookieadmin-" + category_var.toLowerCase());
289 +
290 + if(!card_container){
291 + continue;
292 + }
293 +
294 + card_container.querySelector(".cookieadmin-nocookie-cat").style.display = 'none';
295 +
296 + var cookieadmin_exp = cookieadmin_policy.lang.session;
237 297
238 - exp = cookieadmin_policy.lang.session;
239 -
240 - if(!!cookieadmin_allcookies[c_info].expires){
241 - exp = Math.round((cookieadmin_allcookies[c_info].expires - Date.now())/86400);
242 - if(exp < 1 && !!res['Max-Age']){
243 - exp = res['Max-Age'];
244 - }else{
245 - exp = cookieadmin_policy.lang.session;
298 + if(
299 + !!cookieadmin_allcookies[c_info].expires
300 + && cookieadmin_allcookies[c_info].expires !== "0000-00-00 00:00:00"
301 + ){
302 +
303 + var expDate = new Date(cookieadmin_allcookies[c_info].expires.replace(' ', 'T'));
304 +
305 + if (!isNaN(expDate.getTime())) {
306 + var daysLeft = Math.ceil((expDate.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
307 +
308 + if (daysLeft > 0) {
309 + cookieadmin_exp = daysLeft +' '+ cookieadmin_policy.lang.days;
246 310 }
247 311 }
248 -
249 - 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>';
250 312 }
313 +
314 + var cookieadmin_desc = cookieadmin_allcookies[c_info].description;
315 + var cookieadmin_platform = cookieadmin_allcookies[c_info].platform;
316 +
317 + // Empty description/platform rows are skipped so the card has no blank gap or "null" text
318 + card_container.insertAdjacentHTML('beforeend', '<div class="cookieadmin-cookie-card"><div class="cookieadmin-cookie-header"><strong class="cookieadmin-cookie-name">'+ cookieadmin_esc(c_info.replace(/_+$/, "")) +'</strong> <span class="cookieadmin-cookie-duration"><b>'+ cookieadmin_policy.lang.duration +':</b> '+ cookieadmin_exp +'</span></div>' + (cookieadmin_desc ? '<p class="cookieadmin-cookie-description">'+ cookieadmin_esc(cookieadmin_desc) +'</p>' : '') + (cookieadmin_platform ? '<div class="cookieadmin-cookie-tags"><span class="cookieadmin-tag">' + cookieadmin_esc(cookieadmin_platform) + '</span></div>' : '') + '</div>');
251 319 cookieadmin_shown.push(c_info);
252 320 }
253 321 }
254 322
@@ -314,9 +382,9 @@
314 382
315 383 document.addEventListener("DOMContentLoaded", function() {
316 384
317 385 var cookieadmin_show_reconsent = 0;
318 - if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars.reconsent != 0){
386 + if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars !== 'undefined' && cookieadmin_pro_vars.reconsent != 0){
319 387 var cookieadmin_show_reconsent = 1;
320 388 }
321 389
322 390 //Create overlay
@@ -323,13 +391,19 @@
323 391 var cookieadmin_ovrlay = document.createElement("div");
324 392 cookieadmin_ovrlay.className = "cookieadmin_modal_overlay";
325 393 document.body.appendChild(cookieadmin_ovrlay);
326 394
395 + var before_consent_dispaly = new CustomEvent('cookieadmin_before_consent_display');
396 +
397 + // For anything that needs to be done before disaplying consent.
398 + cookieadmin_policy.hide_banner = false; // initializing
399 + window.dispatchEvent(before_consent_dispaly);
400 +
327 401 //Show notice or re-consent icon as needed
328 - if(!cookieadmin_is_obj(cookieadmin_is_consent)){
402 + if(!cookieadmin_is_obj(cookieadmin_is_consent) && !cookieadmin_policy.hide_banner){
329 403
330 404 if(cookieadmin_policy.cookieadmin_layout !== "popup"){
331 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
405 + document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
332 406 }else{
333 407 cookieadmin_toggle_overlay();
334 408 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
335 409 }
@@ -353,9 +427,8 @@
353 427
354 428 }
355 429
356 430 //Edit Notice and Modal contents
357 - document.getElementsByClassName("cookieadmin_reconsent_img")[0].src = cookieadmin_policy.plugin_url + "/assets/images/cookieadmin_icon.svg";
358 431
359 432 cookieadmin_populate_preference();
360 433
361 434 for(data in cookieadmin_policy){
@@ -400,9 +473,9 @@
400 473 }
401 474 }
402 475
403 476 //Add layout as class
404 - if(!!cookieadmin_policy.cookieadmin_position){
477 + if(!!cookieadmin_policy.cookieadmin_position && cookieadmin_policy.cookieadmin_layout !== "popup"){
405 478 cookieadmin_policy.cookieadmin_position.split("_").forEach(function(clas){
406 479 clas = "cookieadmin_" + clas;
407 480 document.getElementsByClassName("cookieadmin_law_container")[0].classList.add(clas);
408 481 });
@@ -408,9 +481,13 @@
408 481 });
409 482 }
410 483
411 484 // Change consent layout dynamically
412 - document.getElementsByClassName("cookieadmin_law_container")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
485 + // TODO: There is a operator called optional chaining operator ?. something like this users?.[0]?.name; wont use it now just for reference
486 + var cookieadmin_law_container_var = document.getElementsByClassName("cookieadmin_law_container")[0];
487 + if (!!cookieadmin_law_container_var){
488 + cookieadmin_law_container_var.classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
489 + }
413 490
414 491 // Change Modal layout dynamically
415 492 document.getElementsByClassName("cookieadmin_cookie_modal")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_modal);
416 493
@@ -417,11 +494,8 @@
417 494 /*if(cookieadmin_policy.layout == "footer"){
418 495
419 496 }*/
420 497
421 - if(cookieadmin_policy.cookieadmin_modal == "side"){
422 - document.getElementsByClassName("cookieadmin_modal_footer")[0].style.flexDirection = "column";
423 - }
424 498
425 499 // Remove modal close Button
426 500 if(cookieadmin_policy.cookieadmin_layout == "popup"){
427 501 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
@@ -426,8 +500,19 @@
426 500 if(cookieadmin_policy.cookieadmin_layout == "popup"){
427 501 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
428 502 }
429 503
504 + var cookieadmin_modal_opener = null;
505 +
506 + // Focus goes back to the reconsent/Customize button that opened the modal.
507 + // focus() on a hidden opener is a no-op, which is fine.
508 + function cookieadmin_return_focus(){
509 + if(cookieadmin_modal_opener){
510 + cookieadmin_modal_opener.focus();
511 + cookieadmin_modal_opener = null;
512 + }
513 + }
514 +
430 515 //show preference modal
431 516 cookieadmin_show_modal_elemnts = document.querySelectorAll(".cookieadmin_re_consent, .cookieadmin_customize_btn");
432 517 cookieadmin_show_modal_elemnts.forEach(function(e){
433 518
@@ -441,20 +526,24 @@
441 526 });*/
442 527
443 528 cookieadmin_toggle_overlay();
444 529 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
445 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "none";
446 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
530 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
531 + if(cookieadmin_re_consent){
532 + cookieadmin_re_consent.style.display = "none";
533 + }
534 +
535 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
536 + if(cookieadmin_law_container){
537 + cookieadmin_law_container.style.display = "none";
538 + }
447 539
448 540 if(cookieadmin_policy["cookieadmin_modal"] == "side"){
449 541 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "grid";
450 542 }
451 543
452 - if(e.target.className == "cookieadmin_re_consent"){
453 - document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_re_consent";
454 - }else{
455 - document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_law_container";
456 - }
544 + // Remember what opened the modal so focus can go back to it on close
545 + cookieadmin_modal_opener = e.currentTarget;
457 546 });
458 547 });
459 548
460 549 //Save preference
@@ -473,18 +562,35 @@
473 562 }
474 563 });
475 564
476 565 if(Object.keys(prefer).length !== 0){
477 - if(Object.keys(prefer).length === 3){
478 - document.querySelectorAll(".cookieadmin_accept_btn")[1].click();
566 + var override_gpc = document.getElementById('cookieadmin-override_gpc');
567 + var is_override_gpc = false;
568 + if(override_gpc && override_gpc.checked){
569 + is_override_gpc = true;
570 + }
571 +
572 + if(Object.keys(prefer).length === 3 && !is_override_gpc){
573 + let accept_btn = document.querySelectorAll(".cookieadmin_accept_btn");
574 +
575 + if(accept_btn.length > 0){
576 + accept_btn[accept_btn.length-1].click();
577 + }
578 +
479 579 return;
480 580 }
481 581 }else{
482 - document.querySelectorAll(".cookieadmin_reject_btn")[1].click();
582 + let reject_btn = document.querySelectorAll(".cookieadmin_reject_btn");
583 +
584 + if(reject_btn.length > 0){
585 + reject_btn[reject_btn.length-1].click();
586 + }
587 +
483 588 return;
484 589 }
485 590
486 591 cookieadmin_toggle_overlay();
592 + cookieadmin_return_focus();
487 593
488 594 cookieadmin_set_consent(prefer, days);
489 595 });
490 596
@@ -497,51 +603,40 @@
497 603 e.addEventListener("click", function(){
498 604 // console.log(e);
499 605
500 606 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
501 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
502 - if(cookieadmin_show_reconsent){
503 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
607 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
608 + if(cookieadmin_law_container){
609 + cookieadmin_law_container.style.display = "none";
504 610 }
505 611
612 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
613 + if(cookieadmin_re_consent){
614 + cookieadmin_re_consent.style.display = "block";
615 + }
616 +
506 617 if(e.id.includes("modal")){
507 618 cookieadmin_toggle_overlay();
619 + cookieadmin_return_focus();
508 620 }
509 621
510 622 var prefer2 = e.classList.contains("cookieadmin_reject_btn") ? {reject: "true"} : {accept: "true"};
511 -
623 +
512 624 cookieadmin_set_consent(prefer2, days);
513 625 });
514 626 });
515 627
516 - //showmore modal btn
517 - document.getElementsByClassName("cookieadmin_showmore")[0]?.addEventListener("click", function(){
518 -
519 - var cur_height = document.getElementsByClassName("cookieadmin_preference")[0].style.height;
520 -
521 - if(!cur_height){
522 - document.getElementsByClassName("cookieadmin_preference")[0].style.height = "auto";
523 - this.innerHTML = cookieadmin_policy.lang.show_less;
524 - }else{
525 - document.getElementsByClassName("cookieadmin_preference")[0].style.height = "";
526 - this.innerHTML = cookieadmin_policy.lang.show_more;
527 - }
528 - });
529 -
530 -
531 - document.querySelectorAll(".cookieadmin_show_pref_cookies").forEach(function(e){
628 + document.querySelectorAll(".cookieadmin_header .cookieadmin_show_pref_cookies").forEach(function(e){
532 629 e.addEventListener("click", function(el){
533 630
534 - var tgt = el.target.id;
631 + var tgt = el.currentTarget.id;
535 632 tgt = tgt.replace(/-container$/, "");
536 633
537 - if(el.target.classList.contains("dwn")){
538 - el.target.innerHTML = "&#9658;";
539 - el.target.classList.remove("dwn");
634 + if(el.currentTarget.classList.contains("dwn")){
635 + el.currentTarget.classList.remove("dwn");
540 636 document.querySelector("."+tgt).style.display = "none";
541 637 }else{
542 - el.target.innerHTML = "&#9660;";
543 - el.target.classList.add("dwn");
638 + el.currentTarget.classList.add("dwn");
544 639 document.querySelector("."+tgt).style.display = "block";
545 640 }
546 641 });
547 642 });
@@ -549,12 +644,20 @@
549 644 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
550 645 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
551 646 cookieadmin_toggle_overlay();
552 647 if(!cookieadmin_is_obj(cookieadmin_is_consent)){
553 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
648 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
649 + if(cookieadmin_law_container){
650 + cookieadmin_law_container.style.display = "block";
651 + }
554 652 }else if(cookieadmin_show_reconsent){
555 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
653 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
654 + if(cookieadmin_re_consent){
655 + cookieadmin_re_consent.style.display = "block";
656 + }
556 657 }
658 +
659 + cookieadmin_return_focus();
557 660 });
558 661
559 662 });
560 663
@@ -586,8 +689,12 @@
586 689 cookieadmin_set_cookie('cookieadmin_consent', cookieadmin_consent, days);
587 690
588 691 if (typeof cookieadmin_update_gcm === "function") {
589 692 cookieadmin_update_gcm(1);
693 + }
694 +
695 + if (typeof cookieadmin_pro_update_clarity_cookie === "function") {
696 + cookieadmin_pro_update_clarity_cookie();
590 697 }
591 698
592 699 if(!!cookieadmin_policy.reload_on_consent){
593 700 location.reload();