-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchlyrics.js
73 lines (62 loc) · 2.03 KB
/
fetchlyrics.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
document.querySelector("#searchbtn").addEventListener("click", getLyrics);
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
async function getLyrics(e) {
const Elementname = document.querySelector("#searchTxt");
const name = Elementname.value;
if (name === "") {
Elementname.classList.add("is-invalid");
return
} else {
Elementname.classList.remove("is-invalid");
}
document.querySelector(".txtBox").innerHTML=modal_content
//await fetch(`https://some-random-api.ml/lyrics?title=${name}`)
const url = `https://api.akuari.my.id/search/lirik?query=${encodeURIComponent(name)}`
await fetch(url)
.then((response) => response.json())
.then((data) => {
// document.querySelector(".txtBox").innerHTML = `
// <div>
// <img
// src="${data.thumbnail.genius}"
// />
// </div>
// <div class="txtInfos">
// <h1>${capitalizeFirstLetter(data.title)} by ${data.author}</h1>
// <p>Lyrics:</p>
// <p>${data.lyrics}\n</p>
// <p> Link :<a href="${data.links.genius}"> ${data.links.genius}</a></p>
// <p>Disclaimer: ${data.disclaimer}</p>
// </div>`;
//
const lyrs=data.result[0].lirik
const title=data.result[0].judul
const artist=data.result[0].artis
if(!lyrs){
document.querySelector(".txtBox").innerHTML = `
<div class="alert alert-danger">
<h4>Lyrics not found 😞</h4>
</div>
`;
return
}
//replase \n to <br>
const lyrics=lyrs.replace(/\n/g, "<br />")
document.querySelector(".txtBox").innerHTML = `
<div class="alert alert-success m-0">
${lyrics}
</div>
`
})
.catch((err) => {
// document.querySelector(".txtBox").innerHTML = `
// <div class="alert alert-danger">
// <h4>Lyrics not found 😞</h4>
// </div>
// `;
// console.log("Lyrics not found", err);
});
e.preventDefault();
}