-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththose-dogs.html
More file actions
192 lines (178 loc) · 4.78 KB
/
those-dogs.html
File metadata and controls
192 lines (178 loc) · 4.78 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>YouTube Flexbox Embedder</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
body {
margin: 0;
font-family: sans-serif;
background: #181818;
color: #eee;
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.controls {
display: flex;
gap: 1rem;
padding: 1rem;
background: #232323;
align-items: center;
}
.embeds {
display: flex;
flex-wrap: wrap;
gap: 1rem;
padding: 1rem;
flex: 1;
justify-content: flex-start;
align-items: flex-start;
}
.embed-item {
background: #222;
padding: 0.5rem;
border-radius: 8px;
box-shadow: 0 2px 8px #0008;
position: relative;
}
.remove-btn {
position: absolute;
top: 4px;
right: 4px;
background: #c00;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 0.8rem;
padding: 2px 6px;
}
button {
background: #444;
color: #fff;
border: none;
border-radius: 4px;
padding: 0.5rem 1rem;
cursor: pointer;
font-size: 1rem;
transition: background 0.2s;
}
button:hover {
background: #666;
}
</style>
</head>
<body>
<div class="container">
<div class="controls">
<button id="addOneBtn">ADD 1</button>
<button id="addAllBtn">ADD ALL</button>
<button id="playAllBtn">PLAY ALL</button>
<button id="pauseAllBtn">PAUSE ALL</button>
<button id="stopAllBtn">STOP ALL</button>
<button id="removeAllBtn">REMOVE ALL</button>
</div>
<div class="embeds" id="embeds"></div>
</div>
<script>
const VIDEO_IDS = [
"StjW_iuFln4",
"M8HGpFcJfq8",
"J3rYZR8Lt5U",
"gdYRYbjLehg"
];
const EMBED_WIDTH = 480;
const EMBED_HEIGHT = 360;
const players = [];
const loadYouTubeAPI = (function () {
let ytApiReady = false;
return function () {
return new Promise((resolve) => {
if (ytApiReady) return resolve();
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
window.onYouTubeIframeAPIReady = () => {
ytApiReady = true;
resolve();
};
document.body.appendChild(tag);
});
};
})();
const getNextId = (function () {
let nextId = 1;
return function () {
return "ytplayer_" + (nextId++);
};
})();
const embedsDiv = document.getElementById("embeds");
const addOneBtn = document.getElementById("addOneBtn");
const addAllBtn = document.getElementById("addAllBtn");
const playAllBtn = document.getElementById("playAllBtn");
const pauseAllBtn = document.getElementById("pauseAllBtn");
const stopAllBtn = document.getElementById("stopAllBtn");
const removeAllBtn = document.getElementById("removeAllBtn");
async function addVideo(videoId) {
await loadYouTubeAPI();
const playerId = getNextId();
const wrapper = document.createElement("div");
wrapper.className = "embed-item";
wrapper.innerHTML = `
<button class="remove-btn" title="Remove">✖</button>
<div id="${playerId}"></div>
`;
embedsDiv.appendChild(wrapper);
wrapper.querySelector(".remove-btn").onclick = () => {
const idx = players.findIndex((p) => p.id === playerId);
if (idx !== -1) {
players[idx].player.destroy();
players.splice(idx, 1);
}
wrapper.remove();
};
const player = new YT.Player(playerId, {
width: EMBED_WIDTH,
height: EMBED_HEIGHT,
videoId: videoId,
playerVars: {
rel: 0,
modestbranding: 1,
loop: 1,
playlist: videoId
}
});
players.push({ id: playerId, player });
}
addOneBtn.onclick = async () => {
const videoId = VIDEO_IDS[Math.floor(Math.random() * VIDEO_IDS.length)];
await addVideo(videoId);
};
addAllBtn.onclick = async () => {
for (const videoId of VIDEO_IDS) {
await addVideo(videoId);
}
};
playAllBtn.onclick = () => {
players.forEach(({ player }) => player.playVideo());
};
pauseAllBtn.onclick = () => {
players.forEach(({ player }) => player.pauseVideo());
};
stopAllBtn.onclick = () => {
players.forEach(({ player }) => {
player.pauseVideo();
player.seekTo(0, true);
});
};
removeAllBtn.onclick = () => {
players.forEach(({ player }) => player.destroy());
players.length = 0;
embedsDiv.innerHTML = "";
};
</script>
</body>
</html>