-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
44 lines (44 loc) · 1.01 KB
/
index.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
<html>
<head>
<style>
.video-player {
width: 640px;
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
<h1> YouTube Video Player</h1>
<div class="video-player">
<div>
<input type="text" id="url" placeholder="Enter YouTube video URL">
<button onclick="playingVideo()">Play</button>
</div>
<br>
<div id="player"></div>
</div>
<script src="https://www.youtube.com/iframe_api"></script>
<script>
let player;
function playingVideo() {
const videoUrl = document.getElementById('url').value;
const videoId = extractYouTubeVideoId(videoUrl);
if (videoId) {
player = new YT.Player('player', {
height: '360',
width: '640',
videoId: videoId,
events: {}
});
}
}
function extractYouTubeVideoId(url) {
const videoIdRegex = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|vi|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/|y2u\.be\/)([a-zA-Z0-9_-]{11})/;
const match = url.match(videoIdRegex);
return match ? match[1] : null;
}
</script>
<script async src="https://www.youtube.com/iframe_api"></script>
</body>
</html>