-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (65 loc) · 2.55 KB
/
main.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
window.addEventListener("load", function () {
let songInput = document.getElementById("searchInp");
let artistInput = document.getElementById("searchInp1");
// Retrieve values from localStorage
let storedSong = localStorage.getItem("song");
let storedArtist = localStorage.getItem("artist");
// Check if values exist in localStorage
if (storedSong && storedArtist) {
// Set the values of input fields
songInput.value = storedSong;
artistInput.value = storedArtist;
}
});
function sub() {
let song = document.getElementById("searchInp").value;
let artist = document.getElementById("searchInp1").value;
console.log("Song:", song);
console.log("Artist:", artist);
// Encode input values
let encodedSong = encodeURI(song);
let encodedArtist = encodeURI(artist);
// Construct the URL with the input values
let url = `https://api.lyrics.ovh/v1/${encodedArtist}/${encodedSong}`;
console.log("URL:", url); // Log the constructed URL
// Fetch lyrics from the constructed URL
fetch(url)
.then(res => res.json())
.then((data) => {
console.log("API Response:", data); // Log the API response
let display = document.getElementById("result");
if (data.lyrics) {
// Store lyrics in localStorage
localStorage.setItem("lyrics", data.lyrics.replace(/\n/g, "<br>"));
display.innerHTML = localStorage.getItem("lyrics");
// Store input values in localStorage
localStorage.setItem("song", song);
localStorage.setItem("artist", artist);
} else {
display.innerHTML = "Lyrics not found.";
}
})
.catch(error => {
console.error("Error fetching lyrics:", error);
let display = document.getElementById("result");
display.innerHTML = "Error fetching lyrics. Please try again later.";
});
};
window.addEventListener("load", function () {
let display = document.getElementById("result");
display.innerHTML = localStorage.getItem("lyrics");
});
window.addEventListener("contextmenu", function (event) {
event.preventDefault();
let pop = document.getElementById("popup");
pop.style.display = "block"
let x = event.clientX;
let y = event.clientY;
pop.style.left = x + "px";
pop.style.top = y + "px";
window.addEventListener("click", function (event) {
if (event.target !== pop) {
pop.style.display = "none"
}
})
});