cookie-consent-api
Version:
A flexible JS API to manage cookie consent for GDPR
1 lines • 13.5 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.CookieConsentApi=t():e.CookieConsentApi=t()}(window,(function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{enumerable:!0,get:r})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,n){if(1&n&&(e=t(e)),8&n)return e;if(4&n&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(t.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&n&&"string"!=typeof e)for(var o in e)t.d(r,o,function(t){return e[t]}.bind(null,o));return r},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="./",t(t.s=2)}([function(e,t,n){function r(e){return(r="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})(e)}var o,i;!function(s){var u;if(void 0!==(i="function"==typeof(o=s)?o.call(t,n,t,e):o)&&(e.exports=i),u=!0,"object"===r(t)&&(e.exports=s(),u=!0),!u){var c=window.Cookies,f=window.Cookies=s();f.noConflict=function(){return window.Cookies=c,f}}}((function(){function e(){for(var e=0,t={};e<arguments.length;e++){var n=arguments[e];for(var r in n)t[r]=n[r]}return t}function t(e){return e.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}return function n(r){function o(){}function i(t,n,i){if("undefined"!=typeof document){"number"==typeof(i=e({path:"/"},o.defaults,i)).expires&&(i.expires=new Date(1*new Date+864e5*i.expires)),i.expires=i.expires?i.expires.toUTCString():"";try{var s=JSON.stringify(n);/^[\{\[]/.test(s)&&(n=s)}catch(e){}n=r.write?r.write(n,t):encodeURIComponent(n+"").replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),t=encodeURIComponent(t+"").replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var u="";for(var c in i)i[c]&&(u+="; "+c,!0!==i[c])&&(u+="="+i[c].split(";")[0]);return document.cookie=t+"="+n+u}}function s(e,n){if("undefined"!=typeof document){for(var o={},i=document.cookie?document.cookie.split("; "):[],s=0;s<i.length;s++){var u=i[s].split("="),c=u.slice(1).join("=");n||'"'!==c.charAt(0)||(c=c.slice(1,-1));try{var f=t(u[0]);if(c=(r.read||r)(c,f)||t(c),n)try{c=JSON.parse(c)}catch(e){}if(o[f]=c,e===f)break}catch(e){}}return e?o[e]:o}}return o.set=i,o.get=function(e){return s(e,!1)},o.getJSON=function(e){return s(e,!0)},o.remove=function(t,n){i(t,"",e(n,{expires:-1}))},o.defaults={},o.withConverter=n,o}((function(){}))}))},function(e){"use strict";function t(e){return(t="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})(e)}function n(){n.init.call(this)}function r(e){if("function"!=typeof e)throw new TypeError('The "listener" argument must be of type Function. Received type '+t(e))}function o(e){return void 0===e._maxListeners?n.defaultMaxListeners:e._maxListeners}function i(e,t,n,i){var s,u,c;if(r(n),void 0===(u=e._events)?(u=e._events=Object.create(null),e._eventsCount=0):(void 0!==u.newListener&&(e.emit("newListener",t,n.listener?n.listener:n),u=e._events),c=u[t]),void 0===c)c=u[t]=n,++e._eventsCount;else if("function"==typeof c?c=u[t]=i?[n,c]:[c,n]:i?c.unshift(n):c.push(n),0<(s=o(e))&&c.length>s&&!c.warned){c.warned=!0;var f=new Error("Possible EventEmitter memory leak detected. "+c.length+" "+t+" listeners added. Use emitter.setMaxListeners() to increase limit");f.name="MaxListenersExceededWarning",f.emitter=e,f.type=t,f.count=c.length,function(e){console&&console.warn&&console.warn(e)}(f)}return e}function s(){if(!this.fired)return this.target.removeListener(this.type,this.wrapFn),this.fired=!0,0===arguments.length?this.listener.call(this.target):this.listener.apply(this.target,arguments)}function u(e,t,n){var r={fired:!1,wrapFn:void 0,target:e,type:t,listener:n},o=s.bind(r);return o.listener=n,r.wrapFn=o,o}function c(e,t,n){var r=e._events;if(void 0===r)return[];var o=r[t];return void 0===o?[]:"function"==typeof o?n?[o.listener||o]:[o]:n?function(e){for(var t=Array(e.length),n=0;n<t.length;++n)t[n]=e[n].listener||e[n];return t}(o):a(o,o.length)}function f(e){var t=this._events;if(void 0!==t){var n=t[e];if("function"==typeof n)return 1;if(void 0!==n)return n.length}return 0}function a(e,t){for(var n=Array(t),r=0;r<t;++r)n[r]=e[r];return n}var l,p="object"===("undefined"==typeof Reflect?"undefined":t(Reflect))?Reflect:null,v=p&&"function"==typeof p.apply?p.apply:function(e,t,n){return Function.prototype.apply.call(e,t,n)};l=p&&"function"==typeof p.ownKeys?p.ownKeys:Object.getOwnPropertySymbols?function(e){return Object.getOwnPropertyNames(e).concat(Object.getOwnPropertySymbols(e))}:function(e){return Object.getOwnPropertyNames(e)};var h=Number.isNaN||function(e){return e!=e};e.exports=n,e.exports.once=function(e,t){return new Promise((function(n,r){function o(){void 0!==i&&e.removeListener("error",i),n([].slice.call(arguments))}var i;"error"!==t&&(i=function(n){e.removeListener(t,o),r(n)},e.once("error",i)),e.once(t,o)}))},n.EventEmitter=n,n.prototype._events=void 0,n.prototype._eventsCount=0,n.prototype._maxListeners=void 0;var y=10;Object.defineProperty(n,"defaultMaxListeners",{enumerable:!0,get:function(){return y},set:function(e){if("number"!=typeof e||0>e||h(e))throw new RangeError('The value of "defaultMaxListeners" is out of range. It must be a non-negative number. Received '+e+".");y=e}}),n.init=function(){(void 0===this._events||this._events===Object.getPrototypeOf(this)._events)&&(this._events=Object.create(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0},n.prototype.setMaxListeners=function(e){if("number"!=typeof e||0>e||h(e))throw new RangeError('The value of "n" is out of range. It must be a non-negative number. Received '+e+".");return this._maxListeners=e,this},n.prototype.getMaxListeners=function(){return o(this)},n.prototype.emit=function(e){for(var t=[],n=1;n<arguments.length;n++)t.push(arguments[n]);var r="error"===e,o=this._events;if(void 0!==o)r=r&&void 0===o.error;else if(!r)return!1;if(r){var i;if(0<t.length&&(i=t[0]),i instanceof Error)throw i;var s=new Error("Unhandled error."+(i?" ("+i.message+")":""));throw s.context=i,s}var u=o[e];if(void 0===u)return!1;if("function"==typeof u)v(u,this,t);else{var c=u.length,f=a(u,c);for(n=0;n<c;++n)v(f[n],this,t)}return!0},n.prototype.addListener=function(e,t){return i(this,e,t,!1)},n.prototype.on=n.prototype.addListener,n.prototype.prependListener=function(e,t){return i(this,e,t,!0)},n.prototype.once=function(e,t){return r(t),this.on(e,u(this,e,t)),this},n.prototype.prependOnceListener=function(e,t){return r(t),this.prependListener(e,u(this,e,t)),this},n.prototype.removeListener=function(e,t){var n,o,i,s,u;if(r(t),void 0===(o=this._events))return this;if(void 0===(n=o[e]))return this;if(n===t||n.listener===t)0==--this._eventsCount?this._events=Object.create(null):(delete o[e],o.removeListener&&this.emit("removeListener",e,n.listener||t));else if("function"!=typeof n){for(i=-1,s=n.length-1;0<=s;s--)if(n[s]===t||n[s].listener===t){u=n[s].listener,i=s;break}if(0>i)return this;0===i?n.shift():function(e,t){for(;t+1<e.length;t++)e[t]=e[t+1];e.pop()}(n,i),1===n.length&&(o[e]=n[0]),void 0!==o.removeListener&&this.emit("removeListener",e,u||t)}return this},n.prototype.off=n.prototype.removeListener,n.prototype.removeAllListeners=function(e){var t,n,r;if(void 0===(n=this._events))return this;if(void 0===n.removeListener)return 0===arguments.length?(this._events=Object.create(null),this._eventsCount=0):void 0!==n[e]&&(0==--this._eventsCount?this._events=Object.create(null):delete n[e]),this;if(0===arguments.length){var o,i=Object.keys(n);for(r=0;r<i.length;++r)"removeListener"===(o=i[r])||this.removeAllListeners(o);return this.removeAllListeners("removeListener"),this._events=Object.create(null),this._eventsCount=0,this}if("function"==typeof(t=n[e]))this.removeListener(e,t);else if(void 0!==t)for(r=t.length-1;0<=r;r--)this.removeListener(e,t[r]);return this},n.prototype.listeners=function(e){return c(this,e,!0)},n.prototype.rawListeners=function(e){return c(this,e,!1)},n.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):f.call(e,t)},n.prototype.listenerCount=f,n.prototype.eventNames=function(){return 0<this._eventsCount?l(this._events):[]}},function(e,t,n){"use strict";function r(e){return(r="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})(e)}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n,r=0;r<t.length;r++)(n=t[r]).enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}function s(e,t){return(s=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function u(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,r=f(e);if(t){var o=f(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return c(this,n)}}function c(e,t){return!t||"object"!==r(t)&&"function"!=typeof t?function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(e):t}function f(e){return(f=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}n.r(t);var a=n(0),l=n.n(a),p=n(1),v=function(e){function t(){var e,r=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};return o(this,t),(e=n.call(this))._conf=Object.assign({},{cookieName:"cookie_consent_settings",cookieDuration:365,cookieDomain:null,services:[]},r),e._validateConf(),e._updateView(),e}!function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&s(e,t)}(t,e);var n=u(t);return function(e,t,n){t&&i(e.prototype,t),n&&i(e,n)}(t,[{key:"reset",value:function(){l.a.remove(this._conf.cookieName,{domain:this._conf.cookieDomain}),this._updateView(),this.emit("clear")}},{key:"acceptAll",value:function(){var e={};this._conf.services.forEach((function(t){return e[t]=!0})),this._setCookieServices(e),this._updateView(),this.emit("allConfigured")}},{key:"accept",value:function(e){var t=this._getCookieServices();t[e]=!0,this._setCookieServices(t),this._updateView(),this.emit("accept",e),this.isAllConfigured()&&this.emit("allConfigured")}},{key:"refuse",value:function(e){var t=this._getCookieServices();t[e]=!1,this._setCookieServices(t),this._updateView(),this.emit("refuse",e),this.isAllConfigured()&&this.emit("allConfigured")}},{key:"refuseAll",value:function(){var e={};this._conf.services.forEach((function(t){return e[t]=!1})),this._setCookieServices(e),this._updateView(),this.emit("allConfigured")}},{key:"isAllConfigured",value:function(){var e=this._getCookieServices(),t=!0;return this._conf.services.forEach((function(n){void 0===e[n]&&(t=!1)})),t}},{key:"isConfigured",value:function(e){return void 0!==this._getCookieServices()[e]}},{key:"isAccepted",value:function(e){var t=this._getCookieServices();return void 0!==t[e]&&!0===t[e]}},{key:"isRefused",value:function(e){var t=this._getCookieServices();return void 0!==t[e]&&!1===t[e]}},{key:"getServices",value:function(){return this._conf.services}},{key:"_getCookieServices",value:function(){return l.a.getJSON(this._conf.cookieName)||{}}},{key:"_setCookieServices",value:function(e){l.a.set(this._conf.cookieName,e,{expires:this._conf.cookieDuration,domain:this._conf.cookieDomain})}},{key:"_validateConf",value:function(){Array.isArray(this._conf.services)?this._conf.services.forEach((function(e){!1===/^[a-zA-Z0-9]+$/.test(e)&&console.error('CCM: "'+e+'" is not a valid service name, only alphanumeric allowed')})):console.error("CCM: Services is not an array")}},{key:"_updateView",value:function(){var e=this,t=this._getCookieServices();this._conf.services.forEach((function(n){var r=document.querySelectorAll('[data-cookie-consent="'+n+'"]');!0===t[n]?r.forEach((function(t){t.getAttribute("data-ccm-fallback")||t.setAttribute("data-ccm-fallback",t.innerHTML);var n=/<!--if-consent([^]+)endif-->/g.exec(t.innerHTML);n&&2===n.length&&(t.innerHTML=n[1],e._executeScripts(t))})):r.forEach((function(t){var n=t.getAttribute("data-ccm-fallback");n&&(t.innerHTML=n,e._executeScripts(t))}))}))}},{key:"_executeScripts",value:function(e){e.querySelectorAll("script").forEach((function(t){var n=document.createElement("script");n.innerHTML=t.innerHTML,t.remove(),e.append(n)}))}}]),t}(n.n(p).a);t.default=v}]).default}));