forked from nuttylmao/apple-music-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
229 lines (179 loc) · 6.16 KB
/
script.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
///////////////
// PARAMETRS //
///////////////
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const visibilityDuration = urlParams.get("duration") || 0;
const hideAlbumArt = urlParams.has("hideAlbumArt");
/////////////////
// GLOBAL VARS //
/////////////////
let animationSpeed = 0.5;
//////////////////////
// WEBSOCKET SERVER //
//////////////////////
// This is the main function that connects to the Streamer.bot websocket server
function connectws() {
if ("WebSocket" in window) {
//const ws = new WebSocket("ws://" + sbServerAddress + ":" + sbServerPort + "/");
const CiderApp = io("http://localhost:10767/", {
transports: ['websocket']
});
CiderApp.on("disconnect", (event) => {
SetConnectionStatus(false);
setTimeout(connectws, 5000);
});
CiderApp.on("connect", (event) => {
SetConnectionStatus(true);
});
// Set up websocket artwork/information handling
CiderApp.on("API:Playback", ({ data, type }) => {
switch (type) {
// Song changes
case ("playbackStatus.nowPlayingItemDidChange"):
UpdateSongInfo(data);
break;
// Progress bar moves
case ("playbackStatus.playbackTimeDidChange"):
UpdateProgressBar(data);
break;
// Pause/unpause
case ("playbackStatus.playbackStateDidChange"):
UpdatePlaybackState(data);
break;
}
});
}
}
////////////////////////
// NOW PLAYING WIDGET //
////////////////////////
function UpdateSongInfo(data) {
// Set the user's info
let albumArtUrl = data.artwork.url;
albumArtUrl = albumArtUrl.replace("{w}", data.artwork.width);
albumArtUrl = albumArtUrl.replace("{h}", data.artwork.height);
UpdateAlbumArt(document.getElementById("albumArt"), albumArtUrl);
UpdateAlbumArt(document.getElementById("backgroundImage"), albumArtUrl);
setTimeout(() => {
UpdateTextLabel(document.getElementById("songLabel"), data.name);
UpdateTextLabel(document.getElementById("artistLabel"), data.artistName);
}, animationSpeed * 500);
setTimeout(() => {
document.getElementById("albumArtBack").src = albumArtUrl;
document.getElementById("backgroundImageBack").src = albumArtUrl;
}, 2 * animationSpeed * 500);
if (visibilityDuration > 0) {
setTimeout(() => {
SetVisibility(false);
}, visibilityDuration * 1000);
}
}
function UpdateTextLabel(div, text) {
if (div.innerHTML != text) {
div.setAttribute("class", "text-fade");
setTimeout(() => {
div.innerHTML = text;
div.setAttribute("class", ".text-show");
}, animationSpeed * 250);
}
}
function UpdateAlbumArt(div, imgsrc) {
if (div.src != imgsrc) {
div.setAttribute("class", "text-fade");
setTimeout(() => {
div.src = imgsrc;
div.setAttribute("class", "text-show");
}, animationSpeed * 500);
}
}
function UpdateProgressBar(data) {
const progress = ((data.currentPlaybackTime / data.currentPlaybackDuration) * 100);
const progressTime = ConvertSecondsToMinutesSoThatItLooksBetterOnTheOverlay(data.currentPlaybackTime);
const duration = ConvertSecondsToMinutesSoThatItLooksBetterOnTheOverlay(data.currentPlaybackTimeRemaining);
document.getElementById("progressBar").style.width = `${progress}%`;
document.getElementById("progressTime").innerHTML = progressTime;
document.getElementById("duration").innerHTML = `-${duration}`;
}
function UpdatePlaybackState(data) {
console.log(data);
switch (data.state) {
case ("paused"):
case ("stopped"):
SetVisibility(false);
break;
case ("playing"):
UpdateSongInfo(data.attributes);
setTimeout(() => {
SetVisibility(true);
}, animationSpeed * 500);
break;
}
}
//////////////////////
// HELPER FUNCTIONS //
//////////////////////
function ConvertSecondsToMinutesSoThatItLooksBetterOnTheOverlay(time) {
const minutes = Math.floor(time / 60);
const seconds = Math.trunc(time - minutes * 60);
return `${minutes}:${('0' + seconds).slice(-2)}`;
}
function SetVisibility(isVisible) {
widgetVisibility = isVisible;
const mainContainer = document.getElementById("mainContainer");
if (isVisible) {
var tl = new TimelineMax();
tl
.to(mainContainer, animationSpeed, { bottom: "50%", ease: Power1.easeInOut }, 'label')
.to(mainContainer, animationSpeed, { opacity: 1, ease: Power1.easeInOut }, 'label')
}
else {
var tl = new TimelineMax();
tl
.to(mainContainer, animationSpeed, { bottom: "45%", ease: Power1.easeInOut }, 'label')
.to(mainContainer, animationSpeed, { opacity: 0, ease: Power1.easeInOut }, 'label')
}
}
///////////////////////////////////
// STREAMER.BOT WEBSOCKET STATUS //
///////////////////////////////////
// This function sets the visibility of the Streamer.bot status label on the overlay
function SetConnectionStatus(connected) {
let statusContainer = document.getElementById("statusContainer");
if (connected) {
statusContainer.style.background = "#2FB774";
statusContainer.innerText = "Connected!";
var tl = new TimelineMax();
tl
.to(statusContainer, 2, { opacity: 0, ease: Linear.easeNone });
console.log("Connected to Cider!");
}
else {
// statusContainer.style.background = "#D12025";
// statusContainer.innerText = "Connecting...";
// statusContainer.style.opacity = 1;
console.log("Not connected to Cider...");
}
}
//////////////////////////////////////////////////////////////////////////////////////////
// RESIZER THING BECAUSE I THINK I KNOW HOW RESPONSIVE DESIGN WORKS EVEN THOUGH I DON'T //
//////////////////////////////////////////////////////////////////////////////////////////
let outer = document.getElementById('mainContainer'),
maxWidth = outer.clientWidth,
maxHeight = outer.clientHeight;
window.addEventListener("resize", resize);
resize();
function resize() {
const scale = window.innerWidth / maxWidth;
outer.style.transform = 'translate(-50%, 50%) scale(' + scale + ')';
}
/////////////////////////////////////////////////////////////////////
// IF THE USER PUT IN THE HIDEALBUMART PARAMATER, THEN YOU SHOULD //
// HIDE THE ALBUM ART, BECAUSE THAT'S WHAT IT'S SUPPOSED TO DO //
/////////////////////////////////////////////////////////////////////
if (hideAlbumArt)
{
document.getElementById("albumArtBox").style.display = "none";
document.getElementById("songInfoBox").style.width = "calc(100% - 20px)";
}
connectws();