1/** 2 * A lightweight youtube embed. Still should feel the same to the user, just MUCH faster to initialize and paint. 3 * 4 * Thx to these as the inspiration 5 * https://storage.googleapis.com/amp-vs-non-amp/youtube-lazy.html 6 * https://autoplay-youtube-player.glitch.me/ 7 * 8 * Once built it, I also found these: 9 * https://github.com/ampproject/amphtml/blob/master/extensions/amp-youtube (ðð) 10 * https://github.com/Daugilas/lazyYT 11 * https://github.com/vb/lazyframe 12 */ 13class LiteYTEmbed extends HTMLElement { 14 connectedCallback() { 15 this.videoId = this.getAttribute('videoid'); 16 17 let playBtnEl = this.querySelector('.lty-playbtn'); 18 // A label for the button takes priority over a [playlabel] attribute on the custom-element 19 this.playLabel = (playBtnEl && playBtnEl.textContent.trim()) || this.getAttribute('playlabel') || 'Play'; 20 21 /** 22 * Lo, the youtube placeholder image! (aka the thumbnail, poster image, etc) 23 * 24 * See https://github.com/paulirish/lite-youtube-embed/blob/master/youtube-thumbnail-urls.md 25 * 26 * TODO: Do the sddefault->hqdefault fallback 27 * - When doing this, apply referrerpolicy (https://github.com/ampproject/amphtml/pull/3940) 28 * TODO: Consider using webp if supported, falling back to jpg 29 */ 30 this.posterUrl = `https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg`; 31 // Warm the connection for the poster image 32 LiteYTEmbed.addPrefetch('preload', this.posterUrl, 'image'); 33 34 this.style.backgroundImage = `url("${this.posterUrl}")`; 35 36 // Set up play button, and its visually hidden label 37 if (!playBtnEl) { 38 playBtnEl = document.createElement('button'); 39 playBtnEl.type = 'button'; 40 playBtnEl.classList.add('lty-playbtn'); 41 this.append(playBtnEl); 42 } 43 if (!playBtnEl.textContent) { 44 const playBtnLabelEl = document.createElement('span'); 45 playBtnLabelEl.className = 'lyt-visually-hidden'; 46 playBtnLabelEl.textContent = this.playLabel; 47 playBtnEl.append(playBtnLabelEl); 48 } 49 50 // On hover (or tap), warm up the TCP connections we're (likely) about to use. 51 this.addEventListener('pointerover', LiteYTEmbed.warmConnections, {once: true}); 52 53 // Once the user clicks, add the real iframe and drop our play button 54 // TODO: In the future we could be like amp-youtube and silently swap in the iframe during idle time 55 // We'd want to only do this for in-viewport or near-viewport ones: https://github.com/ampproject/amphtml/pull/5003 56 this.addEventListener('click', e => this.addIframe()); 57 } 58 59 // // TODO: Support the the user changing the [videoid] attribute 60 // attributeChangedCallback() { 61 // } 62 63 /** 64 * Add a <link rel={preload | preconnect} ...> to the head 65 */ 66 static addPrefetch(kind, url, as) { 67 const linkEl = document.createElement('link'); 68 linkEl.rel = kind; 69 linkEl.href = url; 70 if (as) { 71 linkEl.as = as; 72 } 73 document.head.append(linkEl); 74 } 75 76 /** 77 * Begin pre-connecting to warm up the iframe load 78 * Since the embed's network requests load within its iframe, 79 * preload/prefetch'ing them outside the iframe will only cause double-downloads. 80 * So, the best we can do is warm up a few connections to origins that are in the critical path. 81 * 82 * Maybe `<link rel=preload as=document>` would work, but it's unsupported: http://crbug.com/593267 83 * But TBH, I don't think it'll happen soon with Site Isolation and split caches adding serious complexity. 84 */ 85 static warmConnections() { 86 if (LiteYTEmbed.preconnected) return; 87 88 // The iframe document and most of its subresources come right off youtube.com 89 LiteYTEmbed.addPrefetch('preconnect', 'https://www.youtube-nocookie.com'); 90 // The botguard script is fetched off from google.com 91 LiteYTEmbed.addPrefetch('preconnect', 'https://www.google.com'); 92 93 // Not certain if these ad related domains are in the critical path. Could verify with domain-specific throttling. 94 LiteYTEmbed.addPrefetch('preconnect', 'https://googleads.g.doubleclick.net'); 95 LiteYTEmbed.addPrefetch('preconnect', 'https://static.doubleclick.net'); 96 97 LiteYTEmbed.preconnected = true; 98 } 99 100 addIframe() { 101 const params = new URLSearchParams(this.getAttribute('params') || []); 102 params.append('autoplay', '1'); 103 104 const iframeEl = document.createElement('iframe'); 105 iframeEl.width = 560; 106 iframeEl.height = 315; 107 // No encoding necessary as [title] is safe. https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html#:~:text=Safe%20HTML%20Attributes%20include 108 iframeEl.title = this.playLabel; 109 iframeEl.allow = 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture'; 110 iframeEl.allowFullscreen = true; 111 // AFAIK, the encoding here isn't necessary for XSS, but we'll do it only because this is a URL 112 // https://stackoverflow.com/q/64959723/89484 113 iframeEl.src = `https://www.youtube-nocookie.com/embed/${encodeURIComponent(this.videoId)}?${params.toString()}`; 114 this.append(iframeEl); 115 116 this.classList.add('lyt-activated'); 117 118 // Set focus for a11y 119 this.querySelector('iframe').focus(); 120 } 121} 122// Register custome element 123customElements.define('lite-youtube', LiteYTEmbed);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.