-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
143 lines (110 loc) · 3.65 KB
/
youtube.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
const key = "AIzaSyDeU5zpcth2OgXDfToyc7-QnSJsDc41UGk";
let value = document.getElementById("search").value;
let dropdown = document.getElementById("dropdown");
let display_div=document.getElementById("showdata");
document.getElementById("mainlogo").addEventListener("click",()=>{
window.location.href="./index.html"
})
let popularfun = async()=> {
let popular_url = `https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&chart=mostPopular&maxResults=20®ionCode=in&key=${key}`
try{
let popularres= await fetch(popular_url);
let popularData= await popularres.json()
console.log('popularData:', popularData)
display(popularData.items)
} catch(error){
console.log('error:', error)
}
}
popularfun()
let id;
let debounce = (fun,delay)=> {
if(id){
clearTimeout(id)
}
id= setTimeout(()=>{
fun();
},delay)
}
let main = async () => {
let value = document.getElementById("search").value;
let data = await getData(value);
if(data.length == 0)
{
display_div.innerHTML=null
let errorimg=document.createElement("img");
errorimg.src="./images/cross.png"
errorimg.style.display="block";
errorimg.style.margin="auto";
display_div.append(errorimg)
}
else searchDisplay(data,value);
if(value != null)
{
document.getElementById("searchbutton").addEventListener("click",function () {
dropdown.style.display = "none";
display(data);
});
}
};
let getData = async (value) => {
let url = `https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=20&q=${value}&key=${key}`;
try {
let res = await fetch(url);
let firstdata = await res.json();
return firstdata.items;
} catch (err) {
console.log("err:", err);
}
};
let searchDisplay = (data,value) => {
dropdown.innerHTML = null;
dropdown.style.display = "block";
if (data == undefined || value=="" ) {
dropdown.style.display = "none";
return false;
}
data.forEach(({ id: { videoId }, snippet }) => {
let listitem = document.createElement("ul");
listitem.setAttribute("id", "droplist");
let name = document.createElement("li");
name.setAttribute("id", "nameli");
name.addEventListener("click", function () {
dropdown.style.display = "none";
display(data);
});
name.innerText = snippet.title;
listitem.append(name);
dropdown.append(listitem);
});
};
let display = (data) => {
display_div.innerHTML=null;
data.forEach( ({id,snippet:{title,channelTitle,thumbnails:{medium:{url}}}}) => {
let localdata = {
id,
title,
channelTitle
}
let box= document.createElement("div");
box.setAttribute("class","box_div");
box.addEventListener("click", function(){
localStorage.setItem("videoData",JSON.stringify(localdata))
window.location.href="./playpage.html"
})
let vidImage=document.createElement("img");
vidImage.setAttribute("class","vidImg");
vidImage.src=url
// let iframe = document.createElement("iframe");
// iframe.setAttribute("class","iframe");
// iframe.src=`https://www.youtube.com/embed/${videoId}`
let title_div = document.createElement("h4");
title_div.setAttribute("class","title")
title_div.innerText = title;
let channel = document.createElement("p");
channel.setAttribute("class","channel_title")
channel.innerText = channelTitle;
box.append(vidImage,title_div,channel);
display_div.append(box)
})
}