const activityProgressMap = new Map(); function formatTime(ms) { const totalSecs = Math.floor(ms / 1000); const hours = Math.floor(totalSecs / 3600); const mins = Math.floor((totalSecs % 3600) / 60); const secs = totalSecs % 60; return `${String(hours).padStart(1, "0")}:${String(mins).padStart(2, "0")}:${String(secs).padStart(2, "0")}`; } function formatVerbose(ms) { const totalSecs = Math.floor(ms / 1000); const hours = Math.floor(totalSecs / 3600); const mins = Math.floor((totalSecs % 3600) / 60); const secs = totalSecs % 60; return `${hours}h ${mins}m ${secs}s`; } function updateElapsedAndProgress() { const now = Date.now(); for (const el of document.querySelectorAll(".activity-timestamp")) { const start = Number(el.dataset.start); if (!start) continue; const elapsed = now - start; const display = el.querySelector(".elapsed"); if (display) display.textContent = `(${formatVerbose(elapsed)} ago)`; } for (const bar of document.querySelectorAll(".progress-bar")) { const start = Number(bar.dataset.start); const end = Number(bar.dataset.end); if (!start || !end || end <= start) continue; const duration = end - start; const elapsed = Math.min(now - start, duration); const progress = Math.min( 100, Math.max(0, Math.floor((elapsed / duration) * 100)), ); const fill = bar.querySelector(".progress-fill"); if (fill) fill.style.width = `${progress}%`; } for (const label of document.querySelectorAll(".progress-time-labels")) { const start = Number(label.dataset.start); const end = Number(label.dataset.end); if (!start || !end || end <= start) continue; const isPaused = now > end; const current = isPaused ? end - start : Math.max(0, now - start); const total = end - start; const currentEl = label.querySelector(".progress-current"); const totalEl = label.querySelector(".progress-total"); const id = `${start}-${end}`; const last = activityProgressMap.get(id); if (isPaused || (last !== undefined && last === current)) { label.classList.add("paused"); } else { label.classList.remove("paused"); } activityProgressMap.set(id, current); if (currentEl) { currentEl.textContent = isPaused ? `Paused at ${formatTime(current)}` : formatTime(current); } if (totalEl) totalEl.textContent = formatTime(total); } } updateElapsedAndProgress(); setInterval(updateElapsedAndProgress, 1000); const head = document.querySelector("head"); const userId = head?.dataset.userId; let instanceUri = head?.dataset.instanceUri; let badgeURL = head?.dataset.badgeUrl; if (userId && instanceUri) { if (!instanceUri.startsWith("http")) { instanceUri = `https://${instanceUri}`; } const wsUri = instanceUri .replace(/^http:/, "ws:") .replace(/^https:/, "wss:") .replace(/\/$/, ""); const socket = new WebSocket(`${wsUri}/socket`); let heartbeatInterval = null; socket.addEventListener("open", () => {}); socket.addEventListener("message", (event) => { const payload = JSON.parse(event.data); if (payload.op === 1 && payload.d?.heartbeat_interval) { heartbeatInterval = setInterval(() => { socket.send(JSON.stringify({ op: 3 })); }, payload.d.heartbeat_interval); socket.send( JSON.stringify({ op: 2, d: { subscribe_to_id: userId, }, }), ); } if (payload.t === "INIT_STATE" || payload.t === "PRESENCE_UPDATE") { updatePresence(payload.d); requestAnimationFrame(() => updateElapsedAndProgress()); } }); socket.addEventListener("close", () => { if (heartbeatInterval) clearInterval(heartbeatInterval); }); } function resolveActivityImage(img, applicationId) { if (!img) return null; if (img.startsWith("mp:external/")) { return `https://media.discordapp.net/external/${img.slice("mp:external/".length)}`; } if (img.includes("/https/")) { const clean = img.split("/https/")[1]; return clean ? `https://${clean}` : null; } if (img.startsWith("spotify:")) { return `https://i.scdn.co/image/${img.split(":")[1]}`; } return `https://cdn.discordapp.com/app-assets/${applicationId}/${img}.png`; } function buildActivityHTML(activity) { const start = activity.timestamps?.start; const end = activity.timestamps?.end; const now = Date.now(); const elapsed = start ? now - start : 0; const total = start && end ? end - start : null; const progress = total && elapsed > 0 ? Math.min(100, Math.floor((elapsed / total) * 100)) : null; let art = null; let smallArt = null; if (activity.assets) { art = resolveActivityImage( activity.assets.large_image, activity.application_id, ); smallArt = resolveActivityImage( activity.assets.small_image, activity.application_id, ); } const activityTypeMap = { 0: "Playing", 1: "Streaming", 2: "Listening", 3: "Watching", 4: "Custom Status", 5: "Competing", }; const activityType = activity.name === "Spotify" ? "Listening to Spotify" : activity.name === "TIDAL" ? "Listening to TIDAL" : activityTypeMap[activity.type] || "Playing"; const activityTimestamp = start && progress === null ? `
` : ""; const activityButtons = activity.buttons && activity.buttons.length > 0 ? ` ` : ""; const progressBar = progress !== null ? `