11const form = document . getElementById ( "form" ) ;
22const search = document . getElementById ( "search" ) ;
33const result = document . getElementById ( "result" ) ;
4- const more = document . getElementById ( "more" ) ;
54
6- const apiURL = "https://api.lyrics.ovh" ;
5+ let lastSearchResults = [ ] ;
6+ const apiURL = "https://lrclib.net/api" ;
77
88async function searchSongs ( term ) {
9- const res = await fetch ( `${ apiURL } /suggest/ ${ term } ` ) ;
9+ const res = await fetch ( `${ apiURL } /search?q= ${ encodeURIComponent ( term ) } ` ) ;
1010 const data = await res . json ( ) ;
11- showData ( data ) ;
11+ lastSearchResults = data || [ ] ;
12+ showData ( lastSearchResults ) ;
1213}
1314
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 ;
2720 }
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+ ` ;
3526}
3627
3728function showData ( data ) {
3829 result . innerHTML = `
3930 <ul class="songs">
40- ${ data . data
31+ ${ data
4132 . 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>
4536 </li>`
4637 )
4738 . join ( "" ) }
4839 </ul>
4940 ` ;
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 = "" ;
6541}
6642
6743function showAlert ( message ) {
@@ -81,10 +57,12 @@ form.addEventListener("submit", (e) => {
8157} ) ;
8258result . addEventListener ( "click" , ( e ) => {
8359 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 ) ;
8866 }
8967} ) ;
9068
0 commit comments