PageSourceSearch

https://connectify.me/wp-content/themes/pro-child/js/lite-yt-embed.js?ver=7.1.1

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