@netresearch/usercentrics-widgets
Version:
Lightweight placeholders compatible with Usercentrics CMP
2 lines (1 loc) • 9.02 kB
JavaScript
!function(){"use strict";function t(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,(i=r.key,o=void 0,"symbol"==typeof(o=function(t,e){if("object"!=typeof t||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(i,"string"))?o:String(o)),r)}var i,o}function n(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&o(t,e)}function i(t){return i=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},i(t)}function o(t,e){return o=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},o(t,e)}function c(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function a(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,r=i(t);if(e){var o=i(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return c(this,n)}}function u(){return u="undefined"!=typeof Reflect&&Reflect.get?Reflect.get.bind():function(t,e,n){var r=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=i(t)););return t}(t,e);if(r){var o=Object.getOwnPropertyDescriptor(r,e);return o.get?o.get.call(arguments.length<3?t:n):o.value}},u.apply(this,arguments)}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function l(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return s(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}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 o,c=!0,a=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return c=t.done,t},e:function(t){a=!0,o=t},f:function(){try{c||null==n.return||n.return()}finally{if(a)throw o}}}}var f,h,d,v=function(){function e(){t(this,e)}return n(e,[{key:"waitForCmp",value:function(t){window.UC_UI&&window.UC_UI.isInitialized()?t():window.addEventListener("UC_UI_INITIALIZED",(function(e){t()}))}},{key:"waitForCmpConsent",value:function(t,e){var n=this;this.waitForCmp((function(){!0===n.getConsent(t)&&e()}))}},{key:"isCmpReady",value:function(){return window.UC_UI&&window.UC_UI.isInitialized()}},{key:"setConsent",value:function(t){if(!this.isCmpReady())throw new Error("Usercentrics CMP is not ready!");window.UC_UI.acceptService(t)}},{key:"getConsent",value:function(t){try{for(var e=window.UC_UI.getServicesBaseInfo(),n=0;n<e.length;n++)if(e[n].id===t)return!!e[n].consent.status;return!1}catch(t){return!1}}}]),e}(),p=new(function(){function e(){t(this,e),this.store={}}return n(e,[{key:"register",value:function(t,e){this.store[t]||(this.store[t]=[]),this.store[t].push(e)}},{key:"unregister",value:function(t,e){var n=this.store[t];if(n)for(var r=0;r<n.length;r++)if(n[r]===e){delete n[r];break}}},{key:"unregisterAll",value:function(t){this.store[t]=[]}},{key:"activate",value:function(t){var e=this.store[t];if(e)for(var n=0;n<e.length;n++)e[n]&&e[n].activate(!1);this.unregisterAll(t)}},{key:"linkCmp",value:function(){for(var t=this,e=new v,n=function(){var n=i[r];e.waitForCmpConsent(n,(function(){return t.activate(n)}))},r=0,i=Object.keys(this.store);r<i.length;r++)n();window.addEventListener("UC_UI_VIEW_CHANGED",(function(n){if(!n.detail||"NONE"!==n.detail.previousView&&"PRIVACY_BUTTON"!==n.detail.previousView)for(var r=function(){var n=o[i];e.waitForCmpConsent(n,(function(){return t.activate(n)}))},i=0,o=Object.keys(t.store);i<o.length;i++)r()}))}}]),e}()),y=document.documentElement.lang;"de-DE"===y||"de"===y||"DE"===y?(f="Wir nutzen den Service ",h=" um Inhalte einzubetten. Dieser Service kann Daten zu Ihren Aktivitäten sammeln. Stimmen Sie der Nutzung des Service zu, um diese Inhalte anzuzeigen.",d="Akzeptieren"):(f="We use the service ",h=" to embed content. This service may collect data about your activities. Agree to use the Service to view this content.",d="Accept");var g=function(e){r(c,e);var o=a(c);function c(){return t(this,c),o.apply(this,arguments)}return n(c,[{key:"activate",value:function(t){u(i(c.prototype),"activate",this).call(this,t);var e=this.el;if(e){var n=e.getAttribute("data-uc-src");e.setAttribute("data-uc-src",null),e.hasAttribute("type")&&e.removeAttribute("type"),this.container.parentElement.replaceChild(e,this.container),window.setTimeout((function(){e.setAttribute("src",n)}),0)}}}]),c}(function(){function e(n){t(this,e),this.el=n,this.cfg={ucId:this.el.getAttribute("data-uc-id"),ucName:this.el.getAttribute("data-usercentrics"),text:this.el.getAttribute("data-text"),accept:this.el.getAttribute("data-accept")}}return n(e,[{key:"getEmbeddingText",value:function(){return this.cfg.text||f+this.cfg.ucName+h}},{key:"getAcceptButtonLabel",value:function(){return this.cfg.accept||d}},{key:"getEmbedding",value:function(){return'<img class="uc-widget-background" src="'.concat(this.getBackground(),'" alt="Background Image" width="100%" height="100%"/><div class="uc-widget-embedding"> <div class="uc-widget-text">').concat(this.getEmbeddingText(),'</div> <div class="uc-widget-control"><button class="uc-widget-accept">').concat(this.getAcceptButtonLabel(),"</button></div></div>")}},{key:"getBackground",value:function(){var t=this.el.getAttribute("data-uc-background-image");return null!=t?t:"data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="}},{key:"activate",value:function(t){var e=this.cfg.ucId;(p.unregister(e,this),p.activate(e),t)&&(new v).setConsent(e)}},{key:"render",value:function(){var t=this.el.getBoundingClientRect(),e=t.width,n=t.height,r=document.createElement("div");r.innerHTML=this.getEmbedding(),r.setAttribute("class","uc-widget-container"),r.setAttribute("width","".concat(Math.floor(e),"px")),r.setAttribute("height","".concat(Math.floor(n),"px")),this.el.replaceWith(r),r.getElementsByClassName("uc-widget-accept")[0].addEventListener("click",this.activate.bind(this,!0)),this.container=r,p.register(this.cfg.ucId,this)}}]),e}()),b=function(){function e(n){t(this,e),this.url=n;var r=n.split("/");this._protocol=r.shift(),r.shift(),this._host=r.shift();var i=r.join("/").split(/[?#]/);this._pathname=i[0]}return n(e,[{key:"host",get:function(){return this._host}},{key:"protocol",get:function(){return this._protocol}},{key:"pathname",get:function(){return this._pathname}}]),e}(),m=function(e){r(c,e);var o=a(c);function c(){return t(this,c),o.apply(this,arguments)}return n(c,[{key:"getBackground",value:function(){var t=this.el.getAttribute("data-uc-src"),e=this.el.hasAttribute("data-uc-background-image");if(!t||e)return u(i(c.prototype),"getBackground",this).call(this);try{var n=new b(t).pathname.split("/").pop();return"https://privacy-proxy-server.usercentrics.eu/video/youtube/".concat(n,"-poster-image")}catch(t){return u(i(c.prototype),"getBackground",this).call(this)}}}]),c}(g),w=function(){function e(){t(this,e)}return n(e,null,[{key:"create",value:function(t){try{var e=new b(t.getAttribute("data-uc-src")).host;t.tagName.toLowerCase();return/\.youtube(-nocookie)?\./.test(e)?new m(t):new g(t)}catch(e){return new g(t)}}}]),e}();function A(){if("complete"===document.readyState){document.removeEventListener("readystatechange",A);var t,e,n=l((t=document.querySelectorAll("[data-uc-src]"),Array.prototype.slice.call(t)));try{for(n.s();!(e=n.n()).done;){var r=e.value;w.create(r).render()}}catch(t){n.e(t)}finally{n.f()}p.linkCmp()}}"complete"===document.readyState?A():document.addEventListener("readystatechange",A)}();