-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (64 loc) · 2.16 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
const searchData = async () => {
try {
const API_KEY = "AIzaSyABSOuXoSUUqHhmrpUa5mkIiERroSdhn8o";
let search_term = document.getElementById("search").value;
let responce = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=20&q=${search_term}&key=${API_KEY}`
);
let trashData = await responce.json();
let data = trashData.items;
append(data);
} catch (err) {
console.log(err);
}
};
const parent = document.getElementById("card");
const append = (data) => {
parent.innerHTML = null;
data.forEach(({ snippet, id: { videoId } }) => {
let div = document.createElement("div");
let title = document.createElement("p");
title.textContent = snippet.title;
let channelName = document.createElement("p");
channelName.textContent = snippet.channelTitle;
let channelThumbnail = document.createElement("img");
channelThumbnail.src = snippet.thumbnails.high.url;
div.append(channelThumbnail, title, channelName);
channelThumbnail.addEventListener("click", function () {
let data = { snippet, videoId };
localStorage.setItem("data", JSON.stringify(data));
window.location.href = "video.html";
});
// channelThumbnail.onclick=()=>{
// }
parent.append(div);
});
};
const sort = async () => {
try {
const API_KEY = "AIzaSyABSOuXoSUUqHhmrpUa5mkIiERroSdhn8o";
let search_term = document.getElementById("search").value;
let sort = "order=relevance";
let value = document.getElementById("sort").value;
if (value == "view") {
sort = "order=viewCount";
} else if (value == "alphabet") {
sort = "order=title";
} else if (value == "rating") {
sort = "order=rating";
} else {
sort = "order=relevance";
}
let responce = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?part=snippet&${sort}&maxResults=20&q=${search_term}&key=${API_KEY}`
);
let trashData = await responce.json();
let data = trashData.items;
append(data);
} catch (err) {
console.log(err);
}
};
//to save quote use this
// let data = JSON.parse(localStorage.getItem("ref"));
// append(data);