-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (176 loc) · 5.54 KB
/
script.js
File metadata and controls
202 lines (176 loc) · 5.54 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const form = document.getElementById("form");
const search = document.getElementById("search");
const result = document.getElementById("result");
const more = document.getElementById("more");
const apiURL = "https://api.lyrics.ovh";
let lastResultsData = null; // full array of tracks
let currentPage = 0;
const pageSize = 10;
function showLoading() {
result.innerHTML = `<p class="loading">Loading…</p>`;
more.innerHTML = "";
}
// Fetch helper without abort timeout
async function fetchJson(url, init = {}) {
const res = await fetch(url, init);
if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`);
return await res.json();
}
// Search by song or artist
async function searchSongs(term) {
try {
showLoading();
const data = await fetchJson(
`${apiURL}/suggest/${encodeURIComponent(term)}`
);
console.log(data);
// store only the array of tracks for client-side paging
lastResultsData = Array.isArray(data.data) ? data.data : [];
currentPage = 0;
renderResultsPage();
} catch (error) {
console.error("searchSongs error:", error);
result.innerHTML = `<p>Something went wrong loading results.</p>`;
more.innerHTML = "";
}
}
// Format seconds as mm:ss
function formatDuration(seconds) {
const mins = Math.floor(seconds / 60);
const secs = seconds % 60;
const padded = secs < 10 ? `0${secs}` : `${secs}`;
return `${mins}:${padded}`;
}
// Render current page of results
function renderResultsPage() {
if (!Array.isArray(lastResultsData)) {
result.innerHTML = `<p>No results</p>`;
more.innerHTML = "";
return;
}
const total = lastResultsData.length;
const start = currentPage * pageSize;
const end = Math.min(start + pageSize, total);
const pageItems = lastResultsData.slice(start, end);
result.innerHTML = `
<ul class="songs">
${pageItems
.map(
(song) => ` <li>
<div class="song">
<img src="${
song.album?.cover_medium || song.album?.cover || ""
}" alt="${song.album?.title || "Album cover"}" />
<div class="meta">
<span><strong>${song.artist?.name || ""}</strong> - ${
song.title || ""
}</span>
<span class="duration">${formatDuration(song.duration || 0)}</span>
${
song.preview ? `<audio controls src="${song.preview}"></audio>` : ""
}
</div>
<button class="btn" data-artist="${
song.artist?.name || ""
}" data-songtitle="${song.title || ""}">Get Lyrics</button>
</div>
</li>`
)
.join("")}
</ul>
`;
// pager buttons
const totalPages = Math.ceil(total / pageSize);
const hasPrev = currentPage > 0;
const hasNext = end < total;
const showFirstLast = totalPages >= 3;
const hasMultiplePages = totalPages > 1;
if (hasMultiplePages) {
more.innerHTML = `
${
showFirstLast && hasPrev
? `<button class="btn pager" data-action="first"><< First</button>`
: ""
}
${
hasPrev
? `<button class="btn pager" data-action="prev">< Previous</button>`
: ""
}
${
hasNext
? `<button class="btn pager" data-action="next">Next ></button>`
: ""
}
${
showFirstLast && hasNext
? `<button class="btn pager" data-action="last">Last >></button>`
: ""
}
`;
} else {
more.innerHTML = "";
}
}
// Show song and artist in DOM (kept for compatibility, delegates to render)
function showData(data) {
lastResultsData = Array.isArray(data.data) ? data.data : [];
currentPage = 0;
renderResultsPage();
}
// Get lyrics for song
async function getLyrics(artist, songTitle) {
try {
const data = await fetchJson(
`${apiURL}/v1/${encodeURIComponent(artist)}/${encodeURIComponent(
songTitle
)}`
);
const lyrics = data.lyrics.replace(/(\r\n|\r|\n)/g, "<br>");
result.innerHTML = `<h2><strong>${artist}</strong> - ${songTitle}</h2>
<span>${lyrics}</span>`;
more.innerHTML = `<button class="btn" id="backBtn">Back to Results</button>`;
} catch (error) {
console.error("getLyrics error:", error);
result.innerHTML = `<p>Couldn't load lyrics. Please try again.</p>`;
more.innerHTML = `<button class="btn" id="backBtn">Back to Results</button>`;
}
}
// Event Listeners
form.addEventListener("submit", (e) => {
e.preventDefault();
const searchTerm = search.value.trim();
if (!searchTerm) {
alert("Please type in a search term");
} else {
searchSongs(searchTerm);
}
});
// Get lyrics button click
result.addEventListener("click", (e) => {
const clicked = e.target.closest("button");
if (!clicked) return;
const artist = clicked.getAttribute("data-artist");
const songTitle = clicked.getAttribute("data-songtitle");
if (artist && songTitle) {
getLyrics(artist, songTitle);
}
});
// Back to search results and pager click handling
more.addEventListener("click", (e) => {
const clicked = e.target.closest("button");
if (!clicked) return;
if (clicked.id === "backBtn") {
renderResultsPage();
return;
}
const action = clicked.getAttribute("data-action");
if (!action) return;
const total = Array.isArray(lastResultsData) ? lastResultsData.length : 0;
const lastPage = Math.max(0, Math.ceil(total / pageSize) - 1);
if (action === "first") currentPage = 0;
if (action === "prev") currentPage = Math.max(0, currentPage - 1);
if (action === "next") currentPage = Math.min(lastPage, currentPage + 1);
if (action === "last") currentPage = lastPage;
renderResultsPage();
});