Skip to content

Commit af4595c

Browse files
committed
Combined solutions
I combined two pull requests: - paulirish#124 - paulirish#122 - Added also support to WebP.
1 parent 32f4a55 commit af4595c

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ <h3>View isolated demos:</h3>
1919
<li><a href="./variants/pe.html">lite-youtube-embed - progressively enhanced</a>
2020
<li><a href="./variants/custom-poster-image.html">lite-youtube-embed - custom poster image</a>
2121
<li><a href="./variants/params.html">lite-youtube-embed - with parameters</a></li>
22+
<li><a href="./variants/multiple-embeds.html">lite-youtube-embed - multiple embeds on same page</a>
2223
<li><a href="./variants/yt.html">normal youtube embed</a>
2324
</ul>
2425
</body>

src/lite-yt-embed.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class LiteYTEmbed extends HTMLElement {
2828
* TODO: Consider using webp if supported, falling back to jpg
2929
*/
3030
if (!this.style.backgroundImage) {
31-
this.style.backgroundImage = `url("https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg")`;
31+
this.style =
32+
`background-image: url('https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg');
33+
background-image: -webkit-image-set(url("https://i.ytimg.com/vi/${this.videoId}/hqdefault.webp") 1x, url("https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg") 1x);
34+
background-image: image-set(url("https://i.ytimg.com/vi/${this.videoId}/hqdefault.webp") type("image/webp"), url("https://i.ytimg.com/vi/${this.videoId}/hqdefault.jpg") type("image/jpeg"));`;
3235
}
3336

3437
// Set up play button, and its visually hidden label
@@ -44,6 +47,7 @@ class LiteYTEmbed extends HTMLElement {
4447
playBtnLabelEl.textContent = this.playLabel;
4548
playBtnEl.append(playBtnLabelEl);
4649
}
50+
playBtnEl.removeAttribute('href');
4751

4852
// On hover (or tap), warm up the TCP connections we're (likely) about to use.
4953
this.addEventListener('pointerover', LiteYTEmbed.warmConnections, {once: true});
@@ -52,6 +56,7 @@ class LiteYTEmbed extends HTMLElement {
5256
// TODO: In the future we could be like amp-youtube and silently swap in the iframe during idle time
5357
// We'd want to only do this for in-viewport or near-viewport ones: https://github.com/ampproject/amphtml/pull/5003
5458
this.addEventListener('click', this.addIframe);
59+
this.needsYTApiForAutoplay = navigator.vendor.includes('Apple') || ['Firefox', 'Android'].some(userAgent => navigator.userAgent.includes(userAgent));
5560
}
5661

5762
// // TODO: Support the the user changing the [videoid] attribute
@@ -95,13 +100,54 @@ class LiteYTEmbed extends HTMLElement {
95100
LiteYTEmbed.preconnected = true;
96101
}
97102

98-
addIframe(e) {
103+
fetchYTPlayerApi() {
104+
if (window.YT || (window.YT && window.YT.Player)) return;
105+
106+
this.ytApiPromise = new Promise((res, rej) => {
107+
var el = document.createElement('script');
108+
el.src = 'https://www.youtube.com/iframe_api';
109+
el.async = true;
110+
el.onload = () => {
111+
YT.ready(res);
112+
};
113+
el.onerror = rej;
114+
this.append(el);
115+
});
116+
}
117+
118+
async addYTPlayerIframe(params) {
119+
this.fetchYTPlayerApi();
120+
await this.ytApiPromise;
121+
122+
const videoPlaceholderEl = document.createElement('div')
123+
this.append(videoPlaceholderEl);
124+
125+
const paramsObj = Object.fromEntries(params.entries());
126+
127+
new YT.Player(videoPlaceholderEl, {
128+
width: '100%',
129+
videoId: this.videoId,
130+
playerVars: paramsObj,
131+
events: {
132+
onReady: event => {
133+
event.target.playVideo();
134+
},
135+
},
136+
});
137+
}
138+
139+
async addIframe(e){
99140
if (this.classList.contains('lyt-activated')) return;
100141
e.preventDefault();
101142
this.classList.add('lyt-activated');
102143

103144
const params = new URLSearchParams(this.getAttribute('params') || []);
104145
params.append('autoplay', '1');
146+
params.append('playsinline', '1');
147+
148+
if (this.needsYTApiForAutoplay) {
149+
return this.addYTPlayerIframe(params);
150+
}
105151

106152
const iframeEl = document.createElement('iframe');
107153
iframeEl.width = 560;

variants/multiple-embeds.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>lite-youtube-embed</title>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<link rel="stylesheet" href="../src/lite-yt-embed.css" />
8+
</head>
9+
<body>
10+
<h1>multiple embeds</h1>
11+
12+
<lite-youtube videoid="ogfYd705cRs" playlabel="Play: Keynote (Google I/O '18)"></lite-youtube>
13+
14+
15+
<lite-youtube videoid="n57U2_-3NLQ" playlabel="Play: Chrome Dev Summit 2021 recap"></lite-youtube>
16+
17+
18+
<script src="../src/lite-yt-embed.js"></script>
19+
20+
<!-- <script>
21+
// Simulate duplicate iframe problem. https://github.com/paulirish/lite-youtube-embed/issues/92
22+
document.body.addEventListener('click', e => e.target.click());
23+
</script> -->
24+
</body>
25+
</html>

variants/solo.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,5 @@ <h1><code>lite-youtube</code> custom element</h1>
1313

1414
<script src="../src/lite-yt-embed.js"></script>
1515

16-
<!-- <script>
17-
// Simulate duplicate iframe problem. https://github.com/paulirish/lite-youtube-embed/issues/92
18-
document.body.addEventListener('click', e => e.target.click());
19-
</script> -->
2016
</body>
2117
</html>

variants/yt.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
</head>
88
<body>
99

10-
<h3>typical iframe embed</h3>
10+
<h1>typical iframe embed</h1>
1111
<iframe
12-
width="560"
13-
height="315"
12+
width="720"
13+
height="405"
1414
src="https://www.youtube.com/embed/ogfYd705cRs"
1515
frameborder="0"
1616
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"

0 commit comments

Comments
 (0)