-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Yandex Music): rewrite presence (#9015)
* feat(Yandex Music): rewrite presence * chore: change username * feat(Yandex Music): hide when paused * refactor: remove un-needed function
- Loading branch information
1 parent
47f4323
commit 137eddc
Showing
2 changed files
with
46 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,38 @@ | ||
const presence = new Presence({ | ||
clientId: "745261937092198532", | ||
}), | ||
strings = presence.getStrings({ | ||
playing: "general.playing", | ||
pause: "general.paused", | ||
}); | ||
|
||
let presenceData: PresenceData; | ||
|
||
function getMillisecondsFromString(timeString: string): number { | ||
const parsedText = timeString.split(":"); | ||
return (Number(parsedText[0]) * 60 + Number(parsedText[1])) * 1000; | ||
} | ||
|
||
function isPodcast(): boolean { | ||
return !!document.querySelectorAll(".track__podcast")[0]; | ||
} | ||
|
||
setInterval(async () => { | ||
const startedAt = | ||
Date.now() - | ||
getMillisecondsFromString( | ||
document.querySelectorAll<HTMLElement>(".progress__left")[0].textContent | ||
), | ||
playing = | ||
document.querySelectorAll(".player-controls__btn_pause").length === 2; | ||
|
||
let artists; | ||
if (isPodcast()) { | ||
artists = | ||
document.querySelectorAll<HTMLElement>(".track__podcast")[0].textContent; | ||
} else { | ||
artists = | ||
document.querySelectorAll<HTMLElement>(".track__artists")[0].textContent; | ||
} | ||
|
||
const coverImageSizes = document | ||
.querySelector(".track") | ||
.querySelector<HTMLImageElement>(".entity-cover__image") | ||
.srcset // get all images of all sizes | ||
.split(", "), | ||
coverImage = coverImageSizes | ||
.at(-1) // get the last one (the best one) | ||
.split(" ") | ||
.at(0), | ||
largeImageKey = coverImage | ||
? `https:${coverImage}` | ||
: "https://cdn.rcd.gg/PreMiD/websites/Y/Yandex%20Music/assets/logo.png"; | ||
|
||
presenceData = { | ||
largeImageKey, | ||
smallImageKey: playing ? Assets.Play : Assets.Pause, | ||
smallImageText: playing ? (await strings).playing : (await strings).pause, | ||
details: | ||
document.querySelectorAll<HTMLElement>(".track__title")[0].textContent, | ||
state: artists, | ||
startTimestamp: startedAt, | ||
endTimestamp: | ||
startedAt + | ||
getMillisecondsFromString( | ||
document.querySelectorAll<HTMLElement>(".progress__right")[0] | ||
.textContent | ||
), | ||
}; | ||
|
||
if (!playing) { | ||
delete presenceData.startTimestamp; | ||
delete presenceData.endTimestamp; | ||
} | ||
}, 1000); | ||
clientId: "745261937092198532", | ||
}); | ||
|
||
presence.on("UpdateData", () => { | ||
if (document.querySelectorAll(".track__title").length !== 0) | ||
presence.setActivity(presenceData); | ||
else presence.setActivity(); | ||
presence.on("UpdateData", async () => { | ||
if ( | ||
document.querySelectorAll(".player-controls__btn_pause").length !== 2 || | ||
!navigator.mediaSession.metadata | ||
) | ||
return presence.clearActivity(); | ||
|
||
const largeImageKey = navigator.mediaSession.metadata.artwork | ||
? navigator.mediaSession.metadata.artwork.at(-1).src | ||
: "https://cdn.rcd.gg/PreMiD/websites/Y/Yandex%20Music/assets/logo.png", | ||
timePassed = document.querySelector(".progress__left").textContent, | ||
[currentTime, duration] = [ | ||
presence.timestampFromFormat(timePassed), | ||
presence.timestampFromFormat( | ||
document.querySelector(".progress__right").textContent | ||
) + presence.timestampFromFormat(timePassed), | ||
], | ||
[startTimestamp, endTimestamp] = presence.getTimestamps( | ||
currentTime, | ||
duration | ||
), | ||
presenceData = { | ||
type: ActivityType.Listening, | ||
largeImageKey, | ||
details: navigator.mediaSession.metadata.title, | ||
state: | ||
navigator.mediaSession.metadata.artist || | ||
navigator.mediaSession.metadata.album, | ||
startTimestamp, | ||
endTimestamp, | ||
}; | ||
|
||
presence.setActivity(presenceData); | ||
}); |