extract Switch to function and change variable names.

This commit is contained in:
Marie 2021-04-22 11:33:57 +02:00 committed by GitHub
parent c65d1a56c8
commit 10c1e57680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,9 +12,9 @@ const notificationPath = `${app.getPath("userData")}/notification.jpg`;
let currentSong = "";
let player;
let currentPlayStatus = statuses.paused;
let barvalue;
let updatecurrent = false;
let oldcurrent;
let progressBarTime;
let currentTimeChanged = false;
let currentTime;
let currentURL = undefined;
const elements = {
@ -244,6 +244,23 @@ function updateMediaInfo(options, notify) {
}
}
/**
* Checks if Tidal is playing a video or song by grabbing the "a" element from the title.
* If it's a song it sets the track URL as currentURL, If it's a video it will set currentURL to undefined.
*/
function updateURL() {
const URLelement = elements.get("title").querySelector("a");
switch (URLelement) {
case null:
currentURL = undefined;
break;
default:
const id = URLelement.href.replace(/[^0-9]/g, "");
currentURL = `https://tidal.com/browse/track/${id}`;
break;
}
}
/**
* Watch for song changes and update title + notify
*/
@ -252,7 +269,7 @@ setInterval(function () {
const artists = elements.getText("artists");
const current = elements.getText("current");
const duration = elements.getText("duration");
const barval = elements.get("bar").getAttribute("aria-valuenow");
const progressBarcurrentTime = elements.get("bar").getAttribute("aria-valuenow");
const songDashArtistTitle = `${title} - ${artists}`;
const currentStatus = getCurrentlyPlayingStatus();
const options = {
@ -265,45 +282,35 @@ setInterval(function () {
};
const playStatusChanged = currentStatus !== currentPlayStatus;
const barvalChanged = barval !== barvalue;
const progressBarTimeChanged = progressBarcurrentTime !== progressBarTime;
const titleOrArtistChanged = currentSong !== songDashArtistTitle;
if (titleOrArtistChanged || playStatusChanged || barvalChanged || updatecurrent) {
// update title and play info with new info
if (titleOrArtistChanged || playStatusChanged || progressBarTimeChanged || currentTimeChanged) {
// update title, url and play info with new info
setTitle(songDashArtistTitle);
updateURL();
currentSong = songDashArtistTitle;
currentPlayStatus = currentStatus;
// check progress bar value and make sure current stays up to date after switch
if(barvalue != barval && !titleOrArtistChanged) {
barvalue = barval;
oldcurrent = options.current;
if(progressBarTime != progressBarcurrentTime && !titleOrArtistChanged) {
progressBarTime = progressBarcurrentTime;
currentTime = options.current;
options.duration = duration;
updatecurrent = true;
currentTimeChanged = true;
}
// Video/Song check if it's a video return URL as undefined due to it not having an id.
switch(elements.get("title").querySelector("a")) {
case null:
currentURL = undefined;
break;
default:
const id = elements.get("title").querySelector("a").href.replace(/[^0-9]/g, "");
currentURL = `https://tidal.com/browse/track/${id}`;
break;
}
if(updatecurrent) {
if(options.current == oldcurrent && currentStatus != "paused") return;
oldcurrent = options.current;
updatecurrent = false;
if(currentTimeChanged) {
if(options.current == currentTime && currentStatus != "paused") return;
currentTime = options.current;
currentTimeChanged = false;
}
// make sure current is set to 0 if title changes
if(titleOrArtistChanged) {
options.current = "0:00";
oldcurrent = options.current;
barvalue = barval;
currentTime = options.current;
progressBarTime = progressBarcurrentTime;
}
const image = elements.getSongIcon();