-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYoutube.js
135 lines (103 loc) · 4.06 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
let div = document.getElementById("videosResults");
let input = document.getElementById("inputBox");
input.addEventListener("keyup", function (event) {
if (event.keyCode === 13) {
// Number 13 is the ("Enter" key button) on the keyboard
event.preventDefault();
document.getElementById("searchbtn").click(); // Trigger the button element with a click
search();
}
});
let playVideo = (id) => {
let videoDiv = document.getElementById("videosResults");
// videoDiv.innerHTML = null;
videoDiv.innerHTML = `<iframe
width="1300"
height="555"
src="https://www.youtube.com/embed/${id}"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowfullscreen
></iframe>`;
};
async function search() {
let inp = document.getElementById("inputBox").value;
let div = document.getElementById("videosResults");
let API = "AIzaSyA266SPutTfCO63sLArqnx8Xi-UR51PSus";
div.innerHTML = "";
let res = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?q=${inp}&part=snippet&maxResults=25&key=${API}`
);
let data = await res.json();
console.log(data);
for (let {
id: { videoId },
channelImage,
snippet: { title, channelTitle, thumbnails },
} of data.items) {
let channelThumbnail = thumbnails.default.url;
let mainDiv = document.createElement("div");
mainDiv.id = "hoverDiv";
mainDiv.onclick = () => playVideo(id);
let channelThumbnailImg = document.createElement("img");
channelThumbnailImg.src = channelThumbnail;
channelThumbnailImg.className = "channel-icon";
let video_frame = document.createElement("iframe");
let flex = document.createElement("div");
flex.id = "flex";
let t = document.createElement("p");
t.style = "color:black";
t.className = "title";
video_frame.src = `https://www.youtube.com/embed/${videoId}`;
video_frame.allow = `fullscreen`;
t.innerHTML = title;
let channelTittle = document.createElement("p");
channelTittle.id = "channel-name";
channelTittle.innerHTML = `${channelTitle} <span id=span>✅</span> `;
flex.append(channelThumbnailImg, channelTittle);
mainDiv.append(video_frame, flex, t);
div.append(mainDiv);
}
}
// <-------------------------------------------------------------------->
async function trending() {
let inp = document.getElementById("inputBox").value;
let div = document.getElementById("videosResults");
div.innerHTML = "";
let API = "AIzaSyA266SPutTfCO63sLArqnx8Xi-UR51PSus";
let res = await fetch(
`https://www.googleapis.com/youtube/v3/videos?part=snippet&maxResults=24&chart=mostPopular®ionCode=IN&key=${API}`
);
let data = await res.json();
console.log(data);
for (let {
id,
channelImage,
snippet: { title, channelTitle, thumbnails },
} of data.items) {
let channelThumbnail = thumbnails.medium.url;
let channelThumbnailImg = document.createElement("img");
channelThumbnailImg.src = channelThumbnail;
channelThumbnailImg.className = "channel-icon";
let video_frame = document.createElement("img");
video_frame.style.width = "100%";
let mainDivs = document.createElement("div");
mainDivs.id = "hoverDiv";
mainDivs.onclick = () => playVideo(id);
let flex = document.createElement("div");
flex.id = "flex";
let t = document.createElement("p");
t.style = "color:black";
t.className = "title";
video_frame.src = channelThumbnail;
t.innerHTML = title;
let channelTittle = document.createElement("p");
channelTittle.id = "channel-name";
channelTittle.innerHTML = `${channelTitle} <span id=span>✅</span> `;
flex.append(channelThumbnailImg, channelTittle);
mainDivs.append(video_frame, flex, t);
div.append(mainDivs);
}
}
trending();