This repository was archived by the owner on Jun 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (52 loc) · 2.73 KB
/
Copy pathscript.js
File metadata and controls
60 lines (52 loc) · 2.73 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
// CHANGE THE URL BELOW TO HAVE YOUR DISCORD ID
const url = "https://api.lanyard.rest/v1/users/750454372650975232"
document.addEventListener("DOMContentLoaded", function(){
const coverImage = document.getElementById('cover');
const songName = document.getElementById('songName');
const songInfo = document.getElementById('songInfo');
const songDuration = document.getElementById("songDuration");
async function fetchLanyard() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching data:', error);
coverImage.style.display = "none"
songName.textContent = `There was an error fetching data from Lanyard`
songInfo.style.display = "none"
}
}
async function updateLanyardData() {
const lanyard = await fetchLanyard();
const discordUsername = lanyard.data.discord_user.display_name
const currentTime = new Date().getTime();
if (lanyard.data.spotify) {
const spotify = lanyard.data.spotify;
const songLength = spotify.timestamps.end - spotify.timestamps.start;
const timeElapsed = currentTime - spotify.timestamps.start;
// Credit to https://github.com/dustinrouillard
function msToMinSeconds(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = Number(((millis % 60000) / 1000).toFixed(0));
return seconds == 60
? minutes + 1 + ":00"
: minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
}
coverImage.style.display = "block"
coverImage.src = spotify.album_art_url;
songInfo.style.display = "block"
songDuration.style.display = "block"
songName.innerHTML = `<a target="_blank" href="https://open.spotify.com/track/${spotify.track_id}" style="text-decoration: none; color: white;">${spotify.song}</a>`;
songInfo.innerHTML = `By <span style="color: #9A6CA7;">${spotify.artist}</span> on <span style="color: #9A6CA7;">${spotify.album}</span>`;
songDuration.innerHTML = `<span style="color: #1ED760; background-color: #101010; padding-left: 1.5px; padding-right: 1.5px;">${msToMinSeconds(timeElapsed)}</span> - <span style="color: #1ED760; background-color: #101010; padding-left: 1.5px; padding-right: 1.5px;">${msToMinSeconds(songLength)}</span>`
} else {
coverImage.style.display = "none"
songName.textContent = `${discordUsername}'s not currently listening to Spotify`
songInfo.style.display = "none"
songDuration.style.display = "none"
}
}
updateLanyardData();
setInterval(updateLanyardData, 5000);
});