2026-02-05 17:08:59 +03:00

1 line
3.2 KiB
JavaScript

if("undefined"==typeof LiteYTEmbed){class e extends HTMLElement{static get observedAttributes(){return["class"]}connectedCallback(){void 0!==window.youtubePreviewImageObserver&&window.youtubePreviewImageObserver.observe(this)}attributeChangedCallback(t,i,a){if(-1===a.indexOf("lty-load"))return;this.videoId=this.getAttribute("videoid");let n=this.querySelector(".lty-playbtn");if(this.playLabel=n&&n.textContent.trim()||this.getAttribute("playlabel")||"Play",!this.style.backgroundImage){let e=this.getAttribute("width"),t=this.getAttribute("data-thumbnail-size")+".jpg";"auto.jpg"===t&&(t=640<e?"maxresdefault.jpg":480<e?"sddefault.jpg":"hqdefault.jpg"),this.style.backgroundImage=`url("https://i.ytimg.com/vi/${this.videoId}/${t}")`}if(n||(n=document.createElement("button"),n.type="button",n.classList.add("lty-playbtn"),this.append(n)),!n.textContent){const e=document.createElement("span");e.className="lyt-visually-hidden",e.textContent=this.playLabel,n.append(e)}n.removeAttribute("href"),this.addEventListener("pointerover",e.warmConnections,{once:!0}),this.addEventListener("click",this.addIframe),this.needsYTApiForAutoplay=navigator.vendor.includes("Apple")||navigator.userAgent.includes("Mobi")}static addPrefetch(e,t,i){const a=document.createElement("link");a.rel=e,a.href=t,i&&(a.as=i),document.head.append(a)}static warmConnections(){e.preconnected||(e.addPrefetch("preconnect","https://www.youtube-nocookie.com"),e.addPrefetch("preconnect","https://www.google.com"),e.addPrefetch("preconnect","https://googleads.g.doubleclick.net"),e.addPrefetch("preconnect","https://static.doubleclick.net"),e.preconnected=!0)}fetchYTPlayerApi(){window.YT||window.YT&&window.YT.Player||(this.ytApiPromise=new Promise((e,t)=>{var i=document.createElement("script");i.src="https://www.youtube.com/iframe_api",i.async=!0,i.onload=t=>{YT.ready(e)},i.onerror=t,this.append(i)}))}async addYTPlayerIframe(e){this.fetchYTPlayerApi(),await this.ytApiPromise;const t=document.createElement("div");this.append(t);const i=Object.fromEntries(e.entries());new YT.Player(t,{width:"100%",videoId:this.videoId,playerVars:i,events:{onReady:e=>{e.target.playVideo()}}})}async addIframe(){if(this.classList.contains("lyt-activated"))return;this.classList.add("lyt-activated");const e=new URLSearchParams(this.getAttribute("params")||[]);if(e.append("autoplay","1"),e.append("playsinline","1"),this.needsYTApiForAutoplay)return this.addYTPlayerIframe(e);let t="https://www.youtube-nocookie.com/embed/";void 0!==this.dataset.noCookie&&"off"===this.dataset.noCookie&&(t="https://www.youtube.com/embed/");const i=document.createElement("iframe");i.width=560,i.height=315,i.title=this.playLabel,i.allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture",i.allowFullscreen=!0,i.src=`${t}${encodeURIComponent(this.videoId)}?${e.toString()}`,this.append(i),i.focus()}}if("IntersectionObserver"in window){const e={root:null,rootMargin:"0px",threshold:0};window.youtubePreviewImageObserver=new IntersectionObserver((function(e){jQuery.each(e,(function(e,t){t.isIntersecting&&(t.target.classList.add("lty-load"),window.youtubePreviewImageObserver.unobserve(t.target))}))}),e)}else jQuery("lite-youtube").addClass("lty-load");void 0===customElements.get("lite-youtube")&&customElements.define("lite-youtube",e)}