/* * Modified version of Lite YouTube Embed * Original source: https://github.com/paulirish/lite-youtube-embed * Original license: Apache 2.0 */ // BELOW IS A DRAFT/MODIFIED VERSION OF THE UPSTREAM SCRIPT ABOVE! // See https://eternodevir.com/palimpsests/logs/lazy-loading-youtube-embeds/ for more info. class LiteYTEmbed extends HTMLElement { connectedCallback() { this.videoId = this.getAttribute('videoid'); this.posterId = this.getAttribute('poster-id') || this.videoId; let playBtnEl = this.querySelector('.lyt-playbtn,.lty-playbtn'); this.playLabel = (playBtnEl && playBtnEl.textContent.trim()) || this.getAttribute('playlabel') || 'Play'; this.dataset.title = this.getAttribute('title') || ""; if (!this.style.backgroundImage && this.posterId) { const quality = this.getAttribute('poster-quality') || 'hq'; this.style.backgroundImage = `url("https://i.ytimg.com/vi/${this.posterId}/${quality}default.jpg")`; } if (!playBtnEl) { playBtnEl = document.createElement('button'); playBtnEl.type = 'button'; playBtnEl.classList.add('lyt-playbtn', 'lty-playbtn'); this.append(playBtnEl); } if (!playBtnEl.textContent) { const playBtnLabelEl = document.createElement('span'); playBtnLabelEl.className = 'lyt-visually-hidden'; playBtnLabelEl.textContent = this.playLabel; playBtnEl.append(playBtnLabelEl); } this.addNoscriptIframe(); if(playBtnEl.nodeName === 'A'){ playBtnEl.removeAttribute('href'); playBtnEl.setAttribute('tabindex', '0'); playBtnEl.setAttribute('role', 'button'); playBtnEl.addEventListener('keydown', e => { if( e.key === 'Enter' || e.key === ' ' ){ e.preventDefault(); this.activate(); } }); } this.addEventListener('pointerover', LiteYTEmbed.warmConnections, {once: true}); this.addEventListener('focusin', LiteYTEmbed.warmConnections, {once: true}); this.addEventListener('click', this.activate); } static addPrefetch(kind, url, as) { const linkEl = document.createElement('link'); linkEl.rel = kind; linkEl.href = url; if (as) linkEl.as = as; document.head.append(linkEl); } static warmConnections() { if (LiteYTEmbed.preconnected) return; LiteYTEmbed.addPrefetch('preconnect', 'https://www.youtube-nocookie.com'); LiteYTEmbed.addPrefetch('preconnect', 'https://www.google.com'); LiteYTEmbed.addPrefetch('preconnect', 'https://googleads.g.doubleclick.net'); LiteYTEmbed.addPrefetch('preconnect', 'https://static.doubleclick.net'); LiteYTEmbed.preconnected = true; } addNoscriptIframe() { const iframeEl = this.createBasicIframe(); const noscriptEl = document.createElement('noscript'); noscriptEl.innerHTML = iframeEl.outerHTML; this.append(noscriptEl); } getParams() { const params = new URLSearchParams(this.getAttribute('params') || []); params.append('autoplay', '1'); params.append('playsinline', '1'); return params; } async activate(){ if (this.classList.contains('lyt-activated')) return; this.classList.add('lyt-activated'); this.append(this.createBasicIframe()); } createBasicIframe(){ const iframeEl = document.createElement('iframe'); iframeEl.width = 560; iframeEl.height = 315; iframeEl.title = this.playLabel; iframeEl.allow = 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture'; iframeEl.allowFullscreen = true; iframeEl.referrerPolicy = 'strict-origin-when-cross-origin'; const embedTarget = this.videoId ? encodeURIComponent(this.videoId) : 'videoseries'; iframeEl.src = `https://www.youtube-nocookie.com/embed/${embedTarget}?${this.getParams().toString()}`; return iframeEl; } } customElements.define('lite-youtube', LiteYTEmbed);