-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscordsync.js
More file actions
150 lines (126 loc) · 4.56 KB
/
discordsync.js
File metadata and controls
150 lines (126 loc) · 4.56 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
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
const lanyardUrl = 'https://api.lanyard.rest/v1/users/368694479391293442';
let previousData = null;
let intervalId;
function fetchDiscordData() {
fetch(lanyardUrl)
.then(response => response.json())
.then(res => {
if (isDataChanged(previousData, res.data)) {
updateUI(res.data);
previousData = res.data;
}
})
.catch(error => {
console.error('Error:', error);
});
}
function isDataChanged(previousData, currentData) {
return JSON.stringify(previousData) !== JSON.stringify(currentData);
}
function updateUI(data) {
const statusColors = {
online: '#43b581',
dnd: '#f04747',
idle: '#faa61a',
offline: '#4a4a4a',
};
const status = data.discord_status;
const spotify = data.spotify;
const profileImage = document.getElementById('realpfp');
const song = document.getElementById('spotifysong');
const statusMarker = document.getElementById('statusDesc');
const artistalbum = document.getElementById('spotifyartistalbum');
const albumart = document.getElementById('albumart');
const accentIndicator = document.getElementById('accent-indicator');
const statusDot = document.querySelector('.status-dot');
const root = document.documentElement;
// Update profile border with status color
if (profileImage) {
profileImage.style.borderColor = statusColors[status] || statusColors.offline;
}
// Update status dot color
if (statusDot) {
statusDot.style.background = statusColors[status] || statusColors.offline;
}
// Update status text
if (statusMarker) {
const statusText = status ? status.charAt(0).toUpperCase() + status.slice(1) : 'Offline';
statusMarker.textContent = statusText;
}
if (data.listening_to_spotify && spotify) {
song.textContent = spotify.song || 'Nothing playing';
artistalbum.textContent = spotify.artist ? `${spotify.artist} — ${spotify.album}` : '—';
albumart.src = spotify.album_art_url;
// Get dominant colors from album art and set CSS custom properties
getDominantColors(albumart.src, (mostAbundantColors, lessAbundantColors) => {
const accentPrimary = `rgb(${mostAbundantColors[0][0]}, ${mostAbundantColors[0][1]}, ${mostAbundantColors[0][2]})`;
const accentSecondary = `rgb(${mostAbundantColors[1][0]}, ${mostAbundantColors[1][1]}, ${mostAbundantColors[1][2]})`;
// Set CSS custom properties for accent colors
root.style.setProperty('--accent-primary', accentPrimary);
root.style.setProperty('--accent-secondary', accentSecondary);
// Update the accent indicator
if (accentIndicator) {
accentIndicator.style.backgroundColor = accentPrimary;
}
});
} else {
// Reset to default white accent when not playing
root.style.setProperty('--accent-primary', '#ffffff');
root.style.setProperty('--accent-secondary', '#888888');
if (accentIndicator) {
accentIndicator.style.backgroundColor = '#ffffff';
}
song.textContent = 'Nothing playing';
artistalbum.textContent = '—';
albumart.src = './img/spotify.png';
}
}
function startInterval() {
intervalId = setInterval(fetchDiscordData, 10 * 1000);
}
function clearCustomInterval() {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
}
async function getDominantColors(imageSrc, callback) {
try {
const img = new Image();
img.crossOrigin = 'Anonymous';
img.src = imageSrc;
img.onload = function () {
const colorThief = new ColorThief();
const mostAbundantColors = colorThief.getPalette(img, 5);
const lessAbundantColors = colorThief.getPalette(img, 15);
callback(mostAbundantColors, lessAbundantColors);
};
} catch (error) {
console.error('Error getting dominant colors:', error);
}
}
document.addEventListener('DOMContentLoaded', function () {
// Clear any lingering inline background styles to let CSS take over
document.body.style.background = '';
const albumArtElement = document.getElementById('albumart');
const songElement = document.getElementById('spotifysong');
if (albumArtElement) {
albumArtElement.addEventListener('click', () => {
if (previousData && previousData.spotify && previousData.spotify.track_id) {
window.open('https://open.spotify.com/track/' + previousData.spotify.track_id, '_blank');
}
});
}
if (songElement) {
songElement.addEventListener('click', () => {
fetchDiscordData();
});
}
window.addEventListener('beforeunload', () => {
clearCustomInterval();
});
window.addEventListener('load', () => {
startInterval();
fetchDiscordData();
});
});