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 currentSong = "";
let player; let player;
let currentPlayStatus = statuses.paused; let currentPlayStatus = statuses.paused;
let barvalue; let progressBarTime;
let updatecurrent = false; let currentTimeChanged = false;
let oldcurrent; let currentTime;
let currentURL = undefined; let currentURL = undefined;
const elements = { 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 * Watch for song changes and update title + notify
*/ */
@ -252,7 +269,7 @@ setInterval(function () {
const artists = elements.getText("artists"); const artists = elements.getText("artists");
const current = elements.getText("current"); const current = elements.getText("current");
const duration = elements.getText("duration"); 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 songDashArtistTitle = `${title} - ${artists}`;
const currentStatus = getCurrentlyPlayingStatus(); const currentStatus = getCurrentlyPlayingStatus();
const options = { const options = {
@ -265,45 +282,35 @@ setInterval(function () {
}; };
const playStatusChanged = currentStatus !== currentPlayStatus; const playStatusChanged = currentStatus !== currentPlayStatus;
const barvalChanged = barval !== barvalue; const progressBarTimeChanged = progressBarcurrentTime !== progressBarTime;
const titleOrArtistChanged = currentSong !== songDashArtistTitle; const titleOrArtistChanged = currentSong !== songDashArtistTitle;
if (titleOrArtistChanged || playStatusChanged || barvalChanged || updatecurrent) { if (titleOrArtistChanged || playStatusChanged || progressBarTimeChanged || currentTimeChanged) {
// update title and play info with new info // update title, url and play info with new info
setTitle(songDashArtistTitle); setTitle(songDashArtistTitle);
updateURL();
currentSong = songDashArtistTitle; currentSong = songDashArtistTitle;
currentPlayStatus = currentStatus; currentPlayStatus = currentStatus;
// check progress bar value and make sure current stays up to date after switch // check progress bar value and make sure current stays up to date after switch
if(barvalue != barval && !titleOrArtistChanged) { if(progressBarTime != progressBarcurrentTime && !titleOrArtistChanged) {
barvalue = barval; progressBarTime = progressBarcurrentTime;
oldcurrent = options.current; currentTime = options.current;
options.duration = duration; 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. if(currentTimeChanged) {
switch(elements.get("title").querySelector("a")) { if(options.current == currentTime && currentStatus != "paused") return;
case null: currentTime = options.current;
currentURL = undefined; currentTimeChanged = false;
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;
} }
// make sure current is set to 0 if title changes // make sure current is set to 0 if title changes
if(titleOrArtistChanged) { if(titleOrArtistChanged) {
options.current = "0:00"; options.current = "0:00";
oldcurrent = options.current; currentTime = options.current;
barvalue = barval; progressBarTime = progressBarcurrentTime;
} }
const image = elements.getSongIcon(); const image = elements.getSongIcon();