diff --git a/Javascript/DiscordProfile.js b/Javascript/DiscordProfile.js new file mode 100644 index 0000000..9290a87 --- /dev/null +++ b/Javascript/DiscordProfile.js @@ -0,0 +1,193 @@ +async function encodeBase64(url) { + const response = await fetch(url); + const blob = await response.blob(); + const reader = new FileReader(); + return new Promise((resolve, reject) => { + reader.onloadend = () => resolve(reader.result.split(',')[1]); + reader.onerror = reject; + reader.readAsDataURL(blob); + }); +} + +let ws = null; +let userData = null; +let previousStatus = null; +let previousActivity = null; + +const userId = "929208515883569182"; + +function connectWebSocket() { + ws = new WebSocket("wss://api.lanyard.rest/socket"); + + ws.onopen = () => { + console.log("WebSocket Connected"); + ws.send(JSON.stringify({ + op: 2, + d: { subscribe_to_id: userId } + })); + }; + + ws.onmessage = async ({ data }) => { + const { op, d } = JSON.parse(data); + switch (op) { + case 0: { + userData = d; + + // Update Status + if (d.discord_status !== previousStatus) { + previousStatus = d.discord_status; + const statusIndicator = document.querySelector('.status-indicator'); + statusIndicator.className = `status-indicator status-${d.discord_status}`; + } + + // Update Activities or Spotify + if (d.listening_to_spotify) { + const activityList = document.getElementById('activities-list'); + activityList.style.display = 'block'; + activityList.innerHTML = ''; + + const listItem = document.createElement('li'); + listItem.classList.add('activity-item'); + + const albumArt = d.spotify.album_art_url ? + await encodeBase64(d.spotify.album_art_url) : + await encodeBase64('https://lanyard-profile-readme.vercel.app/assets/unknown.png'); + + listItem.innerHTML = ` +
${d.spotify.song}
+by ${d.spotify.artist}
+${activity.state || ''}
+${activity.details || ''}
+