-
Notifications
You must be signed in to change notification settings - Fork 17
/
youtube-video.html
45 lines (43 loc) · 1.49 KB
/
youtube-video.html
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
<html>
<head>
<title>Youtube video events</title>
<script type="module" src="dist/youtube-video.js"></script>
</head>
<body>
<p>
Feel free to interact with the video below. Playing, pausing and
ending it will update the text below:
</p>
<p><strong>State:</strong> <span class="video-state"></span></p>
<youtube-video
width="640"
height="360"
id="my-video"
src="https://www.youtube.com/watch?v=Wn9twYUXw6w"
></youtube-video>
<script>
document.addEventListener('DOMContentLoaded', function () {
const videoEl = document.querySelector('youtube-video');
const [stateText] =
document.body.getElementsByClassName('video-state');
// add event listeners
videoEl.addEventListener(
'playing',
() => (stateText.textContent = 'playing')
);
videoEl.addEventListener(
'ended',
() => (stateText.textContent = 'finished')
);
videoEl.addEventListener(
'pause',
() => (stateText.textContent = 'paused')
);
// load video
videoEl.load().then(function () {
videoEl.play();
});
});
</script>
</body>
</html>