forked from PreMiD/Presences
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Tubi): multilang, buttons, covers, privacy & refactor (PreMiD#6692)
Co-authored-by: Rhys Rakoff <64903135+EncryptedDev@users.noreply.github.com>
- Loading branch information
1 parent
f334747
commit 657c90d
Showing
2 changed files
with
122 additions
and
28 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,41 +1,105 @@ | ||
const presence = new Presence({ | ||
clientId: "937290941285429311", | ||
}), | ||
strings = presence.getStrings({ | ||
play: "general.playing", | ||
pause: "general.paused", | ||
}); | ||
browingTimestamp = Math.floor(Date.now() / 1000); | ||
async function getStrings() { | ||
return presence.getStrings( | ||
{ | ||
play: "general.playing", | ||
paused: "general.paused", | ||
browse: "general.browsing", | ||
live: "general.live", | ||
buttonWatchVideo: "general.buttonWatchVideo", | ||
buttonWatchLive: "general.buttonWatchStream", | ||
viewCategory: "general.viewCategory", | ||
search: "general.searchFor", | ||
}, | ||
await presence.getSetting<string>("lang").catch(() => "en") | ||
); | ||
} | ||
enum Assets { | ||
Logo = "https://i.imgur.com/PfRmgZm.png", | ||
Live = "https://i.imgur.com/gYNKPnn.png", | ||
Play = "https://i.imgur.com/OLaz6JN.png", | ||
Paused = "https://i.imgur.com/4iyMINk.png", | ||
SearchImage = "https://i.imgur.com/oGQtnIY.png", | ||
} | ||
let strings: Awaited<ReturnType<typeof getStrings>>, | ||
oldLang: string = null; | ||
|
||
presence.on("UpdateData", async () => { | ||
const presenceData: PresenceData = { | ||
largeImageKey: "tubi-logo", | ||
largeImageKey: Assets.Logo, | ||
}, | ||
video: HTMLVideoElement = document.querySelector( | ||
"video#videoPlayerComponent" | ||
); | ||
if (video && !isNaN(video.duration)) { | ||
const [startTimestamp, endTimestamp] = presence.getTimestamps( | ||
Math.floor(video.currentTime), | ||
Math.floor(video.duration) | ||
); | ||
presenceData.details = document | ||
.querySelector('meta[property="og:title"]') | ||
.getAttribute("content"); | ||
presenceData.smallImageKey = video.paused ? "pause" : "play"; | ||
presenceData.smallImageText = video.paused | ||
? (await strings).pause | ||
: (await strings).play; | ||
presenceData.startTimestamp = startTimestamp; | ||
presenceData.endTimestamp = endTimestamp; | ||
video = document.querySelector<HTMLVideoElement>("video"), | ||
search = document.querySelector<HTMLInputElement>('[type="search"]'), | ||
{ href, pathname } = document.location, | ||
[newLang, privacy, buttons, covers] = await Promise.all([ | ||
presence.getSetting<string>("lang").catch(() => "en"), | ||
presence.getSetting<boolean>("privacy"), | ||
presence.getSetting<boolean>("buttons"), | ||
presence.getSetting<boolean>("covers"), | ||
]); | ||
if (oldLang !== newLang || !strings) { | ||
oldLang = newLang; | ||
strings = await getStrings(); | ||
} | ||
|
||
if (privacy) { | ||
presenceData.details = strings.browse; | ||
return; | ||
} | ||
if (search?.value) { | ||
presenceData.details = strings.search; | ||
presenceData.state = search.value; | ||
presenceData.smallImageKey = Assets.SearchImage; | ||
} else if (pathname.includes("/category/")) { | ||
presenceData.details = strings.viewCategory; | ||
presenceData.state = document.querySelector<HTMLMetaElement>( | ||
'[property="og:title"]' | ||
).content; | ||
} else if (video && !isNaN(video.duration)) { | ||
presenceData.largeImageKey = document.querySelector<HTMLMetaElement>( | ||
'meta[property="og:image"]' | ||
).content; | ||
presenceData.details = document.querySelector<HTMLMetaElement>( | ||
'meta[property="og:title"]' | ||
).content; | ||
if (!pathname.includes("live")) { | ||
presenceData.smallImageKey = video.paused ? Assets.Paused : Assets.Play; | ||
presenceData.smallImageText = video.paused | ||
? strings.paused | ||
: strings.play; | ||
presenceData.endTimestamp = presence.getTimestamps( | ||
Math.floor(video.currentTime), | ||
Math.floor(video.duration) | ||
)[1]; | ||
presenceData.buttons = [ | ||
{ | ||
label: strings.buttonWatchVideo, | ||
url: href, | ||
}, | ||
]; | ||
} else { | ||
presenceData.buttons = [ | ||
{ | ||
label: strings.buttonWatchLive, | ||
url: href, | ||
}, | ||
]; | ||
presenceData.smallImageText = strings.live; | ||
presenceData.smallImageKey = Assets.Live; | ||
} | ||
presenceData.startTimestamp = browingTimestamp; | ||
|
||
if (video.paused) { | ||
delete presenceData.startTimestamp; | ||
delete presenceData.endTimestamp; | ||
} | ||
} else presenceData.details = strings.browse; | ||
|
||
if (presenceData.details) presence.setActivity(presenceData, !video.paused); | ||
} else { | ||
presenceData.details = "Browsing..."; | ||
presence.setActivity(presenceData); | ||
} | ||
if (!buttons) delete presenceData.buttons; | ||
if (!covers) presenceData.largeImageKey = Assets.Logo; | ||
if (presenceData.details) presence.setActivity(presenceData); | ||
else presence.setActivity(); | ||
}); |