This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
search.js
65 lines (57 loc) · 2.06 KB
/
search.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
const videoCardContainer = document.querySelector('.video-container');
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const search = urlParams.get('search_query');
let api_key = "AIzaSyAe-TVdpD8kck_LmkKlSJzFnGJOb2EmLrE";
let video_http = "https://www.googleapis.com/youtube/v3/search?";
let channel_http = "https://www.googleapis.com/youtube/v3/channels?";
// alert("https://youtube.googleapis.com/youtube/v3/search?q=" + search + "&key=" + api_key + "&part=snippet&maxResults=20®ionCode=PH")
// alert(videoCardContainer)
fetch(video_http + new URLSearchParams({
q: search,
key: api_key,
part: 'snippet',
maxResults: 50,
regionCode: 'PH'
}))
.then(res => res.json())
.then(data => {
data.items.forEach(item => {
getChannelIcon(item);
})
})
.catch(err => console.log(err));
const getChannelIcon = (video_data) => {
fetch(channel_http + new URLSearchParams({
key: api_key,
part: 'snippet',
id: video_data.snippet.channelId
}))
.then(res => res.json())
.then(data => {
video_data.channelThumbnail = data.items[0].snippet.thumbnails.default.url;
makeVideoCard(video_data);
})
}
const makeVideoCard = (data) => {
videoCardContainer.innerHTML += `
<div class="video" onclick="location.href = 'watch.html?v=${data.id}'">
<img src="${data.snippet.thumbnails.high.url}" class="thumbnail" alt="">
<div class="content">
<div class="info">
<h4 class="title">${data.snippet.title}</h4>
<p class="channel-name" onclick="location.href = 'results.html?search_query='${data.snippet.channelTitle}'">${data.snippet.channelTitle}</p>
</div>
</div>
</div>
`;
}
// search bar
const searchInput = document.querySelector('.search-bar');
const searchBtn = document.querySelector('.search-btn');
let searchLink = "results.html?search_query=";
searchBtn.addEventListener('click', () => {
if(searchInput.value.length){
location.href = searchLink + searchInput.value;
}
})