-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
49 lines (39 loc) · 1.58 KB
/
main.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
var download_btn = document.getElementById('Download_btn');
var container = document.querySelector('.container');
var videoUrlInput = document.getElementById('url')
download_btn.addEventListener("click", function() {
var videoUrl = videoUrlInput.value;
var resolution = document.getElementById("resolutionSelect").value;
// Remove whitespaces and check if the URL is valid
videoUrl = videoUrl.trim();
if (videoUrl === "") {
alert("Please enter a valid YouTube video URL.");
return;
}
// Get video ID from URL
var videoId = extractVideoId(videoUrl);
if (videoId === null) {
alert("Please enter a valid YouTube video URL.");
return;
}
// Construct thumbnail URL based on resolution
var thumbnailUrl = "https://img.youtube.com/vi/" + videoId + "/" + resolution + ".jpg";
// Display thumbnail
var thumbnailImg = document.createElement("img");
thumbnailImg.src = thumbnailUrl;
var thumbnailContainer = document.getElementById("output");
setTimeout(() => {
thumbnailContainer.innerHTML = "";
thumbnailContainer.appendChild(thumbnailImg);
}, 1000);
thumbnailContainer.innerHTML = "<div class='loader'></div>";
});
function extractVideoId(url) {
var videoId = null;
var pattern = /(?:https?:\/\/(?:www\.)?)?youtu(?:be\.com\/(?:watch\?v=|embed\/|v\/)|\.be\/)([\w\-]+)(?:[\?&]t=([\dhms]+))?/i;
var match = url.match(pattern);
if (match && match[1].length === 11) {
videoId = match[1];
}
return videoId;
}