UNPKG

@netresearch/usercentrics-widgets

Version:

Lightweight placeholders compatible with Usercentrics CMP

2 lines (1 loc) 4.52 kB
!function(){"use strict";class t{waitForCmp(t){window.UC_UI&&window.UC_UI.isInitialized()?t():window.addEventListener("UC_UI_INITIALIZED",(function(e){t()}))}waitForCmpConsent(t,e){this.waitForCmp((()=>{!0===this.getConsent(t)&&e()}))}isCmpReady(){return window.UC_UI&&window.UC_UI.isInitialized()}setConsent(t){if(!this.isCmpReady())throw new Error("Usercentrics CMP is not ready!");window.UC_UI.acceptService(t)}getConsent(t){try{const e=window.UC_UI.getServicesBaseInfo();for(let i=0;i<e.length;i++)if(e[i].id===t)return!!e[i].consent.status;return!1}catch(t){return!1}}}const e=new class{constructor(){this.store={}}register(t,e){this.store[t]||(this.store[t]=[]),this.store[t].push(e)}unregister(t,e){const i=this.store[t];if(i)for(let t=0;t<i.length;t++)if(i[t]===e){delete i[t];break}}unregisterAll(t){this.store[t]=[]}activate(t){const e=this.store[t];if(e)for(let t=0;t<e.length;t++)e[t]&&e[t].activate(!1);this.unregisterAll(t)}linkCmp(){const e=new t;for(const t of Object.keys(this.store))e.waitForCmpConsent(t,(()=>this.activate(t)));window.addEventListener("UC_UI_VIEW_CHANGED",(t=>{if(!t.detail||"NONE"!==t.detail.previousView&&"PRIVACY_BUTTON"!==t.detail.previousView)for(const t of Object.keys(this.store))e.waitForCmpConsent(t,(()=>this.activate(t)))}))}},i=document.documentElement.lang;let s,n,r;"de-DE"===i||"de"===i||"DE"===i?(s="Wir nutzen den Service ",n=" um Inhalte einzubetten. Dieser Service kann Daten zu Ihren Aktivitäten sammeln. Stimmen Sie der Nutzung des Service zu, um diese Inhalte anzuzeigen.",r="Akzeptieren"):(s="We use the service ",n=" to embed content. This service may collect data about your activities. Agree to use the Service to view this content.",r="Accept");class c{constructor(t){this.el=t,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")}}getEmbeddingText(){return this.cfg.text||s+this.cfg.ucName+n}getAcceptButtonLabel(){return this.cfg.accept||r}getEmbedding(){return`<img class="uc-widget-background" src="${this.getBackground()}" alt="Background Image" width="100%" height="100%"/><div class="uc-widget-embedding"> <div class="uc-widget-text">${this.getEmbeddingText()}</div> <div class="uc-widget-control"><button class="uc-widget-accept">${this.getAcceptButtonLabel()}</button></div></div>`}getBackground(){return this.el.getAttribute("data-uc-background-image")??"data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="}activate(i){const s=this.cfg.ucId;if(e.unregister(s,this),e.activate(s),i){(new t).setConsent(s)}}render(){const{width:t,height:i}=this.el.getBoundingClientRect(),s=document.createElement("div");s.innerHTML=this.getEmbedding(),s.setAttribute("class","uc-widget-container"),s.setAttribute("width",`${Math.floor(t)}px`),s.setAttribute("height",`${Math.floor(i)}px`),this.el.replaceWith(s),s.getElementsByClassName("uc-widget-accept")[0].addEventListener("click",this.activate.bind(this,!0)),this.container=s,e.register(this.cfg.ucId,this)}}class a extends c{activate(t){super.activate(t);const e=this.el;if(e){const t=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((()=>{e.setAttribute("src",t)}),0)}}}class o{constructor(t){this.url=t;const e=t.split("/");this._protocol=e.shift(),e.shift(),this._host=e.shift();const i=e.join("/").split(/[?#]/);this._pathname=i[0]}get host(){return this._host}get protocol(){return this._protocol}get pathname(){return this._pathname}}class u extends a{getBackground(){const t=this.el.getAttribute("data-uc-src"),e=this.el.hasAttribute("data-uc-background-image");if(!t||e)return super.getBackground();try{const e=new o(t);return`https://privacy-proxy-server.usercentrics.eu/video/youtube/${e.pathname.split("/").pop()}-poster-image`}catch(t){return super.getBackground()}}}class d{static create(t){try{const e=new o(t.getAttribute("data-uc-src")).host;t.tagName.toLowerCase();return/\.youtube(-nocookie)?\./.test(e)?new u(t):new a(t)}catch(e){return new a(t)}}}function h(){if("complete"!==document.readyState)return;document.removeEventListener("readystatechange",h);const t=(i=document.querySelectorAll("[data-uc-src]"),Array.prototype.slice.call(i));var i;for(const e of t){d.create(e).render()}e.linkCmp()}"complete"===document.readyState?h():document.addEventListener("readystatechange",h)}();