forked from tjhrulz/WebNowPlaying-BrowserExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-now-playing.js
334 lines (319 loc) · 13.3 KB
/
web-now-playing.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// ASCII titles generated with https://patorjk.com/software/taag/#p=display&f=Roman&t=THIS
/*
oooooo oooo .o. ooooooooo. ooooo .o. oooooooooo. ooooo oooooooooooo .oooooo..o
`888. .8' .888. `888 `Y88. `888' .888. `888' `Y8b `888' `888' `8 d8P' `Y8
`888. .8' .8"888. 888 .d88' 888 .8"888. 888 888 888 888 Y88bo.
`888. .8' .8' `888. 888ooo88P' 888 .8' `888. 888oooo888' 888 888oooo8 `"Y8888o.
`888.8' .88ooo8888. 888`88b. 888 .88ooo8888. 888 `88b 888 888 " `"Y88b
`888' .8' `888. 888 `88b. 888 .8' `888. 888 .88P 888 o 888 o oo .d8P
`8' o88o o8888o o888o o888o o888o o88o o8888o o888bood8P' o888ooooood8 o888ooooood8 8""88888P'
*/
let websocket;
let sendData;
let outdatedCheck;
// Use this object to define custom logic to retrieve data
let musicInfo = {
// Mandatory, just give the player name
player: null,
// Check player is ready to start doing info checks. ie. it is fully loaded and has the song title
// While false no other info checks will be called
readyCheck: null,
state: null,
title: null,
artist: null,
album: null,
cover: null,
duration: null,
durationString: null,
position: null,
positionString: null,
volume: null,
rating: null,
repeat: null,
shuffle: null
};
// Use this object to define custom event logic
let musicEventHandler = {
readyCheck: null,
playpause: null,
next: null,
previous: null,
progress: null,
progressSeconds: null,
volume: null,
repeat: null,
shuffle: null,
toggleThumbsUp: null,
toggleThumbsDown: null,
rating: null
};
// This is used to cache recently sent values so updates only send new values
const current = {
player: null, // Always make sure this is set
state: null,
title: null,
artist: null,
album: null,
cover: null,
duration: null,
position: null,
volume: null,
rating: null,
repeat: null,
shuffle: null
};
var MusicInfos;
(function (MusicInfos) {
MusicInfos['STATE'] = 'state';
MusicInfos['TITLE'] = 'title';
MusicInfos['ARTIST'] = 'artist';
MusicInfos['ALBUM'] = 'album';
MusicInfos['COVER'] = 'cover';
MusicInfos['DURATION'] = 'duration';
MusicInfos['POSITION'] = 'position';
MusicInfos['VOLUME'] = 'volume';
MusicInfos['RATING'] = 'rating';
MusicInfos['REPEAT'] = 'repeat';
MusicInfos['SHUFFLE'] = 'shuffle';
})(MusicInfos || (MusicInfos = {}));
/*
ooooo ooooo oooooooooooo ooooo ooooooooo. oooooooooooo ooooooooo. .oooooo..o
`888' `888' `888' `8 `888' `888 `Y88. `888' `8 `888 `Y88. d8P' `Y8
888 888 888 888 888 .d88' 888 888 .d88' Y88bo.
888ooooo888 888oooo8 888 888ooo88P' 888oooo8 888ooo88P' `"Y8888o.
888 888 888 " 888 888 888 " 888`88b. `"Y88b
888 888 888 o 888 o 888 888 o 888 `88b. oo .d8P
o888o o888o o888ooooood8 o888ooooood8 o888o o888ooooood8 o888o o888o 8""88888P'
*/
function pad(number) {
let numberAsString = String(number);
return numberAsString.length < 2 ? `0${numberAsString}` : numberAsString;
}
// Convert seconds to a time string acceptable to Rainmeter
function convertNumberToTimeString(numberInSeconds) {
const numberInMinutes = parseInt(numberInSeconds / 60);
if (numberInMinutes < 60) {
return `${numberInMinutes}:${pad(parseInt(numberInSeconds % 60))}`;
}
return `${parseInt(numberInMinutes / 60)}:${pad(parseInt(numberInMinutes % 60))}:${pad(parseInt(numberInSeconds % 60))}`;
}
/*
ooooo ooo ooooooooo. oooooooooo. .o. ooooooooooooo oooooooooooo ooooooooo.
`888' `8' `888 `Y88. `888' `Y8b .888. 8' 888 `8 `888' `8 `888 `Y88.
888 8 888 .d88' 888 888 .8"888. 888 888 888 .d88'
888 8 888ooo88P' 888 888 .8' `888. 888 888oooo8 888ooo88P'
888 8 888 888 888 .88ooo8888. 888 888 " 888`88b.
`88. .8' 888 888 d88' .8' `888. 888 888 o 888 `88b.
`YbodP' o888o o888bood8P' o88o o8888o o888o o888ooooood8 o888o o888o
*/
function updateInfo() {
if (musicInfo.readyCheck === null || musicInfo.readyCheck()) {
for (const [key, value] of Object.entries(MusicInfos)) {
// Try catch for each updater to make sure info is fail safe
try {
if (value === MusicInfos.DURATION || value === MusicInfos.POSITION) {
if (musicInfo[`${value}String`] !== null && musicInfo[`${value}String`] !== undefined) {
const temp = musicInfo[`${value}String`]();
if (current[value] !== temp && temp !== null) {
websocket.send(`${key}:${temp}`);
current[value] = temp;
}
} else if (musicInfo[value] !== null && musicInfo[value] !== undefined) {
const temp = isNaN(musicInfo[value]()) ? null : convertNumberToTimeString(musicInfo[value]());
if (current[value] !== temp && temp !== null) {
websocket.send(`${key}:${temp}`);
current[value] = temp;
}
}
} else if (value === MusicInfos.VOLUME) {
if (musicInfo[value] !== null && musicInfo[value] !== undefined) {
const temp = isNaN(musicInfo[value]()) ? null : parseFloat(musicInfo.volume()) * 100;
if (current[value] !== temp && temp !== null) {
websocket.send(`${key}:${temp}`);
current[value] = temp;
}
}
} else {
if (musicInfo[value] !== null && musicInfo[value] !== undefined) {
const temp = musicInfo[value]();
if (current[value] !== temp && temp !== null) {
websocket.send(`${key}:${temp}`);
current[value] = temp;
}
}
}
} catch (e) {
websocket.send(`Error: [Cannot update "${value}" for ${musicInfo.player()}]`);
websocket.send(`ErrorD: [${e}]`);
throw e;
}
}
} else {
// TODO: Maybe make it so it clears data/disconnects if this is true and not just sets music to stopped
if (current.state !== 0) {
websocket.send('STATE:0');
current.state = 0;
}
}
}
/*
oooooooooooo oooooo oooo oooooooooooo ooooo ooo ooooooooooooo .oooooo..o
`888' `8 `888. .8' `888' `8 `888b. `8' 8' 888 `8 d8P' `Y8
888 `888. .8' 888 8 `88b. 8 888 Y88bo.
888oooo8 `888. .8' 888oooo8 8 `88b. 8 888 `"Y8888o.
888 " `888.8' 888 " 8 `88b.8 888 `"Y88b
888 o `888' 888 o 8 `888 888 oo .d8P
o888ooooood8 `8' o888ooooood8 o8o `8 o888o 8""88888P'
*/
function fireEvent(event) {
try {
if (musicEventHandler.readyCheck === null || musicEventHandler.readyCheck()) {
if (event.data.toLowerCase() === 'playpause' && musicEventHandler.playpause !== null && musicEventHandler.playpause !== undefined) {
musicEventHandler.playpause();
} else if (event.data.toLowerCase() === 'next' && musicEventHandler.next !== null && musicEventHandler.next !== undefined) {
musicEventHandler.next();
} else if (
event.data.toLowerCase() === 'previous' &&
musicEventHandler.previous !== null &&
musicEventHandler.previous !== undefined
) {
musicEventHandler.previous();
} else if (event.data.toLowerCase().includes('setprogress ') || event.data.toLowerCase().includes('setposition ')) {
if (musicEventHandler.progress !== null && musicEventHandler.progress !== undefined) {
let progress = event.data.toLowerCase();
// +9 because "progress " is 9 chars
progress = progress.substring(progress.indexOf('progress ') + 9);
// Goto the : at the end of the command, this command is now a compound command the first half is seconds the second is percent
progress = parseFloat(progress.substring(0, progress.indexOf(':')));
musicEventHandler.progress(progress);
} else if (musicEventHandler.progressSeconds !== null && musicEventHandler.progressSeconds !== undefined) {
let position = event.data.toLowerCase();
// +9 because "position " is 9 chars
position = position.substring(position.indexOf('position ') + 9);
// Goto the : at the end of the command, this command is now a compound command the first half is seconds the second is percent
position = parseInt(position.substring(0, position.indexOf(':')));
musicEventHandler.progressSeconds(position);
}
} else if (
event.data.toLowerCase().includes('setvolume ') &&
musicEventHandler.volume !== null &&
musicEventHandler.volume !== undefined
) {
let volume = event.data.toLowerCase();
// +7 because "volume " is 7 chars
volume = parseInt(volume.substring(volume.indexOf('volume ') + 7)) / 100;
musicEventHandler.volume(volume);
} else if (event.data.toLowerCase() === 'repeat' && musicEventHandler.repeat !== null && musicEventHandler.repeat !== undefined) {
musicEventHandler.repeat();
} else if (event.data.toLowerCase() === 'shuffle' && musicEventHandler.shuffle !== null && musicEventHandler.shuffle !== undefined) {
musicEventHandler.shuffle();
} else if (
event.data.toLowerCase() === 'togglethumbsup' &&
musicEventHandler.toggleThumbsUp !== null &&
musicEventHandler.toggleThumbsUp !== undefined
) {
musicEventHandler.toggleThumbsUp();
} else if (
event.data.toLowerCase() === 'togglethumbsdown' &&
musicEventHandler.toggleThumbsDown !== null &&
musicEventHandler.toggleThumbsDown !== undefined
) {
musicEventHandler.toggleThumbsDown();
} else if (
event.data.toLowerCase().includes('rating') &&
musicEventHandler.rating !== null &&
musicEventHandler.rating !== undefined
) {
let rating = event.data.toLowerCase();
// Check if additional rating info is given
if (rating.length > 6) {
// +7 because "rating " is 7 chars
rating = parseInt(rating.substring(rating.indexOf('rating ') + 7));
} else {
rating = 0;
}
musicEventHandler.rating(rating);
}
}
} catch (e) {
websocket.send(`Error: [Cannot send event to ${musicInfo.player()}]`);
websocket.send(`ErrorD: [${e}]`);
throw e;
}
}
/*
.oooooo..o oooooooooooo ooooooooooooo ooooo ooo ooooooooo.
d8P' `Y8 `888' `8 8' 888 `8 `888' `8' `888 `Y88.
Y88bo. 888 888 888 8 888 .d88'
`"Y8888o. 888oooo8 888 888 8 888ooo88P'
`"Y88b 888 " 888 888 8 888
oo .d8P 888 o 888 `88. .8' 888
8""88888P' o888ooooood8 o888o `YbodP' o888o
*/
// Max length of time between attempting to reconnect
// 1000 = 1s * 60 = 1min
const MAXTIMEOUT = 1000 * 60;
let connectionAttemptCount = 0;
// eslint-disable-next-line no-unused-vars
function init() {
// TODO: allow custom ports
const url = 'ws://127.0.0.1:8974/';
websocket = new WebSocket(url);
websocket.onopen = () => {
// Reset connection attempts since we are now connected
connectionAttemptCount = 0;
current.player = musicInfo.player();
websocket.send(`PLAYER:${current.player}`);
// If this is not cleared in 1s then assume plugin version is so old it has no version send
outdatedCheck = setTimeout(() => {
chrome.runtime.sendMessage({ method: 'flagAsOutdated' });
}, 1000);
// TODO: Dynamic update rate based on success rate
sendData = setInterval(() => {
if (websocket.readyState === WebSocket.OPEN) {
updateInfo();
}
}, 100);
};
websocket.onclose = () => {
chrome.runtime.sendMessage({ method: 'flagAsNotConnected' });
clearInterval(sendData);
connectionAttemptCount++;
// If we fail repeatedly, exponentially increase the length of time between connection attempts
// Cap at maximum timeout
const timeoutLength = Math.min(1000 + Math.pow(10, connectionAttemptCount / 4), MAXTIMEOUT);
setTimeout(() => {
init();
}, timeoutLength);
};
websocket.onmessage = (event) => {
const versionNumber = event.data.toLowerCase().split(':');
if (versionNumber[0].includes('version')) {
// Check that version number is the same major version
if (versionNumber[1].split('.')[1] < 5) {
chrome.runtime.sendMessage({ method: 'flagAsOutdated' });
} else {
// Clear timeout set above
clearTimeout(outdatedCheck);
chrome.runtime.sendMessage({ method: 'flagAsConnected' });
}
}
try {
fireEvent(event);
} catch (e) {
websocket.send(`Error: [${e}]`);
throw e;
}
};
websocket.onerror = (event) => {
if (typeof event.data !== 'undefined') {
throw event;
}
};
}
window.onbeforeunload = () => {
if (websocket !== undefined) {
websocket.onclose = () => {}; // disable onclose handler first
websocket.close();
}
};