-
Notifications
You must be signed in to change notification settings - Fork 14
/
script.js
186 lines (163 loc) · 6.75 KB
/
script.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
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
const API_KEY = 'AIzaSyCQqvQhNs-lX7KoUCErol_05C5pM15YCRQ';
const SEARCH_ENDPOINT = 'https://www.googleapis.com/youtube/v3/search';
let isDescriptionVisible = true;
let isCommentVisible = false;
function searchVideos(category = '') {
const searchInput = document.getElementById('searchInput').value;
document.getElementById('videoPlayer').src = '';
document.getElementById('searchResults').innerHTML = '';
document.getElementById('videoTitle').innerText = ''; // Clear video title
document.getElementById('descriptionContainer').style.display = 'none'; // Hide description container
document.getElementById('commentContainer').style.display = 'none'; // Hide comment container
hideButtons(); // Hide buttons
let query = searchInput;
if (category && category !== 'all') {
query = category; // Override the search query if a category is selected
}
// Make API request to search for videos
fetch(`${SEARCH_ENDPOINT}?part=snippet&maxResults=10&q=${query}&type=video&key=${API_KEY}`)
.then(response => response.json())
.then(data => {
if (data.items.length > 0) {
// Display search results
const resultsContainer = document.getElementById('searchResults');
data.items.forEach(item => {
const videoId = item.id.videoId;
const title = item.snippet.title;
// Create result item
const resultItem = document.createElement('div');
resultItem.classList.add('result-item');
resultItem.innerHTML = `
<img src="${item.snippet.thumbnails.medium.url}" alt="Thumbnail">
<p>${title}</p>
`;
resultItem.addEventListener('click', () => playVideo(videoId));
resultsContainer.appendChild(resultItem);
});
} else {
alert('No videos found.');
}
})
.catch(error => {
console.error('Error fetching data:', error);
alert('An error occurred while fetching data.');
});
}
function hideButtons() {
const descriptionButton = document.getElementById('toggleDescriptionButton');
const commentButton = document.getElementById('toggleCommentButton');
descriptionButton.style.display = 'none';
commentButton.style.display = 'none';
}
hideButtons();
function playVideo(videoId) {
const videoUrl = `https://www.youtube.com/embed/${videoId}`;
document.getElementById('videoPlayer').src = videoUrl;
updateVideoTitle(videoId); // Update the video title
updateDescription(videoId); // Update the video description
showButtons(); // Show buttons when video is selected
}
function updateVideoTitle(selectedVideoId) {
fetch(`https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${selectedVideoId}&key=${API_KEY}`)
.then(response => response.json())
.then(data => {
if (data.items.length > 0) {
const title = data.items[0].snippet.title;
document.getElementById('videoTitle').innerText = title; // Set the video title
} else {
document.getElementById('videoTitle').innerText = ''; // Clear video title if no data found
}
})
.catch(error => {
console.error('Error fetching video details:', error);
alert('An error occurred while fetching video details.');
});
}
function showButtons() {
const descriptionButton = document.getElementById('toggleDescriptionButton');
const commentButton = document.getElementById('toggleCommentButton');
descriptionButton.style.display = 'block';
commentButton.style.display = 'block';
}
function updateDescription(selectedVideoId) {
fetch(`https://www.googleapis.com/youtube/v3/videos?part=snippet&id=${selectedVideoId}&key=${API_KEY}`)
.then(response => response.json())
.then(data => {
if (data.items.length > 0) {
const description = data.items[0].snippet.description;
const descriptionContainer = document.getElementById('descriptionContainer');
if (description) {
descriptionContainer.innerHTML = `<h3>Description Box:</h3><p>${description}</p>`;
descriptionContainer.style.display = 'block'; // Display description container
} else {
descriptionContainer.innerHTML = '<p>No description available.</p>';
descriptionContainer.style.display = 'none'; // Hide description container if no description
}
}
})
.catch(error => {
console.error('Error fetching video details:', error);
alert('An error occurred while fetching video details.');
});
}
function toggleDescription() {
const descriptionElement = document.getElementById('descriptionContainer');
isDescriptionVisible = !isDescriptionVisible;
descriptionElement.style.display = isDescriptionVisible ? 'block' : 'none';
}
function toggleComments() {
const commentContainer = document.getElementById('commentContainer');
isCommentVisible = !isCommentVisible;
commentContainer.style.display = isCommentVisible ? 'block' : 'none';
}
function toggleDarkMode() {
const body = document.body;
const darkModeIcon = document.getElementById('darkModeIcon');
body.classList.toggle("dark-mode");
darkModeIcon.innerText = body.classList.contains("dark-mode") ? 'light_mode' : 'dark_mode';
}
function getVideoIdFromPlayer() {
const videoPlayer = document.getElementById('videoPlayer');
const videoUrl = videoPlayer.src;
const videoId = videoUrl.split('/').pop();
return videoId;
}
function displayComments(videoId) {
fetch(`https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=${videoId}&key=${API_KEY}`)
.then(response => response.json())
.then(data => {
const firstComment = data.items[0];
const commentContainer = document.getElementById('commentContainer');
if (firstComment) {
const commentText = firstComment.snippet.topLevelComment.snippet.textDisplay;
commentContainer.innerHTML = `<h3>Pinned Comment:</h3><p>${commentText}</p>`;
commentContainer.style.display = 'block';
} else {
commentContainer.innerHTML = '<p>No comments found.</p>';
commentContainer.style.display = 'none';
}
})
.catch(error => {
console.error('Error fetching comments:', error);
alert('An error occurred while fetching comments.');
});
}
function showComments() {
const commentContainer = document.getElementById('commentContainer');
if (commentContainer.style.display === 'block') {
commentContainer.style.display = 'none';
} else {
const videoId = getVideoIdFromPlayer();
displayComments(videoId);
commentContainer.style.display = 'block';
}
}
//adding search functionality by pressing enter key
document.addEventListener('DOMContentLoaded', () => {
const searchInput = document.getElementById('searchInput');
searchInput.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
searchVideos();
}
});
});