PluginProbe
CookieAdmin – Cookie Consent Banner / 1.1.5
CookieAdmin – Cookie Consent Banner v1.1.5
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
cookieadmin / assets / js / consent.js

consent.js in CookieAdmin – Cookie Consent Banner 1.1.5, at assets/js/consent.js

628 lines 19.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 // This file handle the cookie banner and stuff related to that.
2
3 var days = cookieadmin_policy.cookieadmin_days;
4
5 if(typeof cookieadmin_is_consent === 'undefined'){
6 window.cookieadmin_is_consent = {};
7 }
8 var cookieadmin_allcookies = cookieadmin_policy.categorized_cookies;
9 //setInterval(cookieadmin_categorize_cookies, 5000);
10
11 function cookieadmin_is_obj(consentObj){
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 }
22 }
23
24
25 // function cookieadmin_cookie_interceptor(){
26
27 var originalCookieDescriptor =
28 Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
29 Object.getOwnPropertyDescriptor(document, 'cookie');
30 var allowed_cookies = '';
31
32 // Override document.cookie to intercept cookie setting.
33 Object.defineProperty(document, 'cookie', {
34 configurable: true,
35 enumerable: true,
36 get: function(){
37 return originalCookieDescriptor.get.call(document);
38 },
39 set: function(val){
40
41 if (!val) return;
42
43 var separatorIndex = val.indexOf('=');
44 if(separatorIndex === -1) {
45 return;
46 }
47
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 }
55
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();
64
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;
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{
82
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 }
91
92 }else{
93 (cookieadmin_allcookies[cookieName] = cookieadmin_allcookies[cookieName] || {}).string = val.trim();
94 return false;
95 }
96
97 }
98 });
99 // }
100 // cookieadmin_cookie_interceptor();
101
102
103 function cookieadmin_is_cookie(name){
104
105 if(!document.cookie) return false;
106
107 var coki = document.cookie.split(";") ;
108
109 if(name == "all"){
110 return coki ? coki : [];
111 }
112 var nam = name + "=";
113
114 for(var i=0; i < coki.length; i++){
115 if(coki[i].trim().indexOf(nam) == 0){
116 return coki[i].trim();
117 }
118 }
119
120 return false;
121 }
122
123 function cookieadmin_check_consent(){
124 var cookieadmin_cookie = cookieadmin_is_cookie("cookieadmin_consent")
125 if(!!cookieadmin_cookie){
126 cookieadmin_cookie = JSON.parse(cookieadmin_cookie.split("=")[1]);
127 if(!!cookieadmin_cookie.consent){
128 cookieadmin_is_consent.consent = cookieadmin_cookie.consent;
129 delete cookieadmin_cookie.consent;
130 }
131
132 cookieadmin_is_consent.action = cookieadmin_cookie;
133 }
134 }
135 cookieadmin_check_consent();
136
137 function cookieadmin_restore_cookies(update) {
138
139 var cookieadmin_accepted_categories = [];
140
141 if(update.accept && update.accept == "true"){
142
143 document.querySelectorAll(".cookieadmin_toggle").forEach(function(e){
144 key = e.children[0].id;
145 if (key.includes("cookieadmin-")) {
146 key = key.replace("cookieadmin-", "");
147 cookieadmin_accepted_categories.push(key);
148 }
149 });
150
151 }else if(update.reject && update.reject == "true"){
152 return true;
153 }else{
154 for (var [key, value] of Object.entries(update)) {
155 if(key != "consent"){
156 cookieadmin_accepted_categories.push(key);
157 }
158 }
159 }
160
161
162 for(cookie in cookieadmin_allcookies){
163 document.cookie = cookieadmin_allcookies[cookie].string;
164 };
165
166 cookieadmin_accepted_categories.forEach(function(category) {
167
168 document.querySelectorAll(
169 'script[type="text/plain"][data-cookieadmin-category="' + category + '"]'
170 ).forEach(function(el) {
171 var newScript = document.createElement('script');
172
173 // Copy attributes
174 if (el.src) {
175 newScript.src = el.src;
176 } else {
177 newScript.text = el.textContent;
178 }
179
180 if (el.defer) newScript.defer = true;
181 if (el.async) newScript.async = true;
182
183 // Copy other attributes if needed
184 ['id', 'class', 'data-name'].forEach(attr => {
185 if (el.hasAttribute(attr)) {
186 newScript.setAttribute(attr, el.getAttribute(attr));
187 }
188 });
189
190 el.parentNode.replaceChild(newScript, el);
191 });
192 });
193 }
194
195 function cookieadmin_set_cookie(name, value, days = 365, domain = "") {
196 if (!name || !value) return false;
197
198 var date = new Date();
199 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); // default 1 year
200
201 var cookieString = `${encodeURIComponent(name)}=${JSON.stringify(value)};`;
202 cookieString += ` expires=${date.toUTCString()};`;
203 cookieString += ` path=${cookieadmin_policy.base_path};`;
204 cookieString += ` SameSite=Lax;`;
205 if(cookieadmin_policy.is_ssl || window.location.protocol === 'https:'){
206 cookieString += ` Secure;`;
207 }
208
209 // Add domain if explicitly passed
210 if (domain) {
211 cookieString += ` domain=${domain};`;
212 }
213
214 document.cookie = cookieString;
215 return true;
216 }
217
218 //Populates modal with consent if selected & adds found cookies
219 function cookieadmin_populate_preference(){
220
221 consent = cookieadmin_is_consent.action;
222
223 if(!!consent){
224
225 if(consent.accept){
226 document.querySelectorAll(".cookieadmin_toggle").forEach(function(e){
227 e.children[0].checked = true;
228 });
229 }
230 else if(consent.reject){
231 document.querySelectorAll(".cookieadmin_toggle").forEach(function(e){
232 e.children[0].checked = false;
233 });
234 }
235 else{
236 for(btn in consent){
237 if(btn_ele = document.querySelector("#cookieadmin-" + btn)){
238 btn_ele.checked = true;
239 }
240 }
241 }
242 //Load as per preloaded categories selected by admin
243 }else if(cookieadmin_policy['preload'] && cookieadmin_policy['preload'].length > 0){
244
245 cookieadmin_policy['preload'].forEach(function(val){
246 if(btn_ele = document.querySelector("#cookieadmin-" + val)){
247 btn_ele.checked = true;
248 }
249 });
250 }
251
252 var cookieadmin_shown = (typeof cookieadmin_shown !== "undefined") ? cookieadmin_shown : [];
253
254 if(cookieadmin_allcookies){
255
256 var cookieadmin_filtrd = Object.keys(cookieadmin_allcookies).filter(e => !cookieadmin_shown.includes(e));
257
258 for(c_info of cookieadmin_filtrd){
259
260 var category_var = cookieadmin_allcookies[c_info].category;
261
262 if(!category_var){
263 continue;
264 }
265 var card_container = document.querySelector(".cookieadmin-" + category_var.toLowerCase());
266
267 if(!card_container){
268 continue;
269 }
270 card_container.innerHTML = (card_container.innerHTML == 'None') ? '' : card_container.innerHTML;
271
272 var cookieadmin_exp = cookieadmin_policy.lang.session;
273
274 if(
275 !!cookieadmin_allcookies[c_info].expires
276 && cookieadmin_allcookies[c_info].expires !== "0000-00-00 00:00:00"
277 ){
278
279 var expDate = new Date(cookieadmin_allcookies[c_info].expires.replace(' ', 'T'));
280
281 if (!isNaN(expDate.getTime())) {
282 var daysLeft = Math.ceil((expDate.getTime() - Date.now()) / (1000 * 60 * 60 * 24));
283
284 if (daysLeft > 0) {
285 cookieadmin_exp = daysLeft +' '+ cookieadmin_policy.lang.days;
286 }
287 }
288 }
289
290 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>';
291 cookieadmin_shown.push(c_info);
292 }
293 }
294
295 }
296
297 function cookieadmin_toggle_overlay(){
298
299 if(window.getComputedStyle(document.getElementsByClassName("cookieadmin_modal_overlay")[0]).display == "none"){
300 document.getElementsByClassName("cookieadmin_modal_overlay")[0].style.display = "block";
301 }else{
302 document.getElementsByClassName("cookieadmin_modal_overlay")[0].style.display = "none";
303 }
304
305 }
306
307 function cookieadmin_categorize_cookies(){
308
309 if(!cookieadmin_allcookies){
310 return;
311 }
312
313 var cookieadmin_chk_cookies = {};
314 var cookieadmin_consent_chng = [];
315
316 for(a_cookie in cookieadmin_allcookies){
317 if(!cookieadmin_allcookies[a_cookie].category){
318 cookieadmin_chk_cookies[a_cookie] = cookieadmin_allcookies[a_cookie];
319 }else if(cookieadmin_is_consent.old_action !== cookieadmin_is_consent.action && a_cookie !== "cookieadmin_consent"){
320 document.cookie = cookieadmin_allcookies[a_cookie].string;
321 }
322 }
323
324 if(!cookieadmin_is_obj(cookieadmin_chk_cookies)){
325 return;
326 }
327
328 /* var xhttp2 = new XMLHttpRequest();
329
330 var data = 'action=cookieadmin_ajax_handler&cookieadmin_act=categorize_cookies&cookieadmin_security=' + cookieadmin_policy.nonce + "&cookieadmin_cookies=" + JSON.stringify(cookieadmin_chk_cookies);
331
332 xhttp2.onload = function() {
333 parsd = JSON.parse(this.responseText);
334
335 if(parsd.success){
336 cookies = parsd.data;
337 for(coki in cookies){
338 cookieadmin_chk_cookies[coki].name = coki;
339 if(cookies[coki].category === "un_c"){
340 cookieadmin_chk_cookies[coki].source = "unknown";
341 cookieadmin_chk_cookies[coki].description = "unknown";
342 }
343 cookieadmin_allcookies[coki] = cookieadmin_chk_cookies[coki];
344 document.cookie = cookieadmin_chk_cookies[coki].string;
345 }
346 }
347 }
348
349 xhttp2.open("POST", cookieadmin_policy.ajax_url, true);
350 xhttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
351 xhttp2.send(data); */
352 }
353
354
355 document.addEventListener("DOMContentLoaded", function() {
356
357 var cookieadmin_show_reconsent = 0;
358 if(cookieadmin_policy.is_pro != 0 && cookieadmin_pro_vars.reconsent != 0){
359 var cookieadmin_show_reconsent = 1;
360 }
361
362 //Create overlay
363 var cookieadmin_ovrlay = document.createElement("div");
364 cookieadmin_ovrlay.className = "cookieadmin_modal_overlay";
365 document.body.appendChild(cookieadmin_ovrlay);
366
367 //Show notice or re-consent icon as needed
368 if(!cookieadmin_is_obj(cookieadmin_is_consent)){
369
370 if(cookieadmin_policy.cookieadmin_layout !== "popup"){
371 document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
372 }else{
373 cookieadmin_toggle_overlay();
374 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
375 }
376
377 /* //block cookie scripts
378 var cookieadmin_blockedScripts = [
379 'https://www.google-analytics.com/analytics.js',
380 'https://connect.facebook.net/en_US/fbevents.js',
381 'https://www.youtube.com/iframe_api'
382 ];
383
384 cookieadmin_blockedScripts.forEach(function(scriptUrl) {
385 var scriptTag = document.querySelector(`script[src='${scriptUrl}']`);
386 if (scriptTag) {
387 scriptTag.remove(); // Remove script if already loaded
388 }
389 }); */
390
391 }else if(cookieadmin_show_reconsent){
392 document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
393
394 }
395
396 //Edit Notice and Modal contents
397 document.getElementsByClassName("cookieadmin_reconsent_img")[0].src = cookieadmin_policy.plugin_url + "/assets/images/cookieadmin_icon.svg";
398
399 cookieadmin_populate_preference();
400
401 for(data in cookieadmin_policy){
402
403 typ = 0;
404 if(data.includes("_bg_color")){
405 d_ele = data.replace("_bg_color", "");
406 typ = 1;
407 }else if(data.includes("_border_color")){
408 d_ele = data.replace("_border_color", "");
409 typ = 2;
410 }else if(data.includes("_color")){
411 d_ele = data.replace("_color", "");
412 typ = 3;
413 }else{
414 d_ele = data;
415 }
416
417 d_eles = [];
418 if(document.getElementById(d_ele)){
419 d_eles = [document.getElementById(d_ele)];
420 }
421 if(document.getElementsByClassName(d_ele).length){
422 d_eles = (document.getElementsByClassName(d_ele).length > 1) ? document.getElementsByClassName(d_ele) : [document.getElementsByClassName(d_ele)[0]];
423 }
424
425 if(!!d_eles){
426 i = 0;
427 while(i < d_eles.length){
428 d_ele = d_eles[i];
429 if(typ == 3){
430 d_ele.style.color = cookieadmin_policy[data];
431 }else if(typ == 2){
432 d_ele.style.borderColor = cookieadmin_policy[data];
433 }else if(typ == 1){
434 d_ele.style.backgroundColor = cookieadmin_policy[data];
435 }else{
436 d_ele.innerHTML = cookieadmin_policy[data];
437 }
438 i++;
439 }
440 }
441 }
442
443 //Add layout as class
444 if(!!cookieadmin_policy.cookieadmin_position && cookieadmin_policy.cookieadmin_layout !== "popup"){
445 cookieadmin_policy.cookieadmin_position.split("_").forEach(function(clas){
446 clas = "cookieadmin_" + clas;
447 document.getElementsByClassName("cookieadmin_law_container")[0].classList.add(clas);
448 });
449 }
450
451 // Change consent layout dynamically
452 // TODO: There is a operator called optional chaining operator ?. something like this users?.[0]?.name; wont use it now just for reference
453 var cookieadmin_law_container_var = document.getElementsByClassName("cookieadmin_law_container")[0];
454 if (!!cookieadmin_law_container_var){
455 cookieadmin_law_container_var.classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_layout);
456 }
457
458 // Change Modal layout dynamically
459 document.getElementsByClassName("cookieadmin_cookie_modal")[0].classList.add("cookieadmin_" + cookieadmin_policy.cookieadmin_modal);
460
461 /*if(cookieadmin_policy.layout == "footer"){
462
463 }*/
464
465 if(cookieadmin_policy.cookieadmin_modal == "side"){
466 document.getElementsByClassName("cookieadmin_modal_footer")[0].style.flexDirection = "column";
467 }
468
469 // Remove modal close Button
470 if(cookieadmin_policy.cookieadmin_layout == "popup"){
471 document.getElementsByClassName("cookieadmin_close_pref")[0].style.display = "none";
472 }
473
474 //show preference modal
475 cookieadmin_show_modal_elemnts = document.querySelectorAll(".cookieadmin_re_consent, .cookieadmin_customize_btn");
476 cookieadmin_show_modal_elemnts.forEach(function(e){
477
478 e.addEventListener("click", function(e){
479
480 /*cookieadmin_is_cookie("all").forEach(function(e){
481 c_name = e.split("=")[0].trim();
482 if(!!cookieadmin_allcookies[c_name]){
483 console.log(JSON.stringify(cookieadmin_allcookies[c_name]));
484 }
485 });*/
486
487 cookieadmin_toggle_overlay();
488 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "flex";
489 document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "none";
490 document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
491
492 if(cookieadmin_policy["cookieadmin_modal"] == "side"){
493 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "grid";
494 }
495
496 if(e.target.className == "cookieadmin_re_consent"){
497 document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_re_consent";
498 }else{
499 document.getElementsByClassName("cookieadmin_close_pref")[0].id = "cookieadmin_law_container";
500 }
501 });
502 });
503
504 //Save preference
505 document.querySelector(".cookieadmin_save_btn").addEventListener("click", function(){
506
507 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
508 if(cookieadmin_show_reconsent){
509 document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
510 }
511
512 var prefer = {};
513
514 document.querySelectorAll(".cookieadmin_toggle").forEach(function(e){
515 if(!!e.children[0].checked){
516 prefer[e.children[0].id.replace("cookieadmin-","")] = 'true';
517 }
518 });
519
520 if(Object.keys(prefer).length !== 0){
521 if(Object.keys(prefer).length === 3){
522 document.querySelectorAll(".cookieadmin_accept_btn")[1].click();
523 return;
524 }
525 }else{
526 document.querySelectorAll(".cookieadmin_reject_btn")[1].click();
527 return;
528 }
529
530 cookieadmin_toggle_overlay();
531
532 cookieadmin_set_consent(prefer, days);
533 });
534
535
536 //Accept or reject all cookies
537 cookieadmin_save_all_cookie_elemnts = document.querySelectorAll(".cookieadmin_accept_btn, .cookieadmin_reject_btn");
538
539 cookieadmin_save_all_cookie_elemnts.forEach(function(e){
540
541 e.addEventListener("click", function(){
542 // console.log(e);
543
544 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
545 document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "none";
546 if(cookieadmin_show_reconsent){
547 document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
548 }
549
550 if(e.id.includes("modal")){
551 cookieadmin_toggle_overlay();
552 }
553
554 var prefer2 = e.classList.contains("cookieadmin_reject_btn") ? {reject: "true"} : {accept: "true"};
555
556 cookieadmin_set_consent(prefer2, days);
557 });
558 });
559
560 document.querySelectorAll(".cookieadmin_show_pref_cookies").forEach(function(e){
561 e.addEventListener("click", function(el){
562
563 var tgt = el.target.id;
564 tgt = tgt.replace(/-container$/, "");
565
566 if(el.target.classList.contains("dwn")){
567 el.target.innerHTML = "&#9658;";
568 el.target.classList.remove("dwn");
569 document.querySelector("."+tgt).style.display = "none";
570 }else{
571 el.target.innerHTML = "&#9660;";
572 el.target.classList.add("dwn");
573 document.querySelector("."+tgt).style.display = "block";
574 }
575 });
576 });
577
578 document.getElementsByClassName("cookieadmin_close_pref")[0].addEventListener("click", function(e){
579 document.getElementsByClassName("cookieadmin_cookie_modal")[0].style.display = "none";
580 cookieadmin_toggle_overlay();
581 if(!cookieadmin_is_obj(cookieadmin_is_consent)){
582 document.getElementsByClassName("cookieadmin_law_container")[0].style.display = "block";
583 }else if(cookieadmin_show_reconsent){
584 document.getElementsByClassName("cookieadmin_re_consent")[0].style.display = "block";
585 }
586 });
587
588 });
589
590 function cookieadmin_set_consent(prefrenc, days){
591
592 if (typeof cookieadmin_pro_set_consent === "function") {
593 return cookieadmin_pro_set_consent(prefrenc, days);
594 }else{
595 return cookieadmin_save_consent_cookie(prefrenc, days);
596 }
597 }
598
599 function cookieadmin_save_consent_cookie(prefrenc, days, consent_id){
600
601 var cookieadmin_consent = prefrenc;
602
603 if(consent_id){
604 cookieadmin_is_consent.consent = consent_id;
605 cookieadmin_consent['consent'] = consent_id;
606 }
607
608 if(!cookieadmin_is_consent.consent){
609 cookieadmin_is_consent.consent = "";
610 }
611
612 cookieadmin_is_consent["old_action"] = cookieadmin_is_consent.action ? cookieadmin_is_consent.action : {};
613 cookieadmin_is_consent.action = prefrenc;
614 cookieadmin_populate_preference();
615 cookieadmin_set_cookie('cookieadmin_consent', cookieadmin_consent, days);
616
617 if (typeof cookieadmin_update_gcm === "function") {
618 cookieadmin_update_gcm(1);
619 }
620
621 if(!!cookieadmin_policy.reload_on_consent){
622 location.reload();
623 }else{
624 cookieadmin_restore_cookies(prefrenc);
625 }
626 }
627
628