Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
111 lines (60 loc) · 2.09 KB
/
Copy pathmain.js
File metadata and controls
111 lines (60 loc) · 2.09 KB
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
const searchContainer = document.getElementById('channelSearch');
const searchIcon = searchContainer.querySelector('.searchIcon');
const closeIcon = searchContainer.querySelector('.closeIcon');
const input = document.getElementById('input');
const h2 = document.getElementById('h2');
const button = document.getElementById('button');
searchIcon.addEventListener('click', () => {
searchContainer.classList.add('open');
});
closeIcon.addEventListener('click', () => {
searchContainer.classList.remove('open');
});
button.addEventListener('click', () => {
alert('Soon...')
});
const apiKey = "AIzaSyDBidvfGtorU2Z4tO2cRD3k1vangNMLxTQ";
// main.js
export async function getChanelData(handle) {
const url = `https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(handle)}&type=channel&key=${apiKey}`;
const response = await fetch(url);
const data = await response.json();
if (data.items && data.items.length > 0) {
return data.items[0];
} else {
return null;
}
}
export async function getChanelHandle(handle) {
const channelData = await getChanelData(handle);
return { channelData };
}
// export async function getChanelHandle(handle) {
// // handle = handle.replace(/^@/, '').trim();
// const url = `https://www.googleapis.com/youtube/v3/search?part=snippet&q=${encodeURIComponent(handle)}&type=channel&key=${apiKey}`;
// const res = await fetch(url);
// const data = await res.json();
// async function getChanelData() {
// const respone = await fetch(url);
// const data = await respone.json();
// if(data.items && data.items.length > 0) {
// const channelData = data.items[0];
// return channelData;
// } else {
// return null
// }
// }
// getChanelData();
// return { getChanelData };
// }
input.addEventListener('keydown', async (event) => {
if (event.key === 'Enter') {
const handle = input.value;
const channelID = await getChanelHandle(handle);
if(channelID) {
console.log(channelID);
} else {
console.log('Channel not found');
}
}
});