-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (51 loc) · 1.95 KB
/
app.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
let searchQuery;
function searchVideos() {
const apiKey = 'AIzaSyAMc0CRupQ7RUtrTUtdHRGDm4B2gXsyQrk';
searchQuery = document.getElementById('search-input').value.trim();
if (searchQuery === '') {
alert('Please enter a search query.');
return;
}
const apiUrl = `https://www.googleapis.com/youtube/v3/search?key=${apiKey}&part=snippet&q=${searchQuery}`;
fetch(apiUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Handle the response data here
displayVideos(data.items);
})
.catch(error => {
// Handle errors here
console.error('There was a problem with the fetch operation:', error);
});
}
// Define the displayVideos function and other necessary functions here
function displayVideos(videos) {
const container = document.getElementById('video-container');
// Clear previous search results
container.innerHTML = '';
videos.forEach(video => {
const videoTitle = video.snippet?.title;
const videoId = video.id.videoId;
const thumbnailUrl = video.snippet?.thumbnails?.default?.url;
// Create elements to display video information
const videoElement = document.createElement('div');
videoElement.classList.add('video');
const thumbnailElement = document.createElement('img');
thumbnailElement.src = thumbnailUrl;
const titleElement = document.createElement('h2');
titleElement.textContent = videoTitle;
// Append elements to the container
videoElement.appendChild(thumbnailElement);
videoElement.appendChild(titleElement);
container.appendChild(videoElement);
// Add click event listener to open the video in a new tab when clicked
videoElement.addEventListener('click', () => {
window.open(`https://www.youtube.com/watch?v=${videoId}`, '_blank');
});
});
}