PluginProbe
CookieAdmin – Cookie Consent Banner / 1.2.6
CookieAdmin – Cookie Consent Banner v1.2.6
1.2.6 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 All 26 releases
← All changes | assets/js/consent.js +185 -77 1.1.4 → 1.2.6 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,17 @@
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 +
242 +// Cookies already rendered in the modal; must live outside the function or each re-populate duplicates the cards
243 +var cookieadmin_shown = [];
244 +
202 245 function cookieadmin_populate_preference(){
203 246
204 247 consent = cookieadmin_is_consent.action;
205 248
@@ -221,34 +264,60 @@
221 264 btn_ele.checked = true;
222 265 }
223 266 }
224 267 }
268 + //Load as per preloaded categories selected by admin
269 + }else if(cookieadmin_policy['preload'] && cookieadmin_policy['preload'].length > 0){
270 +
271 + cookieadmin_policy['preload'].forEach(function(val){
272 + if(btn_ele = document.querySelector("#cookieadmin-" + val)){
273 + btn_ele.checked = true;
274 + }
275 + });
225 276 }
226 277
227 - cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
228 -
229 278 if(cookieadmin_allcookies){
230 279
231 - cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
280 + var cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
232 281
233 282 for(c_info of cookieadmin_filtrd){
234 283
235 - if(e = document.querySelector(".cookieadmin-" + cookieadmin_allcookies[c_info].category?.toLowerCase())){
236 - e.innerHTML = (e.innerHTML == 'None') ? '' : e.innerHTML;
284 + var category_var = cookieadmin_allcookies[c_info].category;
285 +
286 + if(!category_var){
287 + continue;
288 + }
289 + var card_container = document.querySelector(".cookieadmin-" + category_var.toLowerCase());
290 +
291 + if(!card_container){
292 + continue;
293 + }
294 +
295 + card_container.querySelector(".cookieadmin-nocookie-cat").style.display = 'none';
296 +
297 + var cookieadmin_exp = cookieadmin_policy.lang.session;
237 298
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;
299 + if(
300 + !!cookieadmin_allcookies[c_info].expires
301 + && cookieadmin_allcookies[c_info].expires !== "0000-00-00 00:00:00"
302 + ){
303 +
304 + var expDate = new Date(cookieadmin_allcookies[c_info].expires.replace(' ', 'T'));
305 +
306 + if (!isNaN(expDate.getTime())) {
307 + var daysLeft = Math.ceil((expDate.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
308 +
309 + if (daysLeft > 0) {
310 + cookieadmin_exp = daysLeft +' '+ cookieadmin_policy.lang.days;
246 311 }
247 312 }
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 313 }
314 +
315 + var cookieadmin_desc = cookieadmin_allcookies[c_info].description;
316 + var cookieadmin_platform = cookieadmin_allcookies[c_info].platform;
317 +
318 + // Empty description/platform rows are skipped so the card has no blank gap or "null" text
319 + 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 320 cookieadmin_shown.push(c_info);
252 321 }
253 322 }
254 323
@@ -314,9 +383,9 @@
314 383
315 384 document.addEventListener("DOMContentLoaded", function() {
316 385
317 386 var cookieadmin_show_reconsent = 0;
318 - if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars.reconsent != 0){
387 + if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars !== 'undefined' && cookieadmin_pro_vars.reconsent != 0){
319 388 var cookieadmin_show_reconsent = 1;
320 389 }
321 390
322 391 //Create overlay
@@ -323,13 +392,19 @@
323 392 var cookieadmin_ovrlay = document.createElement("div");
324 393 cookieadmin_ovrlay.className = "cookieadmin_modal_overlay";
325 394 document.body.appendChild(cookieadmin_ovrlay);
326 395
396 + var before_consent_dispaly = new CustomEvent('cookieadmin_before_consent_display');
397 +
398 + // For anything that needs to be done before disaplying consent.
399 + cookieadmin_policy.hide_banner = false; // initializing
400 + window.dispatchEvent(before_consent_dispaly);
401 +
327 402 //Show notice or re-consent icon as needed
328 - if(!cookieadmin_is_obj(cookieadmin_is_consent)){
403 + if(!cookieadmin_is_obj(cookieadmin_is_consent) && !cookieadmin_policy.hide_banner){
329 404
330 405 if(cookieadmin_policy.cookieadmin_layout !== "popup"){
331 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
406 + document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
332 407 }else{
333 408 cookieadmin_toggle_overlay();
334 409 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
335 410 }
@@ -353,9 +428,8 @@
353 428
354 429 }
355 430
356 431 //Edit Notice and Modal contents
357 - document.getElementsByClassName("cookieadmin_reconsent_img")[0].src = cookieadmin_policy.plugin_url + "/assets/images/cookieadmin_icon.svg";
358 432
359 433 cookieadmin_populate_preference();
360 434
361 435 for(data in cookieadmin_policy){
@@ -400,9 +474,9 @@
400 474 }
401 475 }
402 476
403 477 //Add layout as class
404 - if(!!cookieadmin_policy.cookieadmin_position){
478 + if(!!cookieadmin_policy.cookieadmin_position && cookieadmin_policy.cookieadmin_layout !== "popup"){
405 479 cookieadmin_policy.cookieadmin_position.split("_").forEach(function(clas){
406 480 clas = "cookieadmin_" + clas;
407 481 document.getElementsByClassName("cookieadmin_law_container")[0].classList.add(clas);
408 482 });
@@ -408,9 +482,13 @@
408 482 });
409 483 }
410 484
411 485 // Change consent layout dynamically
412 - document.getElementsByClassName("cookieadmin_law_container")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
486 + // TODO: There is a operator called optional chaining operator ?. something like this users?.[0]?.name; wont use it now just for reference
487 + var cookieadmin_law_container_var = document.getElementsByClassName("cookieadmin_law_container")[0];
488 + if (!!cookieadmin_law_container_var){
489 + cookieadmin_law_container_var.classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
490 + }
413 491
414 492 // Change Modal layout dynamically
415 493 document.getElementsByClassName("cookieadmin_cookie_modal")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_modal);
416 494
@@ -417,11 +495,8 @@
417 495 /*if(cookieadmin_policy.layout == "footer"){
418 496
419 497 }*/
420 498
421 - if(cookieadmin_policy.cookieadmin_modal == "side"){
422 - document.getElementsByClassName("cookieadmin_modal_footer")[0].style.flexDirection = "column";
423 - }
424 499
425 500 // Remove modal close Button
426 501 if(cookieadmin_policy.cookieadmin_layout == "popup"){
427 502 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
@@ -426,8 +501,19 @@
426 501 if(cookieadmin_policy.cookieadmin_layout == "popup"){
427 502 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
428 503 }
429 504
505 + var cookieadmin_modal_opener = null;
506 +
507 + // Focus goes back to the reconsent/Customize button that opened the modal.
508 + // focus() on a hidden opener is a no-op, which is fine.
509 + function cookieadmin_return_focus(){
510 + if(cookieadmin_modal_opener){
511 + cookieadmin_modal_opener.focus();
512 + cookieadmin_modal_opener = null;
513 + }
514 + }
515 +
430 516 //show preference modal
431 517 cookieadmin_show_modal_elemnts = document.querySelectorAll(".cookieadmin_re_consent, .cookieadmin_customize_btn");
432 518 cookieadmin_show_modal_elemnts.forEach(function(e){
433 519
@@ -441,20 +527,24 @@
441 527 });*/
442 528
443 529 cookieadmin_toggle_overlay();
444 530 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";
531 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
532 + if(cookieadmin_re_consent){
533 + cookieadmin_re_consent.style.display = "none";
534 + }
535 +
536 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
537 + if(cookieadmin_law_container){
538 + cookieadmin_law_container.style.display = "none";
539 + }
447 540
448 541 if(cookieadmin_policy["cookieadmin_modal"] == "side"){
449 542 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "grid";
450 543 }
451 544
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 - }
545 + // Remember what opened the modal so focus can go back to it on close
546 + cookieadmin_modal_opener = e.currentTarget;
457 547 });
458 548 });
459 549
460 550 //Save preference
@@ -473,18 +563,35 @@
473 563 }
474 564 });
475 565
476 566 if(Object.keys(prefer).length !== 0){
477 - if(Object.keys(prefer).length === 3){
478 - document.querySelectorAll(".cookieadmin_accept_btn")[1].click();
567 + var override_gpc = document.getElementById('cookieadmin-override_gpc');
568 + var is_override_gpc = false;
569 + if(override_gpc && override_gpc.checked){
570 + is_override_gpc = true;
571 + }
572 +
573 + if(Object.keys(prefer).length === 3 && !is_override_gpc){
574 + let accept_btn = document.querySelectorAll(".cookieadmin_accept_btn");
575 +
576 + if(accept_btn.length > 0){
577 + accept_btn[accept_btn.length-1].click();
578 + }
579 +
479 580 return;
480 581 }
481 582 }else{
482 - document.querySelectorAll(".cookieadmin_reject_btn")[1].click();
583 + let reject_btn = document.querySelectorAll(".cookieadmin_reject_btn");
584 +
585 + if(reject_btn.length > 0){
586 + reject_btn[reject_btn.length-1].click();
587 + }
588 +
483 589 return;
484 590 }
485 591
486 592 cookieadmin_toggle_overlay();
593 + cookieadmin_return_focus();
487 594
488 595 cookieadmin_set_consent(prefer, days);
489 596 });
490 597
@@ -497,51 +604,40 @@
497 604 e.addEventListener("click", function(){
498 605 // console.log(e);
499 606
500 607 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";
608 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
609 + if(cookieadmin_law_container){
610 + cookieadmin_law_container.style.display = "none";
504 611 }
505 612
613 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
614 + if(cookieadmin_re_consent){
615 + cookieadmin_re_consent.style.display = "block";
616 + }
617 +
506 618 if(e.id.includes("modal")){
507 619 cookieadmin_toggle_overlay();
620 + cookieadmin_return_focus();
508 621 }
509 622
510 623 var prefer2 = e.classList.contains("cookieadmin_reject_btn") ? {reject: "true"} : {accept: "true"};
511 -
624 +
512 625 cookieadmin_set_consent(prefer2, days);
513 626 });
514 627 });
515 628
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){
629 + document.querySelectorAll(".cookieadmin_header .cookieadmin_show_pref_cookies").forEach(function(e){
532 630 e.addEventListener("click", function(el){
533 631
534 - var tgt = el.target.id;
632 + var tgt = el.currentTarget.id;
535 633 tgt = tgt.replace(/-container$/, "");
536 634
537 - if(el.target.classList.contains("dwn")){
538 - el.target.innerHTML = "&#9658;";
539 - el.target.classList.remove("dwn");
635 + if(el.currentTarget.classList.contains("dwn")){
636 + el.currentTarget.classList.remove("dwn");
540 637 document.querySelector("."+tgt).style.display = "none";
541 638 }else{
542 - el.target.innerHTML = "&#9660;";
543 - el.target.classList.add("dwn");
639 + el.currentTarget.classList.add("dwn");
544 640 document.querySelector("."+tgt).style.display = "block";
545 641 }
546 642 });
547 643 });
@@ -549,12 +645,20 @@
549 645 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
550 646 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
551 647 cookieadmin_toggle_overlay();
552 648 if(!cookieadmin_is_obj(cookieadmin_is_consent)){
553 - document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
649 + var cookieadmin_law_container = document.getElementsByClassName("cookieadmin_law_container")[0];
650 + if(cookieadmin_law_container){
651 + cookieadmin_law_container.style.display = "block";
652 + }
554 653 }else if(cookieadmin_show_reconsent){
555 - document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
654 + var cookieadmin_re_consent = document.getElementsByClassName("cookieadmin_re_consent")[0];
655 + if(cookieadmin_re_consent){
656 + cookieadmin_re_consent.style.display = "block";
657 + }
556 658 }
659 +
660 + cookieadmin_return_focus();
557 661 });
558 662
559 663 });
560 664
@@ -586,8 +690,12 @@
586 690 cookieadmin_set_cookie('cookieadmin_consent', cookieadmin_consent, days);
587 691
588 692 if (typeof cookieadmin_update_gcm === "function") {
589 693 cookieadmin_update_gcm(1);
694 + }
695 +
696 + if (typeof cookieadmin_pro_update_clarity_cookie === "function") {
697 + cookieadmin_pro_update_clarity_cookie();
590 698 }
591 699
592 700 if(!!cookieadmin_policy.reload_on_consent){
593 701 location.reload();