JAVASCRIPT 26
Cookienotice.js Guest on 12th June 2020 08:04:55 PM
  1. /*
  2.  Copyright 2014 Google Inc. All rights reserved.
  3.  
  4.  Licensed under the Apache License, Version 2.0 (the "License");
  5.  you may not use this file except in compliance with the License.
  6.  You may obtain a copy of the License at
  7.  
  8.  http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.  Unless required by applicable law or agreed to in writing, software
  11.  distributed under the License is distributed on an "AS IS" BASIS,
  12.  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  See the License for the specific language governing permissions and
  14.  limitations under the License.
  15.  */
  16.  
  17. /*
  18.  * For more information on this file, see http://www.cookiechoices.org/
  19.  */
  20.  
  21. (function(window) {
  22.  
  23.   if (!!window.cookieChoices) {
  24.     return window.cookieChoices;
  25.   }
  26.  
  27.   var document = window.document;
  28.   // IE8 does not support textContent, so we should fallback to innerText.
  29.   var supportsTextContent = 'textContent' in document.body;
  30.  
  31.   var cookieChoices = (function() {
  32.  
  33.     var cookieName = 'displayCookieNotice';
  34.     var cookieConsentId = 'cookieChoiceInfo';
  35.     var divClass = 'cookie-choices-info';
  36.     var innerDivClass = 'cookie-choices-inner';
  37.     var textSpan = 'cookie-choices-text';
  38.     var buttonsClass = 'cookie-choices-buttons';
  39.     var buttonClass = 'cookie-choices-button';
  40.     var singletonClass = 'singleton-element';
  41.     var dismissLinkId = 'cookieChoiceDismiss';
  42.  
  43.     function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {
  44.       // Clean the HTML Tags from cookieText
  45.       if (cookieText && cookieText.replace) {
  46.         cookieText = cookieText.replace(/<[^>]*>/g,'');
  47.       }
  48.  
  49.       var cookieInnerElement = document.createElement('div');
  50.       cookieInnerElement.className = innerDivClass;
  51.       cookieInnerElement.appendChild(_createConsentText(cookieText));
  52.  
  53.       var buttonsElement = document.createElement('span');
  54.       buttonsElement.className = buttonsClass;
  55.       cookieInnerElement.appendChild(buttonsElement);
  56.  
  57.       if (!!linkText && !!linkHref) {
  58.         buttonsElement.appendChild(_createInformationLink(linkText, linkHref));
  59.       }
  60.  
  61.       buttonsElement.appendChild(_createDismissLink(dismissText));
  62.  
  63.       var cookieConsentElement = document.createElement('div');
  64.       cookieConsentElement.id = cookieConsentId;
  65.       cookieConsentElement.className = divClass + ' ' + singletonClass;
  66.       cookieConsentElement.appendChild(cookieInnerElement);
  67.       return cookieConsentElement;
  68.     }
  69.  
  70.     function _createStyleElement() {
  71.       var style = document.createElement('style');
  72.       style.className = singletonClass;
  73.       style.type = 'text/css';
  74.       _setElementText(style,
  75.           '.' + divClass + ' { ' +
  76.               'position:fixed;width:100%;background-color:#666;margin:0;' +
  77.               'left:0;top:0;padding:0;z-index:4000;text-align:center;' +
  78.               'color:#fff;line-height:140%;padding:10px 0;' +
  79.               'font-family:roboto,Arial; } ' +
  80.           '.' + divClass + ' .' + innerDivClass + ' { ' +
  81.               'position:relative;width:initial;margin:0;left:0;top:0; } ' +
  82.           '.' + divClass + ' .' + textSpan + ' { ' +
  83.               'display:inline-block;vertical-align:middle;font-size:16px;' +
  84.               'margin:10px 20px;color:#ccc;max-width:800px;' +
  85.               'text-align:left; }' +
  86.           '.' + divClass + ' .' + buttonsClass + ' { ' +
  87.               'display:inline-block;vertical-align:middle;' +
  88.               'white-space:nowrap;margin:0 10px; } ' +
  89.           '.' + divClass + ' .' + buttonClass + ':hover { ' +
  90.               ' color: #fff; } ' +
  91.           '.' + divClass + ' .' + buttonClass + ' { ' +
  92.               'font-weight:bold;text-transform:UPPERCASE;' +
  93.               'white-space:nowrap;' +
  94.               'color:#eee;margin-left:8px;padding:0 6px; ' +
  95.               'text-decoration:none; }');
  96.       document.getElementsByTagName('head')[0].appendChild(style);
  97.     }
  98.  
  99.     function _setElementText(element, text) {
  100.       if (supportsTextContent) {
  101.         element.textContent = text;
  102.       } else {
  103.         element.innerText = text;
  104.       }
  105.     }
  106.  
  107.     function _createConsentText(cookieText) {
  108.       var consentText = document.createElement('span');
  109.       _setElementText(consentText, cookieText);
  110.       consentText.className = textSpan;
  111.       return consentText;
  112.     }
  113.  
  114.     function _createDismissLink(dismissText) {
  115.       var dismissLink = document.createElement('a');
  116.       _setElementText(dismissLink, dismissText);
  117.       dismissLink.id = dismissLinkId;
  118.       dismissLink.href = '#';
  119.       dismissLink.className = buttonClass;
  120.       return dismissLink;
  121.     }
  122.  
  123.     function _createInformationLink(linkText, linkHref) {
  124.       var infoLink = document.createElement('a');
  125.       _setElementText(infoLink, linkText);
  126.       infoLink.href = linkHref;
  127.       infoLink.target = '_blank';
  128.       infoLink.className = buttonClass;
  129.       return infoLink;
  130.     }
  131.  
  132.     function _dismissLinkClick(e) {
  133.       _saveUserPreference();
  134.       _removeCookieConsent();
  135.       e.stopPropagation && e.stopPropagation();
  136.       e.cancelBubble = true;
  137.       return false;
  138.     }
  139.  
  140.     function _showCookieConsent(cookieText, dismissText, linkText, linkHref) {
  141.       if (_shouldDisplayConsent()) {
  142.         _removeCookieConsent();
  143.         _createStyleElement();
  144.         var consentElement =
  145.             _createHeaderElement(cookieText, dismissText, linkText, linkHref);
  146.         var fragment = document.createDocumentFragment();
  147.         fragment.appendChild(consentElement);
  148.         document.body.appendChild(fragment.cloneNode(true));
  149.         document.getElementById(dismissLinkId).onclick = _dismissLinkClick;
  150.       }
  151.     }
  152.  
  153.     function _removeCookieConsent() {
  154.       var cookieChoiceElement = document.getElementById(cookieConsentId);
  155.       if (cookieChoiceElement != null) {
  156.         cookieChoiceElement.parentNode.removeChild(cookieChoiceElement);
  157.       }
  158.     }
  159.  
  160.     function _saveUserPreference() {
  161.       // Set the cookie expiry to one year after today.
  162.       var expiryDate = new Date();
  163.       expiryDate.setFullYear(expiryDate.getFullYear() + 1);
  164.       document.cookie =
  165.           cookieName + '=y; path=/; expires=' + expiryDate.toGMTString();
  166.     }
  167.  
  168.     function _shouldDisplayConsent() {
  169.       // Display the header only if the cookie has not been set.
  170.       return !document.cookie.match(new RegExp(cookieName + '=([^;]+)'));
  171.     }
  172.  
  173.     var exports = {};
  174.     exports.showCookieConsentBar = _showCookieConsent;
  175.     return exports;
  176.   })();
  177.  
  178.   window.cookieChoices = cookieChoices;
  179.   return cookieChoices;
  180. })(this);

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.