forked from PreMiD/Presences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresence.ts
116 lines (104 loc) · 3.14 KB
/
presence.ts
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const presence = new Presence({
clientId: "921861694190407730",
});
async function getStrings() {
return presence.getStrings(
{
play: "general.playing",
pause: "general.paused",
viewAlbum: "general.buttonViewAlbum",
viewPlaylist: "general.buttonViewPlaylist",
},
await presence.getSetting<string>("lang").catch(() => "en")
);
}
let strings: Awaited<ReturnType<typeof getStrings>> = null,
oldLang: string = null;
presence.on("UpdateData", async () => {
if (!document.querySelector("#root"))
return presence.setActivity({ largeImageKey: "logo" });
const [newLang, timestamps, cover] = await Promise.all([
presence.getSetting<string>("lang").catch(() => "en"),
presence.getSetting<boolean>("timestamps"),
presence.getSetting<boolean>("cover"),
]);
if (oldLang !== newLang) {
oldLang = newLang;
strings = await getStrings();
}
const presenceData: PresenceData = {
largeImageKey: cover
? document
.querySelector<HTMLImageElement>(
'div[class="player__track-cover"] img'
)
.src.replaceAll("230", "600")
: "logo",
},
songTitle = document.querySelector<HTMLAnchorElement>(
'a[class="player__track-name"]'
),
fromPlaylist = !!document.querySelectorAll(
'div[class="player__track-album"] a'
)[2],
currentTimeSec =
presence.timestampFromFormat(
document.querySelector<HTMLElement>(
'span[class="player__track-time-text"]'
).textContent
) * 1000,
endTimeSec =
presence.timestampFromFormat(
document.querySelectorAll<HTMLElement>(
'span[class="player__track-time-text"]'
)[1].textContent
) * 1000,
paused = !!document.querySelector(
'span[class="player__action-play pct pct-player-play "] '
),
elm = document.querySelector(".player__action-repeat.pct"),
obj = {
repeatType: elm.classList.contains("pct-repeat-once")
? "loopTrack"
: elm.classList.contains("player__action-repeat--active")
? "loopQueue"
: "deactivated",
songPlaylist: document.querySelectorAll<HTMLAnchorElement>(
'div[class="player__track-album"] a'
)[2],
};
let playliststring = "";
if (fromPlaylist) playliststring = ` | From: ${obj.songPlaylist.textContent}`;
presenceData.details = songTitle.textContent;
presenceData.state =
document.querySelector('div[class="player__track-album"] > a').textContent +
playliststring;
if (currentTimeSec > 0 || !paused) {
presenceData.endTimestamp = Date.now() + (endTimeSec - currentTimeSec);
presenceData.smallImageKey = paused ? "pause" : "play";
presenceData.smallImageText = paused ? strings.pause : strings.play;
}
if (paused || !timestamps) {
delete presenceData.startTimestamp;
delete presenceData.endTimestamp;
}
if (obj.repeatType !== "deactivated" && !paused) {
presenceData.smallImageKey =
obj.repeatType === "loopQueue" ? "repeat" : "repeat-one";
presenceData.smallImageText =
obj.repeatType === "loopQueue" ? "Repeat" : "Repeat Once";
}
presenceData.buttons = [
{
label: strings.viewAlbum,
url: songTitle.href,
},
];
if (fromPlaylist) {
presenceData.buttons.push({
label: strings.viewPlaylist,
url: obj.songPlaylist.href,
});
}
presence.setActivity(presenceData);
});