1/*! 2 * CookieConsent v2.2 3 * https://www.github.com/orestbida/cookieconsent 4 * Author Orest Bida 5 * Released under the MIT License 6 */ 7(function(){ 8 'use strict'; 9 var CookieConsent = function(){ 10 var _cookieconsent = {}; 11 var consent_modal_exists = false; 12 var cookie_consent_accepted = false; 13 14 // variables to save main dom elements (to avoid retrieving them later using document.getElementById) 15 var html_dom = document.documentElement; 16 var main_container = null; 17 var consent_modal = null; 18 var settings_container = null; 19 20 // Array of booleans to keep track of enabled/disabled preferences 21 var toggle_states = []; 22 23 //default cookieConsent config settings (some are implicit, not shown here) 24 var _config = { 25 autorun: true, // run as soon as loaded 26 delay: 0, // default milliseconds delay 27 cookie_expiration : 182, // default: 6 months (in days) 28 }; 29 30 /** 31 * Update config settings 32 * @param {Object} conf_params 33 */ 34 var _setConfig = function(conf_params){ 35 _log("CookieConsent [CONFIG]: recieved_config_settings ", conf_params); 36 37 if(typeof conf_params['cookie_expiration'] === "number"){ 38 _config.cookie_expiration = conf_params['cookie_expiration']; 39 } 40 41 if(typeof conf_params['autorun'] === "boolean"){ 42 _config.autorun = conf_params['autorun']; 43 } 44 } 45 46 /** 47 * Search for all occurrences in webpage and add an onClick listener : 48 * when clicked => open settings modal 49 */ 50 var _addCookieSettingsButtonListener = function(){ 51 var all_links = document.querySelectorAll('a[data-cc="c-settings"], button[data-cc="c-settings"]'); 52 for(var x=0; x<all_links.length; x++){ 53 _addEvent(all_links[x], 'click', function(event){ 54 event.preventDefault ? event.preventDefault() : event.returnValue = false; 55 _cookieconsent.showSettings(0); 56 }); 57 } 58 } 59 60 /** 61 * Generate cookie consent html based on config settings 62 */ 63 var _createCookieConsentHTML = function(never_accepted, conf_params){ 64 65 // Create main container which holds both consent modal & settings modal 66 main_container = document.createElement('div'); 67 main_container.setAttribute('c_data', 'c_cookie_main'); 68 69 // Fix layout flash 70 main_container.style.position = "fixed"; 71 main_container.style.zIndex = "1000000"; 72 main_container.innerHTML = '<!--[if lt IE 9 ]><div id="cookieConsentContainer" class="ie"></div><![endif]--><!--[if (gt IE 8)|!(IE)]><!--><div id="cookieConsentContainer"></div><!--<![endif]-->' 73 var all_modals_container = main_container.children[0]; 74 75 // If never_accepted => create consent-modal 76 if(!never_accepted){ 77 78 consent_modal = document.createElement("div"); 79 var consent_modal_inner = document.createElement('div'); 80 var consent_title = document.createElement("h1"); 81 var consent_text = document.createElement("p"); 82 var consent_buttons = document.createElement("div"); 83 var consent_primary_btn = document.createElement("button"); 84 var consent_secondary_btn = document.createElement("button"); 85 86 // Set for each of them, their default configured ids 87 consent_modal.id = 'cm'; 88 consent_modal_inner.id = 'cm_inner'; 89 consent_modal_inner.classList.add("col-xl-6"); 90 consent_modal_inner.classList.add("col-lg-6"); 91 consent_modal_inner.classList.add("col-md-6"); 92 consent_modal_inner.classList.add("col-sm-12"); 93 consent_modal_inner.classList.add("col-xs-12"); 94 consent_title.id = 'cm_title'; 95 consent_text.id = 'cm_text'; 96 consent_buttons.id = "cm_btns"; 97 consent_primary_btn.id = 'cm_primary_btn'; 98 consent_secondary_btn.id = 'cm_secondary_btn'; 99 consent_primary_btn.setAttribute('type', 'button'); 100 consent_primary_btn.className = "cc-accept"; 101 consent_secondary_btn.className = "cc-default"; 102 consent_modal.style.visibility = "hidden"; 103 consent_secondary_btn.setAttribute('type', 'button'); 104 105 consent_title.innerHTML = conf_params.modals['consent_modal']['title']; 106 consent_text.innerHTML = conf_params.modals['consent_modal']['description']; 107 consent_primary_btn.innerHTML = conf_params.modals['consent_modal']['primary_btn']['text']; 108 consent_secondary_btn.innerHTML = conf_params.modals['consent_modal']['secondary_btn']['text']; 109 110 if(conf_params.modals['consent_modal']['primary_btn']['role'] == 'accept_all'){ 111 _addEvent(consent_primary_btn, "click", function(){ 112 _cookieconsent.hide(); 113 _log("CookieConsent [ACCEPT]: cookie_consent was accepted!"); 114 _saveCookiePreferences(conf_params, 1); // 1 => accept all 115 }); 116 }else{ 117 _addEvent(consent_primary_btn, "click", function(){ 118 _cookieconsent.hide(); 119 _log("CookieConsent [ACCEPT]: cookie_consent was accepted (necessary only)!"); 120 _saveCookiePreferences(conf_params, -1); // -1 => accept current selection 121 }); 122 } 123 124 // insert title into consent_modal 125 consent_modal_inner.appendChild(consent_title); 126 127 // insert description into consent_modal 128 consent_modal_inner.appendChild(consent_text); 129 130 // insert buttons into consent_buttons 131 consent_buttons.appendChild(consent_primary_btn); 132 consent_buttons.appendChild(consent_secondary_btn); 133 134 // insert btn_container into consent_modal 135 consent_modal_inner.appendChild(consent_buttons); 136 137 if(conf_params.modals['consent_modal']['secondary_btn']['role'] == 'accept_necessary'){ 138 _addEvent(consent_secondary_btn, 'click', function(){ 139 _cookieconsent.hide(); 140 _saveCookiePreferences(conf_params, 0); // 0 => accept necesssary only 141 }); 142 }else{ 143 _addEvent(consent_secondary_btn, 'click', function(){ 144 _cookieconsent.showSettings(0); 145 }); 146 } 147 148 // Append consent modal to dom 149 consent_modal.appendChild(consent_modal_inner); 150 all_modals_container.appendChild(consent_modal); 151 } 152 153 /** 154 * Create all consent_modal elems 155 */ 156 settings_container = document.createElement("div"); 157 var settings_container_valign = document.createElement("div"); 158 var settings = document.createElement("div"); 159 var settings_container_inner = document.createElement("div"); 160 var settings_inner = document.createElement("div"); 161 var settings_title = document.createElement("h1"); 162 var settings_header = document.createElement("div"); 163 var settings_close_btn = document.createElement("button"); 164 var settings_blocks = document.createElement("div"); 165 settings_close_btn.setAttribute('type', 'button'); 166 167 /** 168 * Set for each of them, their default configured ids 169 */ 170 settings_container.id = 'cs_cont'; 171 settings_container_valign.id = "cs_valign"; 172 settings_container_inner.id = "cs_cont_inner"; 173 settings.id = "cs"; 174 settings_title.id = 'cs_title'; 175 settings_inner.id = "cs_inner"; 176 settings_header.id = "cs_header"; 177 settings_blocks.id = 'cs_blocks'; 178 settings_close_btn.id = 'cs_close_btn'; 179 settings_close_btn.innerHTML = 'x'; 180 settings_close_btn.className = 'cc-default'; 181 182 _addEvent(settings_close_btn, 'click', function(){ 183 _cookieconsent.hideSettings(); 184 }); 185 186 var all_blocks = conf_params.modals['settings_modal']['blocks']; 187 188 // Get number of blocks with cs_lang as language 189 var n_blocks = all_blocks.length; 190 191 // Set cc settings modal title 192 settings_title.innerHTML = conf_params.modals['settings_modal']['title']; 193 var count_toggles = 0, div_btns = []; 194 195 // Create settings modal content (blocks) 196 for(var i=0; i<n_blocks; ++i){ 197 198 // Create title 199 var block_section = document.createElement('div'); 200 var block_title_container = document.createElement('div'); 201 var block_table_container = document.createElement('div'); 202 var block_title = document.createElement('h2');
203 var block_desc = document.createElement('div'); 204 block_section.className = 'cs_block'; 205 block_title.className = 'b_title'; 206 block_title_container.className = 'title'; 207 block_table_container.className = 'desc'; 208 block_desc.className = 'p'; 209 210 // set title and description for each block 211 block_title.innerHTML = all_blocks[i]['title']; 212 block_desc.innerHTML = all_blocks[i]['description']; 213 214 block_title_container.appendChild(block_title); 215 216 // create toggle if specified (opt in/out) 217 if(typeof all_blocks[i]['toggle'] !== 'undefined'){ 218 var block_switch_label = document.createElement('label'); 219 var block_switch = document.createElement('input'); 220 var block_switch_span = document.createElement('span'); 221 222 block_switch_label.className = 'c_b_toggle'; 223 block_switch.className = 'c_toggle'; 224 block_switch.setAttribute('aria-label', 'toggle'); 225 block_switch_span.className = 'sc_toggle'; 226 227 block_switch.setAttribute('type', 'checkbox'); 228 block_switch.value = all_blocks[i]['toggle'].value; 229 block_switch.setAttribute('aria-label', all_blocks[i]['toggle'].value); 230 231 // if cookie is set, get current saved preferences from cookie 232 if(never_accepted){ 233 if(_arrayContains(JSON.parse(_getCookie('cc_cookie')).level, all_blocks[i]['toggle'].value)){ 234 block_switch.checked = true; 235 toggle_states.push(true); 236 }else{ 237 toggle_states.push(false); 238 } 239 }else if(all_blocks[i]['toggle']['enabled']){ 240 block_switch.checked = true; 241 } 242 243 if(all_blocks[i]['toggle']['readonly']){ 244 block_switch.disabled = "disabled" 245 block_switch.readOnly = true; 246 _addClass(block_switch_span, 'd-none'); 247 } 248 249 block_switch_label.appendChild(block_switch); 250 block_switch_label.appendChild(block_switch_span); 251 252 block_title_container.appendChild(block_switch_label); 253 254 _addClass(block_table_container, 'accordion'); 255 _addClass(block_title_container, 'block_button'); 256 _addClass(block_section, 'block__expand'); 257 div_btns.push(block_title_container); 258 259 _addEvent(div_btns[count_toggles], 'click', function(_index){ 260 if(!_hasClass(div_btns[_index].parentNode, '_active')){ 261 _addClass(div_btns[_index].parentNode, '_active'); 262 }else{ 263 _removeClass(div_btns[_index].parentNode, '_active'); 264 } 265 }, false, count_toggles); 266 267 count_toggles++; 268 } 269 270 block_section.appendChild(block_title_container); 271 block_table_container.appendChild(block_desc); 272 273 // if cookie table found, generate table for this block 274 if(typeof all_blocks[i]['cookie_table'] !== 'undefined'){ 275 276 // generate cookie-table for this block 277 var block_table = document.createElement('table'); 278 279 // create table header 280 var thead = document.createElement('thead'); 281 var tr_tmp = document.createElement('tr'); 282 var all_table_headers = conf_params.modals['settings_modal']['cookie_table_headers']; 283 284 /** 285 * Use custom table headers 286 */ 287 for(var p=0; p<all_table_headers.length; ++p){ 288 // create new header 289 var th1 = document.createElement('th'); 290 291 // get custom header content 292 var new_column_key = _getKeys(all_table_headers[p])[0]; 293 var new_column_content = all_table_headers[p][new_column_key]; 294 295 th1.innerHTML = new_column_content; 296 tr_tmp.appendChild(th1); 297 } 298 299 thead.appendChild(tr_tmp); 300 block_table.appendChild(thead); 301 302 var tbody = document.createElement('tbody'); 303 304 // create table content 305 for( var n=0; n < all_blocks[i]['cookie_table'].length; n++ ) { 306 if ( all_blocks[i]['cookie_table'][n]['isShown'] ) { 307 var tr = document.createElement('tr'); 308 309 for(var g=0; g<all_table_headers.length; ++g){ 310 311 var td_tmp = document.createElement('td'); 312 313 // get custom header content 314 var new_column_key = _getKeys(all_table_headers[g])[0]; 315 var new_column_content = ""; 316 if ( new_column_key == 'col1' && all_blocks[i]['cookie_table'][n]['collection'] != undefined && all_blocks[i]['cookie_table'][n]['collection'].length > 0 ) { 317 new_column_content = all_blocks[i]['cookie_table'][n]['collection'].join('<br>'); 318 } else { 319 new_column_content = all_blocks[i]['cookie_table'][n][new_column_key]; 320 } 321 322 td_tmp.innerHTML = new_column_content; 323 td_tmp.setAttribute('data-column', all_table_headers[g][new_column_key]); 324 325 tr.appendChild(td_tmp); 326 } 327 328 tbody.appendChild(tr); 329 } 330 } 331 332 block_table.appendChild(tbody); 333 334 //block_section.appendChild(block_table); 335 block_table_container.appendChild(block_table); 336 } 337 338 block_section.appendChild(block_table_container); 339
340 // append block inside settings dom 341 settings_blocks.appendChild(block_section); 342 } 343 344 // Create settings buttons 345 var settings_buttons = document.createElement('div'); 346 var settings_save_btn = document.createElement('button'); 347 var settings_accept_all_btn = document.createElement('button'); 348 349 settings_buttons.id = 'cs_buttons'; 350 settings_save_btn.id = 'cs_save__btn'; 351 settings_accept_all_btn.id = 'cs_acceptall_btn'; 352 settings_save_btn.setAttribute('type', 'button'); 353 settings_accept_all_btn.setAttribute('type', 'button'); 354 settings_save_btn.className ='cc-default'; 355 settings_accept_all_btn.className ='cc-accept'; 356 settings_save_btn.innerHTML = conf_params.modals['settings_modal']['save_settings_btn']; 357 settings_accept_all_btn.innerHTML = conf_params.modals['settings_modal']['accept_all_btn']; 358 settings_buttons.appendChild(settings_accept_all_btn); 359 settings_buttons.appendChild(settings_save_btn); 360 361 // Add save preferences button onClick event 362 // Hide both settings modal and consent modal 363 _addEvent(settings_save_btn, 'click', function(){ 364 _cookieconsent.hideSettings(); 365 _cookieconsent.hide(); 366 _saveCookiePreferences(conf_params, -1); 367 }); 368 369 _addEvent(settings_accept_all_btn, 'click', function(){ 370 _cookieconsent.hideSettings(); 371 _cookieconsent.hide(); 372 _saveCookiePreferences(conf_params, 1); 373 }); 374 375 settings_header.appendChild(settings_title); 376 settings_header.appendChild(settings_close_btn); 377 settings_inner.appendChild(settings_header); 378 settings_inner.appendChild(settings_blocks); 379 settings_inner.appendChild(settings_buttons); 380 settings_container_inner.appendChild(settings_inner); 381 settings.appendChild(settings_container_inner); 382 settings_container_valign.appendChild(settings); 383 settings_container.appendChild(settings_container_valign); 384 settings_container.style.visibility = "hidden"; 385 386 all_modals_container.appendChild(settings_container); 387 388 // Finally append everything to body (main_container holds all modals container) 389 document.body.appendChild(main_container); 390 } 391 392 /** 393 * Save cookie preferences 394 * accept_type = 0: accept necessary only 395 * accept_type = 1: accept all 396 * accept_type = -1: accept selection 397 */ 398 var _saveCookiePreferences = function(conf_params, accept_type){ 399 400 // Get all cookiepreferences values saved in cookieconsent settings modal 401 var category_toggles = document.querySelectorAll('.c_toggle'); 402 var c_cookie_level = '', changedSettings = false; 403 404 // If there are opt in/out toggles ... 405 if(typeof category_toggles.length === "number"){ 406 switch(accept_type){ 407 case -1: 408 //accept current selection 409 for(var i=0; i<category_toggles.length; i++){ 410 if(category_toggles[i].checked){ 411 c_cookie_level+='"'+category_toggles[i].value+'",'; 412 if(!toggle_states[i]){ 413 changedSettings = true; 414 toggle_states[i] = true; 415 } 416 }else{ 417 if(toggle_states[i]){ 418 changedSettings = true; 419 toggle_states[i] = false; 420 } 421 } 422 } 423 break; 424 case 0: 425 // disable all except necessary 426 for(var i=0; i<category_toggles.length; i++){ 427 if(category_toggles[i].readOnly){
428 c_cookie_level+='"'+category_toggles[i].value+'",'; 429 toggle_states[i] = true; 430 }else{ 431 category_toggles[i].checked = false; 432 if(toggle_states[i]){ 433 changedSettings = true; 434 toggle_states[i] = false; 435 } 436 } 437 } 438 break; 439 case 1: 440 // enable all 441 for(var i=0; i<category_toggles.length; i++){ 442 category_toggles[i].checked = true 443 c_cookie_level+='"'+category_toggles[i].value+'",'; 444 if(!toggle_states[i]){ 445 changedSettings = true; 446 } 447 448 toggle_states[i] = true; 449 } 450 break; 451 } 452 453 // remove last ',' character 454 c_cookie_level = c_cookie_level.slice(0, -1); 455 456 /** 457 * If autoclear_cookies==true -> delete all cookies which are unused (based on selected preferences) 458 */ 459 460 if(conf_params['autoclear_cookies'] && !cookie_consent_accepted){ 461 462 // Get array of all blocks defined inside settings 463 var all_blocks = conf_params.modals['settings_modal']['blocks']; 464 465 // Get number of blocks 466 var len = all_blocks.length; 467 468 // For each block 469 var count = -1; 470 for(var jk=0; jk<len; jk++){ 471 // Save current block (local scope & less accesses -> ~faster value retrieval) 472 var curr_block = all_blocks[jk]; 473 474 // If current block has a toggle for opt in/out 475 if(curr_block.hasOwnProperty('toggle')){ 476 477 // if current block has a cookie table with toggle off => delete cookies 478 if(!toggle_states[++count] && curr_block.hasOwnProperty('cookie_table')){ 479 480 var ckey = _getKeys(conf_params.modals['settings_modal']['cookie_table_headers'][0])[0]; 481 482 // Get number of cookies defined in cookie_table 483 var clen = curr_block['cookie_table'].length; 484 485 // Delete each cookie defined in ccb_cookie_table of current block 486 for(var hk=0; hk<clen; hk++){ 487 // Get current row of table (corresponds to all cookie params) 488 var curr_row = curr_block['cookie_table'][hk]; 489 490 // If cookie exists -> delete it 491 if(_getCookie(curr_row[ckey]) != ""){ 492 _eraseCookie(curr_row[ckey]); 493 //_log('CookieConsent [AUTOCLEAR]: deleting cookie: \''+curr_row[ckey] +'\''); 494 } 495 } 496 } 497 } 498 } 499 } 500 } 501 502 var cookie_content = '{"level": ['+c_cookie_level+']}'; 503 504 // save cookie with preferences 'level' (only if never accepted or settings were updated) 505 if(!cookie_consent_accepted || changedSettings) 506 _setCookie('cc_cookie', cookie_content, _config.cookie_expiration); 507 508 if(typeof conf_params['onAccept'] === "function" && !cookie_consent_accepted){ 509 cookie_consent_accepted = true; 510 return conf_params['onAccept'](JSON.parse(cookie_content)); 511 } 512 513 // fire onChange only if settings were changed
514 if(typeof conf_params['onChange'] === "function" && changedSettings){ 515 conf_params['onChange'](JSON.parse(cookie_content)); 516 } 517 } 518 519 /** 520 * Returns true if value is found inside array 521 * @param {Array} arr 522 * @param {Object} value 523 */ 524 var _arrayContains = function(arr, value){ 525 var len = arr.length; 526 for(var i=0; i<len; i++){ 527 if(arr[i] == value) 528 return true; 529 } 530 return false; 531 } 532 533 /** 534 * Helper function which prints info (console.log()) 535 * @param {object} print_msg 536 * @param {object} optional_param 537 */ 538 var _log = function(print_msg, optional_param, error){ 539 //!error ? console.log(print_msg, optional_param || ' ') : console.error(print_msg, optional_param || ""); 540 } 541 542 /** 543 * Returns true cookie category is saved into cc_cookie 544 * @param {String} cookie_name 545 */ 546 _cookieconsent.allowedCategory = function(cookie_name){ 547 return _arrayContains(JSON.parse(_getCookie('cc_cookie') || '{}')['level'] || [], cookie_name); 548 } 549 550 /** 551 * Check if cookieconsent is alredy attached to dom 552 * If not, create one, configure it and attach it to body 553 */ 554 _cookieconsent.run = function(conf_params){ 555 function __checkCookies(allBlocks, allowedCategories) { 556 allBlocks.forEach(function (block, index1) { 557 if (block.hasOwnProperty('toggle')) { 558 if (block.hasOwnProperty('cookie_table')) { 559 if (allowedCategories.indexOf(block.toggle.value) === -1) { 560 block.cookie_table.forEach(function(cookieParams, index2) { 561 _eraseCookie(cookieParams.col1); 562 }); 563 } 564 } 565 } 566 }); 567 } //checkCookies 568 569 if(!main_container){ 570 // config all parameters 571 _setConfig(conf_params); 572 573 // Get cookie value 574 var cookie_val = _getCookie('cc_cookie'); 575 576 // If cookie is empty => create consent modal 577 consent_modal_exists = cookie_val == ''; 578 579 // Generate cookie-settings dom (& consent modal) 580 _createCookieConsentHTML(!consent_modal_exists, conf_params); 581 _addCookieSettingsButtonListener(); 582 583 // Add class to enable animations 584 setTimeout(function() { 585 _addClass(main_container, 'cookie-consent-anim'); 586 }, 10); 587 588 if (!cookie_val) { 589 if (_config.autorun) { 590 _cookieconsent.show(conf_params['delay'] > 30 ? conf_params['delay'] : 30); 591 } 592 } else { 593 //fire once onAccept method (if defined) 594 if (typeof conf_params['onAccept'] === "function" && !cookie_consent_accepted){ 595 cookie_consent_accepted = true; 596 conf_params['onAccept'](JSON.parse(cookie_val || "{}")); 597 } 598 599 //Delete cookies if not allowed 600 var cookieSettings = JSON.parse(cookie_val || "{}"); 601 if (cookieSettings && cookieSettings.level != undefined) { 602 var count = -1; 603 var allBlocks = conf_params.modals['settings_modal']['blocks']; 604 var allowedCategories = cookieSettings.level; 605 606 __checkCookies(allBlocks, allowedCategories); 607 608 setTimeout(function() { 609 __checkCookies(allBlocks, allowedCategories); 610 }, 1000); 611 } 612 } 613 }else{ 614 _log("CookieConsent [NOTICE]: cookie consent alredy attached to body!"); 615 } 616 617 } 618 619 _cookieconsent.showSettings = function(delay){ 620 setTimeout(function() { 621 _addClass(html_dom, "show--settings"); 622 _log("CookieConsent [SETTINGS]: show settings_modal"); 623 }, delay > 0 ? delay : 0); 624 } 625 626 _cookieconsent.loadScript = function(src, callback, attrs){ 627 // Load script only if not alredy loaded 628 if(!document.querySelector('script[src="' + src + '"]')){ 629 630 var script = document.createElement('script'); 631 632 // if an array is provided => add custom attributes 633 if(attrs && attrs.length > 0){ 634 for(var i=0; i<attrs.length; ++i){ 635 script.setAttribute(attrs[i]['name'], attrs[i]['value']); 636 } 637 } 638 639 if(script.readyState) { // only required for IE <9 640 script.onreadystatechange = function() { 641 if ( script.readyState === "loaded" || script.readyState === "complete" ) { 642 script.onreadystatechange = null; 643 callback(); 644 } 645 }; 646 }else{ //Others 647 script.onload = callback; 648 } 649 650 script.src = src; 651 document.getElementsByTagName('head')[0].appendChild(script); 652 } 653 } 654 655 /** 656 * Show cookie consent modal (with delay parameter) 657 * @param {number} delay 658 */ 659 _cookieconsent.show = function(delay){ 660 if(consent_modal_exists){ 661 setTimeout(function() { 662 _addClass(html_dom, "show--consent"); 663 _log("CookieConsent [MODAL]: show consent_modal"); 664 }, delay > 0 ? delay : 0); 665 } 666 } 667 668 // Hide consent modal 669 _cookieconsent.hide = function(){ 670 if(consent_modal_exists){ 671 _removeClass(html_dom, "show--consent"); 672 _log("CookieConsent [MODAL]: hide"); 673 } 674 } 675 676 /** 677 * Hide settings modal 678 */ 679 _cookieconsent.hideSettings = function(){ 680 _removeClass(html_dom, "show--settings"); 681 _log("CookieConsent [SETTINGS]: hide settings_modal"); 682 } 683 684 /** 685 * Set cookie, specifying name, value and expiration time 686 * @param {string} name 687 * @param {string} value 688 * @param {number} days 689 * @param {number} hours 690 * @param {number} minutes 691 */
692 var _setCookie = function(name, value, days) { 693 var expires = ""; 694 695 var date = new Date(); 696 date.setTime(date.getTime() + (1000 * (days * 24 * 60 * 60))); 697 expires = "; expires=" + date.toUTCString(); 698 699 /** 700 * Set secure cookie if https found 701 */ 702 if(location.protocol === "https:"){ 703 document.cookie = name + "=" + (value || "") + expires + "; path=/; Domain=" + window.location.hostname + "; SameSite=Lax; Secure"; 704 }else{ 705 document.cookie = name + "=" + (value || "") + expires + "; path=/; SameSite=Lax;"; 706 } 707 708 _log("CookieConsent [SET_COOKIE]: cookie "+ name + "='" + value + "' was set!"); 709 } 710 711 /** 712 * Get cookie value by name, 713 * returns cookie value if found, otherwise empty string: "" 714 * @param {string} a 715 */ 716 var _getCookie = function(a) { 717 //Old version for getting the cookie value 718 //return (a = document.cookie.match("(^|;)\\s*" + a + "\\s*=\\s*([^;]+)")) ? a.pop() : "" 719 720 var cookies = document.cookie 721 .split(';') 722 .map(cookie => cookie.split('=')) 723 .reduce((accumulator, [key, value]) => ({ ...accumulator, [key.trim()]: decodeURIComponent(value) }), {}); 724 return (cookies[a] != undefined) ? cookies[a] : ''; 725 } 726 727 /** 728 * Delete cookie by name 729 * @param {string} name 730 */ 731 var _eraseCookie = function(name) { 732 var hostName = window.location.hostname; 733 var hostNameShort = hostName.substring(hostName.indexOf('.')); 734 735 document.cookie = name + '=; Path=/; Domain=' + hostName + '; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 736 document.cookie = name + '=; Path=/; Domain=.' + hostName + '; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 737 document.cookie = name + '=; Path=/; Domain=' + hostNameShort + '; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 738 document.cookie = name + '=; Path=/; Domain=.' + hostNameShort + '; Expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 739 } 740 741 /** 742 * Return true if cookie was found and has valid value (not empty string) 743 * @param {string} cookie_name 744 */ 745 _cookieconsent.validCookie = function(cookie_name){ 746 return _getCookie(cookie_name) != ""; 747 } 748 749 /** 750 * Add event listener to dom object (cross browser function) 751 * @param {Object} elem 752 * @param {string} event //event type 753 * @param {Object } fn 754 * @param {boolean} passive 755 */ 756 var _addEvent = function(elem, event, fn, passive, count_value) { 757 var passive = passive || false, _fn = (typeof count_value !== 'number' ? fn : function(){ fn(count_value) }); 758 759 if (elem.addEventListener) { 760 passive ? elem.addEventListener(event, _fn , { passive: true }) : elem.addEventListener(event, _fn, false); 761 } else { 762 /** 763 * For old browser, convert "click" to onclick 764 * since they're not always supported 765 */ 766 if (event == "click") { 767 event = "onclick"; 768 } 769 elem.attachEvent(event, _fn); 770 } 771 } 772 773 /** 774 * Get all prop. keys defined inside object 775 * @param {Object} obj 776 */ 777 var _getKeys = function(obj){ 778 if(typeof obj === "object"){ 779 var keys = [], i = 0; 780 for (keys[i++] in obj) {}; 781 return keys; 782 } 783 } 784 785 /** 786 * Append class to specified dom element 787 * @param {Object} elem 788 * @param {string} classname 789 */ 790 var _addClass = function (elem, classname){ 791 if(elem.classList) 792 elem.classList.add(classname) 793 else{ 794 if(!_hasClass(elem, classname)) 795 elem.className += ' '+classname; 796 } 797 } 798 799 /** 800 * Remove specified class from dom element 801 * @param {HTMLElement} elem 802 * @param {string} classname 803 */ 804 var _removeClass = function (el, className) { 805 el.classList ? el.classList.remove(className) : el.className = el.className.replace(new RegExp('(\\s|^)' + className + '(\\s|$)'), ' '); 806 } 807 808 /** 809 * Check if html element has classname 810 * @param {HTMLElement} el 811 * @param {String} className 812 */ 813 var _hasClass = function(el, className) { 814 if (el.classList) { 815 return el.classList.contains(className); 816 } 817 return !!el.className.match(new RegExp('(\\s|^)' + className + '(\\s|$)')); 818 } 819 820 /** 821 * Get cookies table by types 822 * @param [type] string 823 **/ 824 _cookieconsent.getCookieTable = function(type) { 825 var cookies = document.cookie 826 .split(';') 827 .map(cookie => cookie.split('='))
828 .reduce((accumulator, [key, value]) => ({ ...accumulator, [key.trim()]: decodeURIComponent(value) }), {}); 829 var cookieTable =[]; 830 831 function __getCookieCollectionByPattern(pattern, cookies) { 832 var collection = []; 833 for (var key of Object.keys(cookies)) { 834 if (key.indexOf(pattern) !== -1) { 835 collection.push(key); 836 } 837 } 838 return collection; 839 } 840 841 var hostName = window.location.hostname; 842 var hostNameShort = hostName.substring(0, hostName.indexOf('.')); 843 844 switch (type.toLowerCase()) { 845 case "necessary" : 846 cookieTable.push({ 847 'col1' : 'PHPSESSID', 848 'col2' : lang('cookie_consent:cookie_description_phpsessid'), 849 'isShown' : true, 850 'collection' : ['PHPSESSID'] 851 }); 852 cookieTable.push({ 853 'col1' : 'cc_cookie', 854 'col2' : lang('cookie_consent:cookie_description_cc_cookie'), 855 'isShown' : true, 856 'collection' : ['cc_cookie'] 857 }); 858 859 var collection = []; 860 861 collection = __getCookieCollectionByPattern('_pyrocms', cookies); 862 if (collection.indexOf(hostNameShort + '_pyrocms') === -1) { 863 collection.push(hostNameShort + '_pyrocms'); 864 } 865 if (collection.indexOf(hostNameShort + '_pyrocms_development') === -1) { 866 collection.push(hostNameShort + '_pyrocms_development'); 867 } 868 for (var i = 0; i < collection.length; i++) { 869 cookieTable.push({ 870 'col1' : collection[i], 871 'col2' : lang('cookie_consent:cookie_description_performance'), 872 'isShown' : i == 0, 873 'collection' : collection 874 }); 875 } 876 877 var collection = __getCookieCollectionByPattern('_idleSecondsCounter', cookies); 878 if (collection.length > 0) { 879 cookieTable.push({ 880 'col1' : collection[i], 881 'col2' : lang('cookie_consent:cookie_description__idlesecondscounter'), 882 'isShown' : true, 883 'collection' : collection 884 }); 885 } 886 break; 887 case "analytics" : 888 cookieTable.push({ 889 'col1' : '_ga', 890 'col2' : lang('cookie_consent:cookie_description__ga'), 891 'isShown' : true, 892 'collection' : ['_ga'] 893 }); 894 cookieTable.push({ 895 'col1' : '_gid', 896 'col2' : lang('cookie_consent:cookie_description__gid'), 897 'isShown' : true, 898 'collection' : ['_gid'] 899 }); 900 var collection = __getCookieCollectionByPattern('_gat_', cookies); 901 for (var i = 0; i < collection.length; i++) { 902 cookieTable.push({ 903 'col1' : collection[i], 904 'col2' : lang('cookie_consent:cookie_description__gat_'), 905 'isShown' : i == 0, 906 'collection' : collection 907 }); 908 } 909 break; 910 case "performance" : 911 //At this moment we do not add cookies to this category 912 break; 913 case "functionality" : 914 var collection = __getCookieCollectionByPattern('__str
914ipe_', cookies); 915 916 //Sometimes __stripe related cookies listed but not being added - adding them manually just for sure 917 if (collection.indexOf('__stripe_mid') === -1) { 918 collection.push('__stripe_mid'); 919 } 920 if (collection.indexOf('__stripe_sid') === -1) { 921 collection.push('__stripe_sid'); 922 } 923 924 for (var i = 0; i < collection.length; i++) { 925 cookieTable.push({ 926 'col1' : collection[i], 927 'col2' : lang('cookie_consent:cookie_description___stripe_'), 928 'isShown' : i == 0, 929 'collection' : collection 930 }); 931 } 932 break; 933 } 934 935 return cookieTable; 936 } //getCookieTable 937 938 return _cookieconsent; 939 }; 940 941 /** 942 * Make CookieConsent object accessible globally 943 */ 944 if(typeof window['initCookieConsent'] !== 'function'){ 945 window['initCookieConsent'] = CookieConsent; 946 } 947})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.