-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
103 lines (84 loc) · 2.81 KB
/
script.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
const input = document.getElementById("search-input");
const autocomplete = document.getElementById("autocomplete");
const r_title = document.getElementById("res-title");
const r_artist = document.getElementById("res-artist");
const r_lyrics = document.getElementById("res-lyrics");
const loading = document.querySelector(".lds-ring");
let disableAutocomplete = false;
function getInput(element) {
element = element || input;
let value = element.value;
value = value.trim();
return value;
}
async function getLyrics(query) {
query = query.trim();
let r = await fetch(`https://api.yodabot.xyz/api/lyrics/search?q=${query}`);
return await r.json();
}
async function performAutocomplete(query) {
query = query.trim();
let r = await fetch(`https://api.yodabot.xyz/api/lyrics/suggest?q=${query}&amount=5`);
return await r.json();
}
async function displayLyrics(query) {
query = query.trim();
if (!query) {
alert("Please enter a query!");
return
}
input.readOnly = true;
disableAutocomplete = true;
r_title.innerText = "";
r_artist.innerText = "";
r_lyrics.innerText = "";
autocomplete.innerHTML = "";
loading.style.display = "inline-block";
let lyrics = await getLyrics(query);
loading.style.display = "none";
input.readOnly = false;
disableAutocomplete = false;
if (!lyrics || !lyrics.title || !lyrics.artist || !lyrics.lyrics) {
alert("No lyrics found. Sorry!");
return;
}
input.value = "";
autocomplete.innerHTML = "";
r_title.innerText = lyrics.title;
r_artist.innerText = lyrics.artist;
r_lyrics.innerText = lyrics.lyrics;
}
async function displayAutocomplete(e) {
let value = getInput(e.target);
if (value === "") {
autocomplete.innerHTML = "";
return;
}
if (disableAutocomplete) return;
let suggestions = await performAutocomplete(value);
autocomplete.innerHTML = "";
for (let suggestion of suggestions) {
let li = document.createElement("li");
li.addEventListener('click', async function (e) {
window.disableAutocomplete = true;
input.value = e.target.innerText;
await displayLyrics(e.target.innerText);
});
li.innerText = `${suggestion.title} - ${suggestion.artists.join(", ")}`;
autocomplete.appendChild(li);
}
}
input.addEventListener('input', displayAutocomplete);
input.addEventListener('propertychange', displayAutocomplete);
input.addEventListener('change', displayAutocomplete);
input.addEventListener('keydown', async function (e) {
if (e.code === 'Enter') {
window.disableAutocomplete = true;
await displayLyrics(getInput(e.target));
}
});
setInterval(() => {
if (input.value === "") {
autocomplete.innerHTML = "";
}
}, 100);