-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
93 lines (82 loc) · 2.9 KB
/
main.js
File metadata and controls
93 lines (82 loc) · 2.9 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
document.getElementById('search-form').addEventListener('submit', async function (e) {
e.preventDefault();
const query = document.getElementById('search-input').value;
const url = `https://youtube-v3-alternative.p.rapidapi.com/search?query=${query}&geo=US&lang=en`;
const options = {
method: 'GET',
headers: {
'x-rapidapi-key': '9452172446msh620b23d249c353fp16dad9jsn7b983901f89b',
'x-rapidapi-host': 'youtube-v3-alternative.p.rapidapi.com'
}
};
try {
const response = await fetch(url, options);
const result = await response.json();
displayVideos(result.data);
} catch (error) {
console.error('Error fetching search results:', error);
}
});
function displayVideos(videos) {
const videoList = document.getElementById('video-list');
videoList.innerHTML = '';
videos.forEach(video => {
const videoItem = document.createElement('div');
videoItem.className = 'video-item';
videoItem.innerHTML = `
<div class="video-thumbnail" style="background-image: url('${video.thumbnail[0].url}');"></div>
<div class="video-info">
<div class="video-title">${video.title}</div>
<div class="video-channel">${video.channelTitle}</div>
</div>
`;
videoItem.addEventListener('click', () => openModal(video.videoId));
videoList.appendChild(videoItem);
});
}
async function openModal(videoId) {
const modal = document.getElementById('video-modal');
const videoPlayer = document.getElementById('video-player');
const videoUrl = `https://www.youtube.com/embed/${videoId}`;
console.log('Opening video:', videoId, videoUrl); // Debugging information
videoPlayer.src = videoUrl;
modal.style.display = 'block';
videoPlayer.onerror = function () {
alert('This video is not available on YouTube.');
closeModal();
};
document.getElementById('download-mp3').onclick = async function () {
const url = `https://youtube-mp36.p.rapidapi.com/dl?id=${videoId}`;
const options = {
method: 'GET',
headers: {
'x-rapidapi-key': '9452172446msh620b23d249c353fp16dad9jsn7b983901f89b',
'x-rapidapi-host': 'youtube-mp36.p.rapidapi.com'
}
};
try {
const response = await fetch(url, options);
const result = await response.json();
if (result.status === 'ok') {
window.location.href = result.link;
} else {
alert('Error downloading MP3: ' + result.msg);
}
} catch (error) {
console.error('Error downloading MP3:', error);
}
};
}
document.getElementById('close-modal').addEventListener('click', closeModal);
window.onclick = function (event) {
const modal = document.getElementById('video-modal');
if (event.target == modal) {
closeModal();
}
};
function closeModal() {
const modal = document.getElementById('video-modal');
const videoPlayer = document.getElementById('video-player');
videoPlayer.src = '';
modal.style.display = 'none';
}