Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Youtube Music): timestamps #8324

Merged
merged 8 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion websites/Y/YouTube Music/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"matches": [
"*://music.youtube.com/*"
],
"version": "3.0.18",
"version": "3.0.19",
"logo": "https://cdn.rcd.gg/PreMiD/websites/Y/YouTube%20Music/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/Y/YouTube%20Music/assets/thumbnail.png",
"color": "#E40813",
Expand Down Expand Up @@ -79,6 +79,12 @@
"if": {
"privacy": false
}
},
{
"id": "usetimeleft",
"title": "Use Time Left",
"icon": "fad fa-stopwatch",
"value": true
}
]
}
25 changes: 15 additions & 10 deletions websites/Y/YouTube Music/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ presence.on("UpdateData", async () => {
hidePaused,
showBrowsing,
privacyMode,
usetimeleft,
ION606 marked this conversation as resolved.
Show resolved Hide resolved
] = await Promise.all([
presence.getSetting<boolean>("buttons"),
presence.getSetting<boolean>("timestamps"),
presence.getSetting<boolean>("cover"),
presence.getSetting<boolean>("hidePaused"),
presence.getSetting<boolean>("browsing"),
presence.getSetting<boolean>("privacy"),
presence.getSetting<boolean>("usetimeleft"),
]),
{ mediaSession } = navigator,
watchID =
Expand All @@ -41,9 +43,13 @@ presence.on("UpdateData", async () => {
if (videoElement) {
if (!videoListenerAttached) {
//* If video scrobbled, update timestamps
videoElement.addEventListener("seeked", updateSongTimestamps);
videoElement.addEventListener("seeked", () =>
updateSongTimestamps(usetimeleft)
);
//* If video resumes playing, update timestamps
videoElement.addEventListener("play", updateSongTimestamps);
videoElement.addEventListener("play", () =>
updateSongTimestamps(usetimeleft)
);

videoListenerAttached = true;
}
Expand All @@ -60,7 +66,7 @@ presence.on("UpdateData", async () => {

if (["playing", "paused"].includes(mediaSession.playbackState)) {
if (privacyMode) {
presenceData.type = ActivityType.Listening;
presenceData.type = ActivityType.Playing;
ION606 marked this conversation as resolved.
Show resolved Hide resolved
return presence.setActivity({
...(mediaSession.playbackState === "playing" && {
largeImageKey:
Expand All @@ -80,7 +86,7 @@ presence.on("UpdateData", async () => {
.querySelector<HTMLSpanElement>("#left-controls > span")
.textContent.trim()
) {
updateSongTimestamps();
updateSongTimestamps(usetimeleft);

if (mediaTimestamps[0] === mediaTimestamps[1]) return;

Expand Down Expand Up @@ -145,7 +151,7 @@ presence.on("UpdateData", async () => {
};
} else if (showBrowsing) {
if (privacyMode) {
presenceData.type = ActivityType.Listening;
presenceData.type = ActivityType.Playing;
ION606 marked this conversation as resolved.
Show resolved Hide resolved
return presence.setActivity({
largeImageKey:
"https://cdn.rcd.gg/PreMiD/websites/Y/YouTube%20Music/assets/logo.png",
Expand Down Expand Up @@ -260,20 +266,19 @@ presence.on("UpdateData", async () => {

if (!showBrowsing) return presence.clearActivity();

//* For some bizarre reason the timestamps are NaN eventho they are never actually set in testing, this spread is a workaround
presenceData.type = ActivityType.Listening;
presenceData.type = ActivityType.Playing;
ION606 marked this conversation as resolved.
Show resolved Hide resolved
presence.setActivity(presenceData);
});

function updateSongTimestamps() {
function updateSongTimestamps(usetimeleft: boolean) {
const element = document
.querySelector<HTMLSpanElement>("#left-controls > span")
.textContent.trim()
.split(" / "),
[currTimes, totalTimes] = element;

mediaTimestamps = presence.getTimestamps(
presence.timestampFromFormat(currTimes),
presence.timestampFromFormat(totalTimes)
usetimeleft ? presence.timestampFromFormat(currTimes) : Date.now(),
usetimeleft ? presence.timestampFromFormat(totalTimes) : null
);
}
Loading