1 line
No EOL
14 KiB
JavaScript
1 line
No EOL
14 KiB
JavaScript
!function(){"use strict";function _toPropertyKey(e){var t=function _toPrimitive(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var o=n.call(e,t||"default");if("object"!=typeof o)return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}function _typeof(e){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_typeof(e)}function _defineProperties(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,_toPropertyKey(o.key),o)}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,o=new Array(t);n<t;n++)o[n]=e[n];return o}function _createForOfIteratorHelper(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?_arrayLikeToArray(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0,F=function(){};return{s:F,n:function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}},e:function(e){throw e},f:F}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var s,r=!0,a=!1;return{s:function(){n=n.call(e)},n:function(){var e=n.next();return r=e.done,e},e:function(e){a=!0,s=e},f:function(){try{r||null==n.return||n.return()}finally{if(a)throw s}}}}String.prototype.formatUnicorn=String.prototype.formatUnicorn||function(){var e=this.toString();if(arguments.length){var t,n=_typeof(arguments[0]),o="string"===n||"number"===n?Array.prototype.slice.call(arguments):arguments[0];for(t in o)e=e.replace(new RegExp("\\{"+t+"\\}","gi"),o[t])}return e};var e=function(){function PrivacyWire(e){!function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,PrivacyWire),this.name="privacywire",this.toggleToStatus=!0,this.cookieGroups=Object.freeze(["necessary","functional","statistics","marketing","external_media"]),this.settings=this.sanitizeSettings(e),this.userConsent=this.sanitizeStoredConsent(),this.elements=this.initiateElements(),this.syncConsentToCheckboxes(),this.checkForValidConsent()||this.showBanner(),this.checkElementsWithRequiredConsent(),this.handleButtons()}return function _createClass(e,t,n){return t&&_defineProperties(e.prototype,t),n&&_defineProperties(e,n),Object.defineProperty(e,"prototype",{writable:!1}),e}(PrivacyWire,[{key:"sanitizeSettings",value:function sanitizeSettings(e){var t={};t.version=parseInt(e.version),t.dnt=Boolean(parseInt(e.dnt)),t.bots=Boolean(parseInt(e.bots)),t.customFunction="".concat(e.customFunction),t.messageTimeout=parseInt(e.messageTimeout),t.consentByClass=Boolean(parseInt(e.consentByClass)),t.cookieGroups={};var n,o=_createForOfIteratorHelper(this.cookieGroups);try{for(o.s();!(n=o.n()).done;){var s=n.value;t.cookieGroups[s]="".concat(e.cookieGroups[s])}}catch(e){o.e(e)}finally{o.f()}return t}},{key:"sanitizeStoredConsent",value:function sanitizeStoredConsent(){if(!window.localStorage.getItem(this.name))return this.getDefaultConsent();var e=JSON.parse(window.localStorage.getItem(this.name));if(parseInt(e.version)!==this.settings.version)return this.getDefaultConsent();if(void 0===e.cookieGroups)return this.removeStoredConsent(),this.getDefaultConsent();var t={};t.version=parseInt(e.version),t.cookieGroups={};var n,o=_createForOfIteratorHelper(this.cookieGroups);try{for(o.s();!(n=o.n()).done;){var s=n.value;t.cookieGroups[s]=Boolean(e.cookieGroups[s])}}catch(e){o.e(e)}finally{o.f()}return t}},{key:"getDefaultConsent",value:function getDefaultConsent(){var e,t={version:0,cookieGroups:{}},n=_createForOfIteratorHelper(this.cookieGroups);try{for(n.s();!(e=n.n()).done;){var o=e.value;t.cookieGroups[o]="necessary"===o}}catch(e){n.e(e)}finally{n.f()}return t}},{key:"initiateElements",value:function initiateElements(){var e={banner:{}};e.banner.wrapper=document.getElementById("privacywire-wrapper"),e.banner.intro=e.banner.wrapper.getElementsByClassName("privacywire-banner"),e.banner.options=e.banner.wrapper.getElementsByClassName("privacywire-options"),e.banner.message=e.banner.wrapper.getElementsByClassName("privacywire-message"),e.buttons={},e.buttons.acceptAll=e.banner.wrapper.getElementsByClassName("allow-all"),e.buttons.acceptNecessary=e.banner.wrapper.getElementsByClassName("allow-necessary"),e.buttons.choose=e.banner.wrapper.getElementsByClassName("choose"),e.buttons.toggle=e.banner.wrapper.getElementsByClassName("toggle"),e.buttons.save=e.banner.wrapper.getElementsByClassName("save"),e.buttons.askForConsent=document.getElementsByClassName("privacywire-consent-button"),e.buttons.externalTrigger=document.getElementsByClassName("privacywire-show-options"),e.checkboxes={};var t,n=_createForOfIteratorHelper(this.cookieGroups);try{for(n.s();!(t=n.n()).done;){var o=t.value;"necessary"!==o&&(e.checkboxes[o]=document.getElementById(o))}}catch(e){n.e(e)}finally{n.f()}return e.blueprint=document.getElementById("privacywire-ask-consent-blueprint"),e.elementsWithRequiredConsent=!0===this.settings.consentByClass?document.getElementsByClassName("require-consent"):document.querySelectorAll("[data-category]"),e.consentWindows=document.getElementsByClassName("privacywire-ask-consent"),e}},{key:"handleButtons",value:function handleButtons(){this.handleButtonHelper(this.elements.buttons.acceptAll,"handleButtonAcceptAll"),this.handleButtonHelper(this.elements.buttons.acceptNecessary,"handleButtonAcceptNecessary"),this.handleButtonHelper(this.elements.buttons.choose,"handleButtonChoose"),this.handleButtonHelper(this.elements.buttons.toggle,"handleButtonToggle"),this.handleButtonHelper(this.elements.buttons.save,"handleButtonSave"),this.handleButtonHelper(this.elements.buttons.askForConsent,"handleButtonAskForConsent"),this.handleButtonHelper(this.elements.buttons.externalTrigger,"handleButtonExternalTrigger")}},{key:"handleButtonHelper",value:function handleButtonHelper(e,t){if(e){var n=this;Array.from(e).forEach((function(e){n[t](e)}))}}},{key:"reHandleExternalButtons",value:function reHandleExternalButtons(){this.elements.buttons.externalTrigger=document.getElementsByClassName("privacywire-show-options"),this.handleButtonHelper(this.elements.buttons.externalTrigger,"handleButtonExternalTrigger")}},{key:"handleButtonAcceptAll",value:function handleButtonAcceptAll(e){var t=this;e.addEventListener("click",(function(){var e,n=_createForOfIteratorHelper(t.cookieGroups);try{for(n.s();!(e=n.n()).done;){var o=e.value;t.userConsent.cookieGroups[o]=!0}}catch(e){n.e(e)}finally{n.f()}t.syncConsentToCheckboxes(),t.saveConsent()}))}},{key:"handleButtonAcceptNecessary",value:function handleButtonAcceptNecessary(e){var t=this;e.addEventListener("click",(function(){t.userConsent=t.getDefaultConsent(),t.syncConsentToCheckboxes(),t.saveConsent()}))}},{key:"handleButtonChoose",value:function handleButtonChoose(e){var t=this;e.addEventListener("click",(function(){t.showOptions()}))}},{key:"handleButtonToggle",value:function handleButtonToggle(e){var t=this;e.addEventListener("click",(function(){for(var e in t.elements.checkboxes)t.elements.checkboxes[e].checked=t.toggleToStatus;t.toggleToStatus=!t.toggleToStatus}))}},{key:"handleButtonSave",value:function handleButtonSave(e){var t=this;e.addEventListener("click",(function(){var e,n=_createForOfIteratorHelper(t.cookieGroups);try{for(n.s();!(e=n.n()).done;){var o=e.value;"necessary"!==o&&(t.userConsent.cookieGroups[o]=t.elements.checkboxes[o].checked)}}catch(e){n.e(e)}finally{n.f()}t.saveConsent()}))}},{key:"handleButtonAskForConsent",value:function handleButtonAskForConsent(e){var t=this;e.addEventListener("click",(function(){var n=e.dataset;t.userConsent.cookieGroups[n.consentCategory]=!0,t.syncConsentToCheckboxes(),t.saveConsent(),e.parentElement.remove()}))}},{key:"handleButtonExternalTrigger",value:function handleButtonExternalTrigger(e){var t=this;e.addEventListener("click",(function(e){e.preventDefault(),t.showOptions()}))}},{key:"syncConsentToCheckboxes",value:function syncConsentToCheckboxes(){var e,t=_createForOfIteratorHelper(this.cookieGroups);try{for(t.s();!(e=t.n()).done;){var n=e.value;"necessary"!==n&&(this.elements.checkboxes[n].checked=this.userConsent.cookieGroups[n])}}catch(e){t.e(e)}finally{t.f()}}},{key:"checkForValidConsent",value:function checkForValidConsent(){return this.userConsent.version>0&&this.userConsent.version===this.settings.version||(this.settings.bots?this.checkForBots():this.settings.dnt&&!0===this.checkForUsersDNT())}},{key:"checkForUsersDNT",value:function checkForUsersDNT(){return!(!this.settings.dnt||"1"!==navigator.doNotTrack)&&(this.userConsent=this.getDefaultConsent(),this.saveConsent(!0),!0)}},{key:"detectRobot",value:function detectRobot(){return new RegExp([/bot/,/spider/,/crawl/,/APIs-Google/,/AdsBot/,/Googlebot/,/mediapartners/,/Google Favicon/,/Google Page Speed Insights/,/Chrome-Lighthouse/,/FeedFetcher/,/Google-Read-Aloud/,/DuplexWeb-Google/,/googleweblight/,/bing/,/yandex/,/baidu/,/duckduck/,/yahoo/,/ecosia/,/ia_archiver/,/facebook/,/instagram/,/pinterest/,/reddit/,/slack/,/twitter/,/whatsapp/,/youtube/,/semrush/].map((function(e){return e.source})).join("|"),"i").test(navigator.userAgent)}},{key:"checkForBots",value:function checkForBots(){return!!this.detectRobot()&&(this.userConsent=this.getDefaultConsent(),this.saveConsent(!0),!0)}},{key:"saveConsent",value:function saveConsent(){var e=arguments.length>0&&void 0!==arguments[0]&&arguments[0];this.userConsent.version=this.settings.version,window.localStorage.removeItem(this.name),window.localStorage.setItem(this.name,JSON.stringify(this.userConsent)),this.hideBannerAndOptions(),e||this.showMessage(),this.checkElementsWithRequiredConsent(),this.triggerCustomFunction()}},{key:"triggerCustomFunction",value:function triggerCustomFunction(){this.settings.customFunction.length&&"function"==typeof window[this.settings.customFunction]&&window[this.settings.customFunction]()}},{key:"hideBannerAndOptions",value:function hideBannerAndOptions(){this.elements.banner.wrapper.classList.remove("show-banner","show-options"),document.body.classList.remove("has-privacywire-window-opened"),document.dispatchEvent(new CustomEvent("PrivacyWireBannerAndOptionsClosed"))}},{key:"showBanner",value:function showBanner(){this.elements.banner.wrapper.classList.add("show-banner"),document.body.classList.add("has-privacywire-window-opened"),document.dispatchEvent(new CustomEvent("PrivacyWireBannerOpened"))}},{key:"showOptions",value:function showOptions(){this.elements.banner.wrapper.classList.remove("show-banner"),this.elements.banner.wrapper.classList.add("show-options"),document.body.classList.add("has-privacywire-window-opened"),document.dispatchEvent(new CustomEvent("PrivacyWireOptionsOpened"))}},{key:"showMessage",value:function showMessage(){var e=this;this.elements.banner.wrapper.classList.add("show-message"),setTimeout((function(){e.elements.banner.wrapper.classList.remove("show-message")}),this.settings.messageTimeout)}},{key:"checkElementsWithRequiredConsent",value:function checkElementsWithRequiredConsent(){if(!1===this.settings.consentByClass&&(this.elements.elementsWithRequiredConsent=document.querySelectorAll("[data-category]")),this.cleanOldConsentWindows(),this.elements.elementsWithRequiredConsent){var e=this;Array.from(this.elements.elementsWithRequiredConsent).forEach((function(t){var n=t.dataset.category;if(n){var o=!1;for(var s in e.userConsent.cookieGroups)if(s===n&&!0===e.userConsent.cookieGroups[s]){o=!0;break}o?e.updateAllowedElement(t):e.updateDisallowedElement(t)}}))}}},{key:"cleanOldConsentWindows",value:function cleanOldConsentWindows(){var e=this;this.elements.consentWindows&&Array.from(this.elements.consentWindows).forEach((function(t){var n=t.dataset.disallowedConsentCategory,o=!1;for(var s in e.userConsent.cookieGroups)if(s===n&&!0===e.userConsent.cookieGroups[s]){o=!0;break}o&&t.remove()}))}},{key:"updateDisallowedElement",value:function updateDisallowedElement(e){var t=e.dataset;if(t.askConsent&&"1"!==t.askConsentRendered){var n=t.category,o=this.settings.cookieGroups[n],s=document.createElement("div");s.classList.add("privacywire-ask-consent","consent-category-"+n),s.dataset.disallowedConsentCategory=n,s.innerHTML=this.elements.blueprint.innerHTML.formatUnicorn({category:o,categoryname:n}),e.insertAdjacentElement("afterend",s),e.dataset.askConsentRendered="1"}}},{key:"updateAllowedElement",value:function updateAllowedElement(e){"script"===e.tagName.toLowerCase()?this.updateAllowedElementScript(e):this.updateAllowedElementOther(e)}},{key:"updateAllowedElementScript",value:function updateAllowedElementScript(e){for(var t=e.dataset,n=document.createElement(e.tagName),o=0,s=Object.keys(t);o<s.length;o++){var r=s[o];n.dataset[r]=e.dataset[r]}n.type=t.type,t.src&&(n.src=t.src),n.textContent=e.textContent,e.id&&(n.id=e.id),n.defer=e.defer,n.async=e.async,n=this.removeUnusedAttributesFromElement(n),e.insertAdjacentElement("afterend",n),e.remove()}},{key:"updateAllowedElementOther",value:function updateAllowedElementOther(e){var t,n=e.dataset;e.type=null!==(t=n.type)&&void 0!==t?t:"text/javascript",e.src=n.src,e.srcset=n.srcset,this.removeUnusedAttributesFromElement(e)}},{key:"removeUnusedAttributesFromElement",value:function removeUnusedAttributesFromElement(e){return e.removeAttribute("data-ask-consent"),e.removeAttribute("data-ask-consent-rendered"),e.removeAttribute("data-category"),e.removeAttribute("data-src"),e.removeAttribute("data-srcset"),e.removeAttribute("data-type"),e.classList.remove("require-consent"),e}},{key:"refresh",value:function refresh(){this.checkElementsWithRequiredConsent(),this.handleButtonHelper(this.elements.buttons.askForConsent,"handleButtonAskForConsent")}},{key:"removeStoredConsent",value:function removeStoredConsent(){window.localStorage.getItem(this.name)&&window.localStorage.removeItem(this.name)}}]),PrivacyWire}();document.addEventListener("DOMContentLoaded",(function(){window.PrivacyWire=new e(PrivacyWireSettings)}))}(); |