1
1
const form = document . getElementById ( "form" ) ;
2
2
const search = document . getElementById ( "search" ) ;
3
3
const result = document . getElementById ( "result" ) ;
4
- const more = document . getElementById ( "more" ) ;
5
4
6
- const apiURL = "https://api.lyrics.ovh" ;
5
+ let lastSearchResults = [ ] ;
6
+ const apiURL = "https://lrclib.net/api" ;
7
7
8
8
async function searchSongs ( term ) {
9
- const res = await fetch ( `${ apiURL } /suggest/ ${ term } ` ) ;
9
+ const res = await fetch ( `${ apiURL } /search?q= ${ encodeURIComponent ( term ) } ` ) ;
10
10
const data = await res . json ( ) ;
11
- showData ( data ) ;
11
+ lastSearchResults = data || [ ] ;
12
+ showData ( lastSearchResults ) ;
12
13
}
13
14
14
- async function getLyrics ( artist , songTitle ) {
15
- const res = await fetch ( `${ apiURL } /v1/${ artist } /${ songTitle } ` ) ;
16
- const data = await res . json ( ) ;
17
- console . log ( artist , songTitle ) ;
18
- if ( data . error ) {
19
- showAlert ( data . error ) ;
20
- } else {
21
- const lyrics = data . lyrics . replace ( / ( \r \n | \r | \n ) / g, "<br>" ) ;
22
-
23
- result . innerHTML = `
24
- <h2><strong>${ artist } </strong> - ${ songTitle } </h2>
25
- <span>${ lyrics } </span>
26
- ` ;
15
+ function showLyricsByIndex ( index ) {
16
+ const song = lastSearchResults [ index ] ;
17
+ if ( ! song ) {
18
+ showAlert ( "Lyrics not found." ) ;
19
+ return ;
27
20
}
28
- more . innerHTML = "" ;
29
- }
30
-
31
- async function getMoreSongs ( url ) {
32
- const res = await fetch ( `https://cors-anywhere.herokuapp.com/${ url } ` ) ; // proxy is required to avoid CORS issue
33
- const data = await res . json ( ) ;
34
- showData ( data ) ;
21
+ const lyrics = song . plainLyrics || "No lyrics available." ;
22
+ result . innerHTML = `
23
+ <h2><strong>${ song . artistName } </strong> - ${ song . trackName } </h2>
24
+ <span>${ lyrics . replace ( / ( \r \n | \r | \n ) / g, "<br>" ) } </span>
25
+ ` ;
35
26
}
36
27
37
28
function showData ( data ) {
38
29
result . innerHTML = `
39
30
<ul class="songs">
40
- ${ data . data
31
+ ${ data
41
32
. map (
42
- ( song ) => `<li>
43
- <span><strong>${ song . artist . name } </strong> - ${ song . title } </span>
44
- <button class="btn" data-artist ="${ song . artist . name } " data-songtitle=" ${ song . title } ">Get Lyrics</button>
33
+ ( song , index ) => `<li>
34
+ <span><strong>${ song . artistName } </strong> - ${ song . trackName } </span>
35
+ <button class="btn" data-index ="${ index } ">Get Lyrics</button>
45
36
</li>`
46
37
)
47
38
. join ( "" ) }
48
39
</ul>
49
40
` ;
50
- // Pagination
51
- if ( data . prev || data . next ) {
52
- more . innerHTML = `
53
- ${
54
- data . prev
55
- ? `<button class="btn" onclick="getMoreSongs('${ data . prev } ')">Prev</button>`
56
- : ""
57
- }
58
- ${
59
- data . next
60
- ? `<button class="btn" onclick="getMoreSongs('${ data . next } ')">Next</button>`
61
- : ""
62
- }
63
- ` ;
64
- } else more . innerHTML = "" ;
65
41
}
66
42
67
43
function showAlert ( message ) {
@@ -81,10 +57,12 @@ form.addEventListener("submit", (e) => {
81
57
} ) ;
82
58
result . addEventListener ( "click" , ( e ) => {
83
59
const clickedElement = e . target ;
84
- if ( clickedElement . tagName === "BUTTON" ) {
85
- const artist = clickedElement . getAttribute ( "data-artist" ) ;
86
- const songTitle = clickedElement . getAttribute ( "data-songtitle" ) ;
87
- getLyrics ( artist , songTitle ) ;
60
+ if (
61
+ clickedElement . tagName === "BUTTON" &&
62
+ clickedElement . hasAttribute ( "data-index" )
63
+ ) {
64
+ const index = clickedElement . getAttribute ( "data-index" ) ;
65
+ showLyricsByIndex ( index ) ;
88
66
}
89
67
} ) ;
90
68
0 commit comments