2019-10-20 22:47:01 +02:00
|
|
|
const { setTitle, getTitle } = require("./scripts/window-functions");
|
|
|
|
const { dialog } = require("electron").remote;
|
2019-10-30 22:49:04 +01:00
|
|
|
const { settings } = require("./scripts/settings");
|
2019-10-22 21:25:57 +02:00
|
|
|
const { ipcRenderer } = require("electron");
|
|
|
|
const { app } = require("electron").remote;
|
|
|
|
const { downloadFile } = require("./scripts/download");
|
2019-10-30 23:42:08 +01:00
|
|
|
const statuses = require("./constants/statuses");
|
2019-10-30 22:49:04 +01:00
|
|
|
const hotkeys = require("./scripts/hotkeys");
|
|
|
|
const globalEvents = require("./constants/globalEvents");
|
|
|
|
const notifier = require("node-notifier");
|
2019-10-22 21:25:57 +02:00
|
|
|
const notificationPath = `${app.getPath("userData")}/notification.jpg`;
|
2019-10-20 22:47:01 +02:00
|
|
|
|
|
|
|
const elements = {
|
|
|
|
play: '*[data-test="play"]',
|
|
|
|
pause: '*[data-test="pause"]',
|
|
|
|
next: '*[data-test="next"]',
|
|
|
|
previous: 'button[data-test="previous"]',
|
|
|
|
title: '*[data-test^="footer-track-title"]',
|
|
|
|
artists: '*[class^="mediaArtists"]',
|
|
|
|
home: '*[data-test="menu--home"]',
|
|
|
|
back: '[class^="backwardButton"]',
|
|
|
|
forward: '[class^="forwardButton"]',
|
|
|
|
search: '[class^="searchField"]',
|
|
|
|
shuffle: '*[data-test="shuffle"]',
|
|
|
|
repeat: '*[data-test="repeat"]',
|
|
|
|
block: '[class="blockButton"]',
|
|
|
|
account: '*[data-test^="profile-image-button"]',
|
|
|
|
settings: '*[data-test^="open-settings"]',
|
2019-10-22 21:25:57 +02:00
|
|
|
media: '*[data-test="current-media-imagery"]',
|
|
|
|
image: '*[class^="image--"]',
|
2019-10-20 22:47:01 +02:00
|
|
|
|
2019-10-22 21:25:57 +02:00
|
|
|
/**
|
|
|
|
* Get an element from the dom
|
|
|
|
* @param {*} key key in elements object to fetch
|
|
|
|
*/
|
2019-10-20 22:47:01 +02:00
|
|
|
get: function(key) {
|
|
|
|
return window.document.querySelector(this[key.toLowerCase()]);
|
|
|
|
},
|
|
|
|
|
2019-10-22 21:25:57 +02:00
|
|
|
/**
|
|
|
|
* Get the icon of the current song
|
|
|
|
*/
|
|
|
|
getSongIcon: function() {
|
|
|
|
const figure = this.get("media");
|
|
|
|
|
|
|
|
if (figure) {
|
|
|
|
const mediaElement = figure.querySelector(this["image"]);
|
|
|
|
if (mediaElement) {
|
|
|
|
return mediaElement.src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shorthand function to get the text of a dom element
|
|
|
|
* @param {*} key key in elements object to fetch
|
|
|
|
*/
|
2019-10-20 22:47:01 +02:00
|
|
|
getText: function(key) {
|
2019-10-22 21:25:57 +02:00
|
|
|
const element = this.get(key);
|
|
|
|
return element ? element.textContent : "";
|
2019-10-20 22:47:01 +02:00
|
|
|
},
|
|
|
|
|
2019-10-22 21:25:57 +02:00
|
|
|
/**
|
|
|
|
* Shorthand function to click a dom element
|
|
|
|
* @param {*} key key in elements object to fetch
|
|
|
|
*/
|
2019-10-20 22:47:01 +02:00
|
|
|
click: function(key) {
|
|
|
|
this.get(key).click();
|
|
|
|
return this;
|
|
|
|
},
|
2019-10-22 21:25:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shorthand function to focus a dom element
|
|
|
|
* @param {*} key key in elements object to fetch
|
|
|
|
*/
|
2019-10-20 22:47:01 +02:00
|
|
|
focus: function(key) {
|
|
|
|
return this.get(key).focus();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Play or pause the current song
|
|
|
|
*/
|
|
|
|
function playPause() {
|
|
|
|
const play = elements.get("play");
|
|
|
|
|
|
|
|
if (play) {
|
|
|
|
elements.click("play");
|
|
|
|
} else {
|
|
|
|
elements.click("pause");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add hotkeys for when tidal is focused
|
|
|
|
* Reflects the desktop hotkeys found on:
|
|
|
|
* https://defkey.com/tidal-desktop-shortcuts
|
|
|
|
*/
|
|
|
|
function addHotKeys() {
|
|
|
|
hotkeys.add("Control+p", function() {
|
|
|
|
elements.click("account").click("settings");
|
|
|
|
});
|
|
|
|
hotkeys.add("Control+l", function() {
|
|
|
|
handleLogout();
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("Control+h", function() {
|
|
|
|
elements.click("home");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("backspace", function() {
|
|
|
|
elements.click("back");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("shift+backspace", function() {
|
|
|
|
elements.click("forward");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+f", function() {
|
|
|
|
elements.focus("search");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+u", function() {
|
|
|
|
// reloading window without cache should show the update bar if applicable
|
|
|
|
window.location.reload(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+left", function() {
|
|
|
|
elements.click("previous");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+right", function() {
|
|
|
|
elements.click("next");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+right", function() {
|
|
|
|
elements.click("next");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+s", function() {
|
|
|
|
elements.click("shuffle");
|
|
|
|
});
|
|
|
|
|
|
|
|
hotkeys.add("control+r", function() {
|
|
|
|
elements.click("repeat");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will ask the user whether he/she wants to log out.
|
|
|
|
* It will log the user out if he/she selects "yes"
|
|
|
|
*/
|
|
|
|
function handleLogout() {
|
|
|
|
const logoutOptions = ["Cancel", "Yes, please", "No, thanks"];
|
|
|
|
|
|
|
|
dialog.showMessageBox(
|
|
|
|
null,
|
|
|
|
{
|
|
|
|
type: "question",
|
|
|
|
title: "Logging out",
|
|
|
|
message: "Are you sure you want to log out?",
|
|
|
|
buttons: logoutOptions,
|
|
|
|
defaultId: 2,
|
|
|
|
},
|
|
|
|
function(response) {
|
|
|
|
if (logoutOptions.indexOf("Yes, please") == response) {
|
|
|
|
for (i = 0; i < window.localStorage.length; i++) {
|
|
|
|
key = window.localStorage.key(i);
|
|
|
|
if (key.startsWith("_TIDAL_activeSession")) {
|
|
|
|
window.localStorage.removeItem(key);
|
|
|
|
i = window.localStorage.length + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.location.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add ipc event listeners.
|
|
|
|
* Some actions triggered outside of the site need info from the site.
|
|
|
|
*/
|
|
|
|
function addIPCEventListeners() {
|
|
|
|
window.addEventListener("DOMContentLoaded", () => {
|
2019-10-22 21:25:57 +02:00
|
|
|
ipcRenderer.on("getPlayInfo", () => {
|
2019-10-20 22:47:01 +02:00
|
|
|
alert(`${elements.getText("title")} - ${elements.getText("artists")}`);
|
|
|
|
});
|
2019-10-22 21:25:57 +02:00
|
|
|
|
|
|
|
ipcRenderer.on("globalEvent", (event, args) => {
|
|
|
|
switch (args) {
|
2019-10-30 22:49:04 +01:00
|
|
|
case globalEvents.playPause:
|
2019-10-22 21:25:57 +02:00
|
|
|
playPause();
|
|
|
|
break;
|
2019-10-30 22:49:04 +01:00
|
|
|
case globalEvents.next:
|
2019-10-22 21:25:57 +02:00
|
|
|
elements.click("next");
|
|
|
|
break;
|
2019-10-30 22:49:04 +01:00
|
|
|
case globalEvents.previous:
|
2019-10-22 21:25:57 +02:00
|
|
|
elements.click("previous");
|
|
|
|
break;
|
2019-10-30 22:49:04 +01:00
|
|
|
case globalEvents.play:
|
|
|
|
elements.click("play");
|
|
|
|
break;
|
|
|
|
case globalEvents.pause:
|
|
|
|
elements.click("pause");
|
|
|
|
break;
|
2019-10-22 21:25:57 +02:00
|
|
|
}
|
|
|
|
});
|
2019-10-20 22:47:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-30 23:42:08 +01:00
|
|
|
/**
|
|
|
|
* Update the current status of tidal (e.g playing or paused)
|
|
|
|
*/
|
|
|
|
function updateStatus() {
|
|
|
|
const play = elements.get("play");
|
|
|
|
let status = statuses.paused;
|
|
|
|
// if play button is NOT visible tidal is playing
|
|
|
|
if (!play) {
|
|
|
|
status = statuses.playing;
|
|
|
|
}
|
|
|
|
ipcRenderer.send("update-status", status);
|
|
|
|
}
|
|
|
|
|
2019-10-20 22:47:01 +02:00
|
|
|
/**
|
2019-10-22 21:25:57 +02:00
|
|
|
* Watch for song changes and update title + notify
|
2019-10-20 22:47:01 +02:00
|
|
|
*/
|
|
|
|
setInterval(function() {
|
|
|
|
const title = elements.getText("title");
|
|
|
|
const artists = elements.getText("artists");
|
|
|
|
const songDashArtistTitle = `${title} - ${artists}`;
|
2019-10-22 21:25:57 +02:00
|
|
|
|
2019-10-30 23:42:08 +01:00
|
|
|
updateStatus();
|
|
|
|
|
2019-10-20 22:47:01 +02:00
|
|
|
if (getTitle() !== songDashArtistTitle) {
|
|
|
|
setTitle(songDashArtistTitle);
|
2019-10-22 21:25:57 +02:00
|
|
|
|
|
|
|
const image = elements.getSongIcon();
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
title,
|
|
|
|
message: artists,
|
|
|
|
};
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
if (image.startsWith("http")) {
|
|
|
|
downloadFile(image, notificationPath).then(
|
|
|
|
() => {
|
|
|
|
options.icon = notificationPath;
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
}).then(
|
|
|
|
() => {
|
2019-10-30 22:49:04 +01:00
|
|
|
ipcRenderer.send("update-info", options);
|
|
|
|
settings.notifications && notifier.notify(options);
|
2019-10-22 21:25:57 +02:00
|
|
|
},
|
|
|
|
() => {}
|
|
|
|
);
|
2019-10-20 22:47:01 +02:00
|
|
|
}
|
2019-10-22 21:25:57 +02:00
|
|
|
}, 200);
|
2019-10-20 22:47:01 +02:00
|
|
|
|
|
|
|
addHotKeys();
|
|
|
|
addIPCEventListeners();
|