mirror of
https://github.com/Mastermindzh/tidal-hifi.git
synced 2025-08-23 21:15:00 +02:00
various code improvements and some boyscout rule fixes :)
This commit is contained in:
@@ -4,20 +4,25 @@ import fs from "fs";
|
||||
import Player from "mpris-service";
|
||||
import { globalEvents } from "./constants/globalEvents";
|
||||
import { settings } from "./constants/settings";
|
||||
import { statuses } from "./constants/statuses";
|
||||
import { Songwhip } from "./features/songwhip/songwhip";
|
||||
import { ListenBrainz, ListenBrainzStore } from "./features/listenbrainz/listenbrainz";
|
||||
import {
|
||||
ListenBrainz,
|
||||
ListenBrainzConstants,
|
||||
ListenBrainzStore,
|
||||
} from "./features/listenbrainz/listenbrainz";
|
||||
import { Options } from "./models/options";
|
||||
import { downloadFile } from "./scripts/download";
|
||||
import { addHotkey } from "./scripts/hotkeys";
|
||||
import { settingsStore } from "./scripts/settings";
|
||||
import { setTitle } from "./scripts/window-functions";
|
||||
import { StoreData } from "./features/listenbrainz/models/storeData";
|
||||
import { MediaStatus } from "./models/mediaStatus";
|
||||
|
||||
const notificationPath = `${app.getPath("userData")}/notification.jpg`;
|
||||
const appName = "Tidal Hifi";
|
||||
let currentSong = "";
|
||||
let player: Player;
|
||||
let currentPlayStatus = statuses.paused;
|
||||
let currentPlayStatus = MediaStatus.paused;
|
||||
|
||||
const elements = {
|
||||
play: '*[data-test="play"]',
|
||||
@@ -106,7 +111,7 @@ const elements = {
|
||||
window.location.href.includes("/playlist/") ||
|
||||
window.location.href.includes("/mix/")
|
||||
) {
|
||||
if (currentPlayStatus === statuses.playing) {
|
||||
if (currentPlayStatus === MediaStatus.playing) {
|
||||
// find the currently playing element from the list (which might be in an album icon), traverse back up to the mediaItem (row) and select the album cell.
|
||||
// document.querySelector("[class^='isPlayingIcon'], [data-test-is-playing='true']").closest('[data-type="mediaItem"]').querySelector('[class^="album"]').textContent
|
||||
const row = window.document.querySelector(this.currentlyPlaying).closest(this.mediaItem);
|
||||
@@ -179,7 +184,7 @@ function addCustomCss() {
|
||||
* make sure it returns a number, if not use the default
|
||||
*/
|
||||
function getUpdateFrequency() {
|
||||
const storeValue = settingsStore.get(settings.updateFrequency) as number;
|
||||
const storeValue = settingsStore.get<string, number>(settings.updateFrequency);
|
||||
const defaultValue = 500;
|
||||
|
||||
if (!isNaN(storeValue)) {
|
||||
@@ -280,7 +285,7 @@ function handleLogout() {
|
||||
defaultId: 2,
|
||||
})
|
||||
.then((result: { response: number }) => {
|
||||
if (logoutOptions.indexOf("Yes, please") == result.response) {
|
||||
if (logoutOptions.indexOf("Yes, please") === result.response) {
|
||||
for (let i = 0; i < window.localStorage.length; i++) {
|
||||
const key = window.localStorage.key(i);
|
||||
if (key.startsWith("_TIDAL_activeSession")) {
|
||||
@@ -322,6 +327,8 @@ function addIPCEventListeners() {
|
||||
case globalEvents.pause:
|
||||
elements.click("pause");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -336,9 +343,9 @@ function getCurrentlyPlayingStatus() {
|
||||
|
||||
// if pause button is visible tidal is playing
|
||||
if (pause) {
|
||||
status = statuses.playing;
|
||||
status = MediaStatus.playing;
|
||||
} else {
|
||||
status = statuses.paused;
|
||||
status = MediaStatus.paused;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -363,27 +370,41 @@ function updateMediaInfo(options: Options, notify: boolean) {
|
||||
if (settingsStore.get(settings.notifications) && notify) {
|
||||
new Notification({ title: options.title, body: options.artists, icon: options.icon }).show();
|
||||
}
|
||||
if (player) {
|
||||
player.metadata = {
|
||||
...player.metadata,
|
||||
...{
|
||||
"xesam:title": options.title,
|
||||
"xesam:artist": [options.artists],
|
||||
"xesam:album": options.album,
|
||||
"mpris:artUrl": options.image,
|
||||
"mpris:length": convertDuration(options.duration) * 1000 * 1000,
|
||||
"mpris:trackid": "/org/mpris/MediaPlayer2/track/" + getTrackID(),
|
||||
},
|
||||
};
|
||||
player.playbackStatus = options.status == statuses.paused ? "Paused" : "Playing";
|
||||
}
|
||||
if (settingsStore.get(settings.ListenBrainz.enabled)) {
|
||||
const data = ListenBrainzStore.get("OldData") as string[];
|
||||
if (data && data[1] !== options.title) {
|
||||
ListenBrainz.scrobble(options.title, options.artists, options.status, convertDuration(options.duration));
|
||||
} else if (!data) {
|
||||
ListenBrainz.scrobble(options.title, options.artists, options.status, convertDuration(options.duration));
|
||||
}
|
||||
updateMpris(options);
|
||||
updateListenBrainz(options);
|
||||
}
|
||||
}
|
||||
|
||||
function updateMpris(options: Options) {
|
||||
if (player) {
|
||||
player.metadata = {
|
||||
...player.metadata,
|
||||
...{
|
||||
"xesam:title": options.title,
|
||||
"xesam:artist": [options.artists],
|
||||
"xesam:album": options.album,
|
||||
"mpris:artUrl": options.image,
|
||||
"mpris:length": convertDuration(options.duration) * 1000 * 1000,
|
||||
"mpris:trackid": "/org/mpris/MediaPlayer2/track/" + getTrackID(),
|
||||
},
|
||||
};
|
||||
player.playbackStatus = options.status === MediaStatus.paused ? "Paused" : "Playing";
|
||||
}
|
||||
}
|
||||
|
||||
function updateListenBrainz(options: Options) {
|
||||
if (settingsStore.get(settings.ListenBrainz.enabled)) {
|
||||
const oldData = ListenBrainzStore.get(ListenBrainzConstants.oldData) as StoreData;
|
||||
if (
|
||||
(!oldData && options.status === MediaStatus.playing) ||
|
||||
(oldData && oldData.title !== options.title)
|
||||
) {
|
||||
ListenBrainz.scrobble(
|
||||
options.title,
|
||||
options.artists,
|
||||
options.status,
|
||||
convertDuration(options.duration)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user