Various WebNowPlaying plugin updates (#394)

* Create WebNowPlaying plugin

* Only run WebNowPlaying plugin on win32

* [WebNowPlaying] Fix time formatting

* [WebNowPlaying] Minor code cleanup

Co-authored-by: Quacksire <19170969+quacksire@users.noreply.github.com>
This commit is contained in:
Jozen Blue Martinez 2022-02-09 09:19:42 +08:00 committed by GitHub
parent 43bf12d284
commit 988b10a13f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,11 +15,11 @@ const pad = (number: number, length: number) => String(number).padStart(length,
* @returns String * @returns String
*/ */
const convertTimeToString = (timeInSeconds: number) => { const convertTimeToString = (timeInSeconds: number) => {
const timeInMinutes = timeInSeconds / 60; const timeInMinutes = Math.floor(timeInSeconds / 60);
if (timeInMinutes < 60) { if (timeInMinutes < 60) {
return timeInMinutes + ":" + pad(timeInSeconds % 60, 2); return timeInMinutes + ":" + pad(Math.floor(timeInSeconds % 60), 2);
} }
return timeInMinutes / 60 + ":" + pad(timeInMinutes % 60, 2) + ":" + pad(timeInSeconds % 60, 2); return Math.floor(timeInMinutes / 60) + ":" + pad(Math.floor(timeInMinutes % 60), 2) + ":" + pad(Math.floor(timeInSeconds % 60), 2);
} }
export default class WebNowPlaying { export default class WebNowPlaying {
@ -40,7 +40,20 @@ export default class WebNowPlaying {
console.debug(`[Plugin][${this.name}] Loading Complete.`); console.debug(`[Plugin][${this.name}] Loading Complete.`);
} }
sendSongInfo(attributes: any) { /**
* Blocks non-windows systems from running this plugin
* @private
* @decorator
*/
private static windowsOnly(_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
if (process.platform !== 'win32') {
descriptor.value = function () {
return
}
}
}
private sendSongInfo(attributes: any) {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return; if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
const fields = ['STATE', 'TITLE', 'ARTIST', 'ALBUM', 'COVER', 'DURATION', 'POSITION', 'VOLUME', 'REPEAT', 'SHUFFLE']; const fields = ['STATE', 'TITLE', 'ARTIST', 'ALBUM', 'COVER', 'DURATION', 'POSITION', 'VOLUME', 'REPEAT', 'SHUFFLE'];
@ -88,8 +101,7 @@ export default class WebNowPlaying {
} }
}); });
} }
private fireEvent(evt: any) {
fireEvent(evt: any) {
if (!evt.data) return; if (!evt.data) return;
let value = ''; let value = '';
if (evt.data.split(/ (.+)/).length > 1) { if (evt.data.split(/ (.+)/).length > 1) {
@ -142,7 +154,8 @@ export default class WebNowPlaying {
/** /**
* Runs on app ready * Runs on app ready
*/ */
onReady(win: any) { @WebNowPlaying.windowsOnly
public onReady(win: any) {
this._win = win; this._win = win;
// Connect to Rainmeter plugin and retry on disconnect. // Connect to Rainmeter plugin and retry on disconnect.
@ -197,7 +210,8 @@ export default class WebNowPlaying {
/** /**
* Runs on app stop * Runs on app stop
*/ */
onBeforeQuit() { @WebNowPlaying.windowsOnly
public onBeforeQuit() {
if (this.ws) { if (this.ws) {
this.ws.send('STATE:0'); this.ws.send('STATE:0');
this.ws.onclose = null; // disable onclose handler first to stop it from retrying this.ws.onclose = null; // disable onclose handler first to stop it from retrying
@ -221,7 +235,8 @@ export default class WebNowPlaying {
* Runs on song change * Runs on song change
* @param attributes Music Attributes * @param attributes Music Attributes
*/ */
onNowPlayingItemDidChange(attributes: any) { @WebNowPlaying.windowsOnly
public onNowPlayingItemDidChange(attributes: any) {
this.sendSongInfo(attributes); this.sendSongInfo(attributes);
} }
} }