-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (75 loc) · 2.02 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
let APIKey = 'AIzaSyDS0-Mo1eGlDIwfLyw_dekt8hUD40q7Rcc';
let container = document.getElementById('search_results');
let myLocation = () => {
navigator.geolocation.getCurrentPosition(currentLocation);
};
window.onload = myLocation();
function currentLocation(position) {
let lat = position.coords.latitude;
let lon = position.coords.longitude;
myArea(lat, lon);
}
let myArea = (lat, lon) => {
fetch(
`https://youtube.googleapis.com/youtube/v3/videos?part=snippet®ionCode=IN&key=${APIKey}&maxResults=20&chart=mostPopular`
)
.then(function (res) {
return res.json();
})
.then(function (res) {
console.log(res.items);
appendVideos(res.items);
})
.catch(function (err) {
console.log('err:', err);
});
};
let getData = async () => {
try {
let query = document.getElementById('query').value;
let res = await fetch(
`https://youtube.googleapis.com/youtube/v3/search?regionCode=IN&q=${query}&key=${APIKey}&part=snippet&chart=mostPopular&maxResults=20`
);
// let data = await res.json();
let { items } = await res.json(); //items -> data.items
let array_of_videos = items;
appendVideos(array_of_videos);
console.log(array_of_videos);
} catch (err) {
console.log('err:', err);
}
};
let appendVideos = (data) => {
container.innerHTML = null;
data.forEach(
({
snippet: {
title,
channelId,
thumbnails: {
medium: { url, height, width },
},
},
id: { videoId },
}) => {
//let title = el.snippet.title;
let div = document.createElement('div');
let img = document.createElement('img');
img.src = url;
img.width = width;
img.height = height;
img.allow = 'fullscreen';
let name = document.createElement('h5');
name.innerText = title;
div.append(img, name);
div.addEventListener('click', () => {
localStorage.clear;
let video = [title, videoId, channelId, APIKey];
localStorage.setItem('videoId', JSON.stringify(video));
window.location.href = 'videoPlayer.html';
});
container.append(div);
//console.log(title, videoId);
}
);
};