-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (106 loc) · 3.95 KB
/
index.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
//-----------------side navbar---------
const openMenu=document.querySelector("#show-menu");
const hideMenuIcon=document.querySelector("#hide-menu");
const sideMenu=document.querySelector("#nav-menu");
openMenu.addEventListener("click",function(){
sideMenu.classList.add("active");
})
hideMenuIcon.addEventListener("click",function(){
sideMenu.classList.remove("active")
})
//---------------searchbar----------------//
//const API_KEY=`AIzaSyD5Gs0LKqoMgmUUbx8HvHKDPA8nmaXZm9U`;
//const API_KEY=`AIzaSyBw6bqJRBUmUMbSLb2nlE92MPFzXlL6p1s`
//const API_KEY=`AIzaSyBHZwxaavMd6CN0Quk3r8QmW91y_Sh3yJ4`;
const API_KEY=`AIzaSyCSQCfUz7ru_hqczYfyG_41iwAzfuKvLUo`
const searchVideos=async()=>{
try{
const query=document.getElementById("query").value;
const res = await fetch(`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=25&q=${query}&key=${API_KEY}`);
const data=await res.json();
console.log(data)
const actual_data=data.items;
//console.log(actual_data)
appendVideos(actual_data)
}catch(err){
console.log(err)
}
}
const appendVideos=(data)=>{
const container_id=document.getElementById("container");
container_id.innerHTML=null;
data.forEach(({snippet,id})=>{
const title=snippet.title;
const videoId=id.videoId;
const thumbnail=snippet.thumbnails.high.url;
const channel_name=snippet.channelTitle;
const div=document.createElement("div");
const img=document.createElement("img");
img.src=thumbnail
const title_html=document.createElement("h4");
title_html.innerText=title;
const channel_html=document.createElement("h5");
channel_html.innerText=channel_name;
let data={
videoId,
snippet,
}
div.onclick=()=>{
storeClickvideo(data)
}
div.append(img,title_html,channel_html)
container_id.append(div)
})
}
// const showVideo=(x)=>{
// window.location.href="video.html";
// x=JSON.stringify(x)
// localStorage.setItem(x)
// }
function storeClickvideo(data){
localStorage.setItem("clicked_item",JSON.stringify(data))
window.location.href="video.html";
}
//-------------------------------------------------landingpage video----------------------------
//const api_key=`AIzaSyBw6bqJRBUmUMbSLb2nlE92MPFzXlL6p1s`;
const api_key=`AIzaSyCSQCfUz7ru_hqczYfyG_41iwAzfuKvLUo`
const video=async()=>{
try{
const res=await fetch(` https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&chart=mostPopular®ionCode=US&key=${api_key}`);
// const res=await fetch(`https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=Ks-_Mh1QhMc&key==${api_key}`)
const data=await res.json();
let items = data.items;
console.log(data )
append(items)
//console.log(items);
}catch(err){
console.log(err)
}
}
video()
const append=(data)=>{
const container_id=document.getElementById("container");
container_id.innerHTML=null;
data.forEach(({snippet,id})=>{
const title=snippet.title;
const videoId=id.videoId;
const thumbnail=snippet.thumbnails.high.url;
const channel_name=snippet.channelTitle;
const div=document.createElement("div");
const img=document.createElement("img");
img.src=thumbnail
const title_html=document.createElement("h4");
title_html.innerText=title;
const channel_html=document.createElement("h5");
channel_html.innerText=channel_name;
let data={
videoId,
snippet,
}
div.onclick=()=>{
storeClickvideo(data)
}
div.append(img,title_html,channel_html)
container_id.append(div)
})
}