-
Notifications
You must be signed in to change notification settings - Fork 0
/
lyrics.js
113 lines (72 loc) · 2.78 KB
/
lyrics.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const form = document.getElementById('form');
const search = document.getElementById('search');
const result = document.getElementById('display-result');
const API_URL = "https://api.lyrics.ovh/";
// Adding event listener in form
form.addEventListener('submit', e =>{
e.preventDefault();
const searchValue = search.value.trim();
// checking search value is empty or not
if(!searchValue && searchValue == undefined){
alert("There is noting in search");
}else{
searchSong(searchValue);
}
})
async function searchSong(searchValue){
const searchResult = await fetch(`${API_URL}/suggest/${searchValue}`);
const data = await searchResult.json();
// console.log(data);
showData(data) //This connection help to show the data
}
function showData(data){
// SLice method for 10 data show//
result.innerHTML = `
${data.data.map(song => `
<div class="single-result row align-items-center my-3 p-3">
<div class="col-md-9">
<h3 class="lyrics-name">
${song.artist.name}
</h3>
<p class="author lead">Album by <span>${song.title}</p>
</div>
<div class="col-md-3 text-md-right text-center">
<button data-artist="${song.artist.name}" data-songTitle="${song.title}" class="btn btn-success">Get Lyrics</button>
</div>
</div>
`).slice(0, 10).join('') //This method are joining the all off html tags in html
}
`
}
//Event Listener in get lyrics button
result.addEventListener('click', e =>{
const clickedElement = e.target;
//checking clicked element is button or not
if(clickedElement.tagName === 'BUTTON'){
const artist = clickedElement.getAttribute("data-artist"); //This will interact the data-artist
const songTitle = clickedElement.getAttribute("data-songTitle"); //This will interact the data-songTitle
getLyrics (artist, songTitle)
}
})
//Get Lyrics
async function getLyrics(artist, songTitle){
try {
let res = await fetch(`${API_URL}/v1/${artist}/${songTitle}`); // Here is the most tricky part
let data = await res.json()
const lyrics = data.lyrics.replace(/(\r\n|\r|\n)/g, '<br/>'); // This will replace all the lyrics at the same time
if(data.lyrics == undefined){
alert('No lyrics fount !!!')
}
result.innerHTML = `
<div class="single-lyrics text-center">
<h2 class="text-success mb-4">${artist} - ${songTitle}</h2>
<pre class="lyric text-white">${lyrics}</pre>
</div>
`;
} catch(err) {
// catches errors both in fetch and response.json
alert('This lyrics is not found!!!.');
location.reload()
}
console.log(data);
}