-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (55 loc) · 2.2 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const titleSong = document.getElementById("stitle");
const personSong = document.getElementById("sname");
const btn = document.getElementById("submit1");
btn.addEventListener("click", () => {
const songName = document.getElementById("stitle").value;
const songArtist = document.getElementById("sname").value;
const apiKey = document.getElementById("apiKey").value;
if (songArtist.trim() !== '' || songName.trim() !== '') {
fetch(`https://youtube.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q=${songName}%20${songArtist}&type=video&key=${apiKey}`, {
method: 'GET'
})
.then((res) => res.json())
.then(data => {
console.log(data)
var vidId = data.items[0].id.videoId;
getVid(vidId, apiKey);
})
.catch((error) => {
alert('Invalid API key');
});
}
else {
alert('Fill in all the fields.')
}
function getVid(vidId, apiKey) {
fetch(`https://youtube.googleapis.com/youtube/v3/videos?part=contentDetails&id=${vidId}&key=${apiKey}`, {
method: 'GET'
})
.then((res) => res.json())
.then(data => {
var licensed_content = data.items[0].contentDetails.licensedContent;
var str = licensed_content ? 'This song is copyright' : 'This song is NOT copyrighted';
console.log(str);
document.getElementById('output').innerHTML = str;
document.getElementById("youtubePlace").src=`https://www.youtube.com/embed/${vidId}`;
})
.catch((error) => {
alert('Invalid API key');
});
}
})
titleSong.addEventListener('keydown', (e) => {
var regex = new RegExp('[a-zA-Z ]');
if (e.ctrlKey || e.altKey || typeof e.key !== 'string' || e.key.length !== 1) return;
if (!regex.test(e.key)) {
e.preventDefault();
}
});
personSong.addEventListener('keydown', (e) => {
var regex = new RegExp('[a-zA-Z ]');
if (e.ctrlKey || e.altKey || typeof e.key !== 'string' || e.key.length !== 1) return;
if (!regex.test(e.key)) {
e.preventDefault();
}
});