Skip to content

Commit a8ce87f

Browse files
committed
Added Discord rich presence and added extra properties to songinfo provider
1 parent 3a5d9bd commit a8ce87f

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@
2323
"linux": {
2424
"icon": "assets/generated/icons/png",
2525
"category": "AudioVideo",
26-
"target": ["AppImage", "snap", "freebsd", "deb", "rpm"]
26+
"target": [
27+
"AppImage",
28+
"snap",
29+
"freebsd",
30+
"deb",
31+
"rpm"
32+
]
2733
}
2834
},
2935
"scripts": {
@@ -53,6 +59,7 @@
5359
"@ffmpeg/core": "^0.8.4",
5460
"@ffmpeg/ffmpeg": "^0.9.5",
5561
"YoutubeNonStop": "git://github.com/lawfx/YoutubeNonStop.git#v0.8.0",
62+
"discord-rpc": "^3.1.4",
5663
"downloads-folder": "^3.0.1",
5764
"electron-debug": "^3.1.0",
5865
"electron-is": "^3.0.0",

plugins/discord-rpc/back.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const DiscordRPC = require('discord-rpc');
2+
const rpc = new DiscordRPC.Client({
3+
transport: 'ipc'
4+
});
5+
6+
const clientId = '790655993809338398';
7+
8+
module.exports = win => {
9+
// If the page is ready, register the callback
10+
win.on('ready-to-show', () => {
11+
// Startup the rpc client
12+
rpc.login({
13+
clientId
14+
}).catch(console.error);
15+
16+
// Register the callback
17+
global.songInfo.onNewData(songInfo => {
18+
// Song information changed, so lets update the rich presence
19+
20+
const activityInfo = {
21+
details: songInfo.title,
22+
state: songInfo.artist,
23+
largeImageKey: 'logo',
24+
largeImageText: songInfo.views + ' - ' + songInfo.likes
25+
};
26+
27+
if (songInfo.isPaused) {
28+
// Add an idle icon to show that the song is paused
29+
activityInfo.smallImageKey = 'idle';
30+
activityInfo.smallImageText = 'idle/paused';
31+
} else {
32+
// Add the start and end time of the song
33+
const songStartTime = Date.now() - (songInfo.elapsedSeconds * 1000);
34+
activityInfo.startTimestamp = songStartTime;
35+
activityInfo.endTimestamp = songStartTime + (songInfo.songDuration * 1000);
36+
}
37+
38+
rpc.setActivity(activityInfo);
39+
});
40+
});
41+
};

providers/song-info/back.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const imageSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-
1010
// This selects the song subinfo, this includes artist, views, likes
1111
const subInfoSelector = '#layout > ytmusic-player-bar > div.middle-controls.style-scope.ytmusic-player-bar > div.content-info-wrapper.style-scope.ytmusic-player-bar > span';
1212

13+
// This selects the progress bar, used for songlength and current progress
14+
const progressSelector = '#progress-bar';
15+
1316
// This is used for to control the songs
1417
const presskey = (window, key) => {
1518
window.webContents.sendInputEvent({
@@ -55,6 +58,18 @@ const getSubInfo = async win => {
5558
return subInfo;
5659
};
5760

61+
// Grab the progress using the selector
62+
const getProgress = async win => {
63+
// Get max value of the progressbar element
64+
const songDuration = await win.webContents.executeJavaScript(
65+
'document.querySelector("' + progressSelector + '").max');
66+
// Get current value of the progressbar element
67+
const elapsedSeconds = await win.webContents.executeJavaScript(
68+
'document.querySelector("' + progressSelector + '").value');
69+
70+
return {songDuration, elapsedSeconds};
71+
};
72+
5873
// Grab the native image using the src
5974
const getImage = async src => {
6075
const result = await fetch(src);
@@ -79,7 +94,9 @@ module.exports = win => {
7994
likes: '',
8095
imageSrc: '',
8196
image: null,
82-
isPaused: true
97+
isPaused: true,
98+
songDuration: 0,
99+
elapsedSeconds: 0
83100
};
84101
// The song control functions
85102
global.songControls = {
@@ -102,6 +119,10 @@ module.exports = win => {
102119
global.songInfo.title = await getTitle(win);
103120
global.songInfo.isPaused = await getPausedStatus(win);
104121

122+
const {songDuration, elapsedSeconds} = await getProgress(win);
123+
global.songInfo.songDuration = songDuration;
124+
global.songInfo.elapsedSeconds = elapsedSeconds;
125+
105126
// If title changed then we do need to update other info
106127
if (oldTitle !== global.songInfo.title) {
107128
const subInfo = await getSubInfo(win);

yarn.lock

+13
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,14 @@ dir-glob@^2.2.2:
28852885
dependencies:
28862886
path-type "^3.0.0"
28872887

2888+
discord-rpc@^3.1.4:
2889+
version "3.1.4"
2890+
resolved "https://registry.yarnpkg.com/discord-rpc/-/discord-rpc-3.1.4.tgz#6d449a682e6a0dec4f0444d5f36f9ebfabaccf91"
2891+
integrity sha512-QaBu+gHica2SzgRAmTpuJ4J8DX9+fDwAqhvaie3hcbkU9WPqewEPh21pWdd/7vTI/JNuapU7PFm2ZKg3BTkbGg==
2892+
dependencies:
2893+
node-fetch "^2.6.1"
2894+
ws "^7.3.1"
2895+
28882896
28892897
version "22.8.1"
28902898
resolved "https://registry.yarnpkg.com/dmg-builder/-/dmg-builder-22.8.1.tgz#9b3bcbbc43e5fed232525d61a5567ea4b66085c3"
@@ -8745,6 +8753,11 @@ ws@^7.2.3:
87458753
resolved "https://registry.yarnpkg.com/ws/-/ws-7.3.1.tgz#d0547bf67f7ce4f12a72dfe31262c68d7dc551c8"
87468754
integrity sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==
87478755

8756+
ws@^7.3.1:
8757+
version "7.4.2"
8758+
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.2.tgz#782100048e54eb36fe9843363ab1c68672b261dd"
8759+
integrity sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==
8760+
87488761
xdg-basedir@^4.0.0:
87498762
version "4.0.0"
87508763
resolved "https://registry.yarnpkg.com/xdg-basedir/-/xdg-basedir-4.0.0.tgz#4bc8d9984403696225ef83a1573cbbcb4e79db13"

0 commit comments

Comments
 (0)