-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightest-yt-embed.js
89 lines (89 loc) · 3.9 KB
/
lightest-yt-embed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class YTEmbed extends HTMLElement {
constructor() {
super();
const v = this.getAttribute('v');
const [id, params] = v.split('?');
this.id = id;
this.params = params;
const videoIds = this.id.split(',');
this.videoIds = videoIds;
let linkUrl , embedUrl;
switch (true) {
case this.videoIds.length > 1:
if (this.classList.contains('no-link-embed')) {
linkUrl = `https://www.youtube.com/watch_videos?video_ids=${this.videoIds.join(',')}&autoplay=1`;
embedUrl = `https://www.youtube-nocookie.com/embed/?playlist=${this.videoIds.join(',')}&autoplay=1`;
} else if (this.classList.contains('no-embed')) {
linkUrl = `https://www.youtube.com/watch_videos?video_ids=${this.videoIds.join(',')}&autoplay=1`;
} else {
linkUrl = `https://www.youtube-nocookie.com/embed/?playlist=${this.videoIds.join(',')}&autoplay=1`;
embedUrl = linkUrl;
}
break;
case this.id.startsWith('PL') || this.id.startsWith('TL'):
if (this.classList.contains('no-link-embed')) {
linkUrl = `https://www.youtube.com/playlist?list=${this.id}&autoplay=1`;
embedUrl = `https://www.youtube-nocookie.com/embed/videoseries?list=${this.id}&autoplay=1`;
} else if (this.classList.contains('no-embed')) {
linkUrl = `https://www.youtube.com/playlist?list=${this.id}&autoplay=1`;
} else {
linkUrl = `https://www.youtube-nocookie.com/embed/videoseries?list=${this.id}&autoplay=1`;
embedUrl = linkUrl;
}
break;
default:
if (this.classList.contains('no-link-embed')) {
linkUrl = `https://www.youtube.com/watch?v=${this.id}${this.params ? '&' + this.params + '&': '&'}autoplay=1`;
embedUrl = `https://www.youtube-nocookie.com/embed/${this.id}${this.params ? '?' + this.params + '&' : '?'}autoplay=1`;
} else if (this.classList.contains('no-embed')) {
linkUrl = `https://www.youtube.com/watch?v=${this.id}${this.params ? '&' + this.params + '&' : '&'}autoplay=1`;
} else {
linkUrl = `https://www.youtube-nocookie.com/embed/${this.id}${this.params ? '?' + this.params + '&' : '?'}autoplay=1`;
embedUrl = linkUrl;
}
break;
}
this.linkUrl = linkUrl;
this.embedUrl = embedUrl;
this.link = document.createElement('a');
this.link.textContent = this.getAttribute('t') || 'View Video';
this.link.title = 'View video in new tab';
this.link.href = this.linkUrl;
this.button = document.createElement('button');
this.button.textContent = '▶️ Play';
this.button.title = 'Play Video';
this.button.className = 'showHideButton';
if (this.classList.contains('no-embed')) {
this.button.onclick = () => window.open(this.linkUrl);
this.appendChild(this.button);
this.appendChild(this.link);
} else {
this.button.onclick = () => this.toggleVideo();
this.wrapper = document.createElement('div');
this.wrapper.className = 'yt-wrapper';
this.appendChild(this.button);
this.appendChild(this.link);
this.appendChild(this.wrapper);
}
}
toggleVideo() {
const iframeExists = this.wrapper.querySelector('iframe');
if (!iframeExists) {
const iframe = document.createElement('iframe');
iframe.src = this.embedUrl;
iframe.allow = "accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share";
iframe.allowFullscreen = true;
iframe.className = 'yt';
this.wrapper.appendChild(iframe);
this.wrapper.style.display = 'block';
this.button.textContent = '⏹️ Stop';
this.button.title = 'Stop Video';
} else {
this.wrapper.removeChild(iframeExists);
this.wrapper.style.display = 'none';
this.button.textContent = '▶️ Play';
this.button.title = 'Play Video';
}
}
}
customElements.define('y-t', YTEmbed);