-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
266 lines (263 loc) · 12.8 KB
/
index.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lyrics Searching Website</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<link rel="shortcut icon" href="images/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">
<link rel="stylesheet" href="css/style.css">
<meta name="theme-color" content="#000000">
<meta name="description"
content="Lyric Finder is a web application that allows you to search for lyrics of any song you want.">
<meta name="keywords"
content="lyrics, lyric, lyric finder, apis, open api hub, microproject, lyric search, songs, song lyrics">
<meta name="author" content="Piyush Chugeja">
</head>
<body style="overflow-x: hidden;" onload="checkCookies()">
<button class="backToTop" id="goToTop" onclick="window.scrollTo(0, 0)" title="Go back to top">
<i class="fas fa-chevron-up"></i>
</button>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark text-center p-2">
<a class="navbar-brand m-auto">
<img src="images/logo.svg" class="d-inline-block align-top" alt="Lyric finder logo">
</a>
</nav>
<div class="container position-relative" id="searchBar" title="Main Search Bar">
<div class="row mt-5 mx-auto">
<div class="col-12">
<input type="text" name="song" class="slide-up w-100" id="song" placeholder="Taylor Swift" required
autocomplete="off" oninput="getSuggested()" />
<label for="song"><span>What are you looking for?</span></label>
</div>
</div>
</div>
<div class="container" title="Lyrics Container">
<div class="row d-flex justify-content-center mt-5">
<h3 class="text-center" id="result"></h3>
<div class="row d-flex justify-content-center mt-3" id="suggestions"></div>
</div>
<div id="lyrics" class="d-none">
<div class="row text-center">
<div class="col-md-6 d-flex justify-content-center">
<div class="col-md-4 position-md-fixed mt-md--5">
<h3 id="song-name" class="p-1"></h3>
<h4 id="artist-name" class="text-muted"></h4>
<img id="album-cover" class="img-thumbnail w-50" src="">
<p class="lead font-weight-bold" id="album-name"></p>
<button class="mainButton outline" onclick="getSuggested()" id="goBackButton">
<i class="fas fa-search"></i>
Go back to search
</button>
<button class="mainButton outline" onclick="printMe()" id="printButton">
<i class="fas fa-print"></i>
Print Lyrics
</button>
</div>
</div>
<div class="col-md-6 text-center my-5 my-md-2">
<h5>
Lyrics
</h5>
<hr>
<p id="lyrics-text"></p>
</div>
</div>
</div>
</div>
<script>
function getSuggested() {
var song = document.getElementById("song").value;
var suggestionsArea = document.getElementById("suggestions");
if (suggestionsArea.innerHTML == "") {
suggestionsArea.innerHTML = "<div class='spinner-border text-dark' role='status'><span class='sr-only'>Loading...</span></div>";
}
if (suggestionsArea.classList.contains("d-none")) {
suggestionsArea.classList.remove("d-none");
suggestionsArea.classList.add("d-flex");
document.getElementById("searchBar").classList.remove("d-none");
document.getElementById("result").classList.remove("d-none");
document.getElementById("lyrics").classList.add("d-none");
}
if (!(song.length > 0)) {
suggestionsArea.innerHTML = "";
document.getElementById("result").innerHTML = "Please enter something";
return;
}
var url = "https://api.lyrics.ovh/suggest/" + song;
var request = new XMLHttpRequest();
request.open("GET", url);
request.send();
request.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
suggestionsArea.innerHTML = "";
document.getElementById("result").innerHTML = "Following are the suggestions for <b>" + song + "</b>";
var data = JSON.parse(this.responseText);
if (data.data.length == 0) {
document.getElementById("result").innerHTML = "No suggestions found for <b>" + song + "</b>";
return;
}
for (var i = 0; i < data.data.length; i++) {
makeSuggestionCard(data.data[i].title, data.data[i].album.title, data.data[i].artist.name, data.data[i].album.cover_big, data.data[i].explicit_lyrics);
}
}
else if (this.readyState == 4 && this.status == 404) {
suggestionsArea.innerHTML = "";
document.getElementById("result").innerHTML = "No suggestions found for <b>" + song + "</b>";
}
}
window.scrollTo(0, 0);
setCookies(song);
}
function getLyrics(artist, song, album, cover) {
var suggestionsArea = document.getElementById("suggestions");
var lyricsArea = document.getElementById("lyrics");
var url = "https://api.lyrics.ovh/v1/" + artist + "/" + song;
var request = new XMLHttpRequest();
request.open("GET", url);
request.send();
request.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
try {
var data = JSON.parse(this.responseText);
suggestionsArea.classList.remove("d-flex");
suggestionsArea.classList.add("d-none");
lyricsArea.classList.remove("d-none");
document.getElementById("song-name").innerHTML = song;
document.getElementById("artist-name").innerHTML = artist;
document.getElementById("album-cover").src = cover;
document.getElementById("searchBar").classList.add("d-none");
document.getElementById("result").classList.add("d-none");
var songLyrics = data.lyrics;
var lyrics = songLyrics.split("\n");
var lyricsText = "";
for (var i = 0; i < lyrics.length; i++) {
if (lyrics[i].includes("Paroles"))
continue;
lyricsText += lyrics[i] + "<br>";
}
console.log(lyricsText);
document.getElementById("lyrics-text").innerHTML = lyricsText;
document.getElementById("album-cover").innerHTML = "";
document.getElementById("album-name").innerHTML = album;
} catch (e) {
Swal.fire({
title: 'Oops...',
text: 'No lyrics found!',
icon: 'error'
});
console.log(e);
}
}
else if (this.readyState == 4 && this.status == 404) {
Swal.fire({
title: 'Oops..',
text: 'No lyrics found! We are sorry for the inconvenience.',
icon: 'error',
confirmButtonText: 'OK'
});
}
}
window.scrollTo(0, 0);
}
function makeSuggestionCard(song, album, artist, albumCover, isExplicit) {
var suggestionsArea = document.getElementById("suggestions");
var mainDiv = document.createElement("div");
mainDiv.className = "card m-3 m-md-1"
mainDiv.style = "max-width: 540px;";
var rowDiv = document.createElement("div");
rowDiv.className = "row no-gutters align-items-center";
var colDiv = document.createElement("div");
colDiv.className = "col-md-4";
var img = document.createElement("img");
img.className = "card-img";
img.src = albumCover;
img.alt = album;
colDiv.appendChild(img);
rowDiv.appendChild(colDiv);
var colDiv2 = document.createElement("div");
colDiv2.className = "col-md-8";
var cardDiv = document.createElement("div");
cardDiv.className = "card-body";
var h5 = document.createElement("h5");
h5.className = "card-title";
h5.innerHTML = song;
var div = document.createElement("div");
div.className = "row";
var p = document.createElement("p");
p.className = "card-text";
p.innerHTML = "<strong>Artist:</strong> " + artist;
var col6 = document.createElement("div");
col6.className = "col-md-6";
col6.appendChild(p);
div.appendChild(col6);
var p2 = document.createElement("p");
p2.className = "card-text";
p2.innerHTML = "<strong>Album:</strong> " + album;
var col6 = document.createElement("div");
col6.className = "col-md-6";
col6.appendChild(p2);
div.appendChild(col6);
var e = document.createElement("div");
e.className = "alert alert-dark py-1 my-1 text-center";
e.innerHTML = "<strong>Explicit:</strong> " + isExplicit.toString().toUpperCase();
var button = document.createElement("button");
button.className = "mainButton outline my-1 btn-block";
button.innerHTML = "<i class='fas fa-music'></i> Get Lyrics";
button.onclick = function () {
getLyrics(artist, song, album, albumCover);
}
col6 = document.createElement("div");
col6.className = "col-md-6";
col6.appendChild(button);
div.appendChild(col6);
col6 = document.createElement("div");
col6.className = "col-md-6";
col6.appendChild(e);
div.appendChild(col6);
cardDiv.appendChild(h5);
cardDiv.appendChild(p);
cardDiv.appendChild(p2);
cardDiv.appendChild(div);
colDiv2.appendChild(cardDiv);
rowDiv.appendChild(colDiv2);
mainDiv.appendChild(rowDiv);
suggestionsArea.appendChild(mainDiv);
}
window.addEventListener('scroll', function () {
if (window.scrollY > 80) {
document.getElementById("goToTop").style.display = "block";
}
else {
document.getElementById("goToTop").style.display = "none";
}
});
function checkCookies() {
var cookie = document.cookie;
if (cookie.length > 0) {
Swal.fire({
title: 'Welcome back!',
text: 'Here are the results as per your last serarch.',
icon: 'success',
confirmButtonText: 'OK'
});
cookies = cookie.split(";");
document.getElementById("song").value = cookies[0].split("=")[1];
getSuggested();
}
}
function setCookies(song) {
var date = new Date();
date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
var expires = "expires=" + date.toUTCString();
document.cookie = "song=" + song + ";" + expires + ";path=/";
}
function printMe() {
window.print();
}
</script>
</body>
</html>