PageSourceSearch

https://shoptalkshow.com/wp-content/themes/shoptalk6/javascripts/lite-yt-embed.js?v1.1

js shoptalkshow.com collected 2026-10-02 02:17:24 UTC 5,259 bytes, 133 lines download raw bytes

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