-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (60 loc) · 2.03 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Modules to control application life and create native browser window
const { app } = require("electron");
const path = require("path");
const { menubar } = require("menubar");
const mb = menubar({
browserWindow: { width: 450, height: 660 },
preloadWindow: true,
icon: path.join(__dirname, "/MenuIcon.png"),
webPreferences: {
partition: "persist:xmmenuplayer",
},
});
mb.app.commandLine.appendSwitch(
"disable-backgrounding-occluded-windows",
"true"
);
mb.on("ready", () => {
console.log("app is ready");
win = mb.window;
// win.openDevTools();
// First URL
win.loadURL("https://player.siriusxm.com/now-playing");
// mb.on('after-create-window', () => {
// Once dom-ready
win.webContents.once("dom-ready", () => {
setInterval(() => {
// Artist Name
const artistName = win.webContents
.executeJavaScript(
`document.querySelector('.sxm-player-controls .artist-name').innerText`
)
.then((result) => result.trim());
// Track Name
const trackName = win.webContents
.executeJavaScript(
`document.querySelector('.sxm-player-controls .track-name').innerText`
)
.then((result) => result.trim());
// Player State
const playerState = win.webContents
.executeJavaScript(
`document.querySelector('.sxm-player-controls .play-pause-btn').getAttribute('title')`
)
.then((result) => result.trim())
.then((state) => (state == "Play" ? "⏸︎" : ""));
Promise.all([artistName, trackName, playerState])
.then(([artistName, trackName, playerState]) => {
// Set Menubar Title
mb.tray.setTitle(`${playerState} ${artistName} - ${trackName}`);
})
.catch(() => {});
}, 1000);
});
});
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});