-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (42 loc) · 1.21 KB
/
app.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
const input = document.querySelector('.yt-url');
const button = document.querySelector('.submit');
const imageBox = document.querySelector('.image-container');
const image = document.querySelector('a');
let url;
let youtubeImgData = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&key=AIzaSyAG7ECVg-NFDAnyfm6rONLWOhIYEsoBMxc&id=';
button.addEventListener('click', getUrl);
document.addEventListener('keydown', enterPress)
function enterPress(e) {
if(e.which == 13 && input.value != '') {
getUrl();
console.log('enter');
}
}
function getUrl() {
url = input.value.split('=')[1];
if (input.value != '') {
url = youtubeImgData + url;
fetch(url).then((response) => {
return response.json();
}).then((data) => {
console.log(data.items[0].snippet.thumbnails)
delay(data.items[0].snippet.thumbnails);
})
}
}
function delay(link) {
setTimeout(function() {
if ('maxres' in link) {
setImage(link.maxres.url);
} else if ('high' in link) {
setImage(link.high.url);
} else {
setImage(link.default.url);
}
},1000)
input.value = '';
}
function setImage(imgUrl) {
image.setAttribute('href',imgUrl);
image.innerHTML = '<img src="' + imgUrl + '">';
}