123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import {
- clearMetadata,
- fetchMetadata,
- metadata,
- } from "./functions/fetchMetadata";
- import {
- clearLiveMetadata,
- fetchLiveMetadata,
- liveMetadata,
- } from "./functions/fetchLiveMetadata";
- const presence = new Presence({
- clientId: "1325519017527476316",
- }),
- getStrings = async () => {
- return presence.getStrings(
- {
- play: "general.playing",
- pause: "general.paused",
- browse: "general.browsing",
- watchingMovie: "general.watchingMovie",
- watchingSeries: "general.watchingSeries",
- watchingLive: "general.watchingLive",
- viewSeries: "general.buttonViewSeries",
- viewMovies: "general.buttonViewMovie",
- watchEpisode: "general.buttonViewEpisode",
- watchMovie: "general.buttonWatchMovie",
- watchStream: "general.buttonWatchStream",
- seriesDisplayFull: "u-next.seriesDisplay.full",
- seriesDisplayShort: "u-next.seriesDisplay.short",
- movieDisplay: "u-next.movieDisplay",
- liveDisplay: "u-next.liveDisplay",
- },
- await presence.getSetting<string>("lang").catch(() => "en")
- );
- };
- let oldLang: string = null,
- strings: Awaited<ReturnType<typeof getStrings>>;
- presence.on("UpdateData", async () => {
- const [
- lang,
- usePresenceName,
- showTimestamp,
- showBrowsingStatus,
- showCover,
- showSeries,
- showMovies,
- showSmallImages,
- privacyMode,
- ] = await Promise.all([
- presence.getSetting<string>("lang").catch(() => "en"),
- presence.getSetting<boolean>("usePresenceName"),
- presence.getSetting<boolean>("timestamp"),
- presence.getSetting<boolean>("showBrowsingStatus"),
- presence.getSetting<boolean>("showCover"),
- presence.getSetting<boolean>("showSeries"),
- presence.getSetting<boolean>("showMovies"),
- presence.getSetting<boolean>("showSmallImages"),
- presence.getSetting<boolean>("privacy"),
- ]);
- if (oldLang !== lang) {
- oldLang = lang;
- strings = await getStrings();
- }
- const path = document.location.href,
- //* Match /title/sid and get sid (When you load the page / reload while browsing)
- browsingMediaId =
- path.match(/\/title\/(\w+)/) ??
- //* ?td=td when normally browsing and clicking on smth
- path.match(/td=(\w+)/);
- if (browsingMediaId) {
- if (privacyMode) return presence.clearActivity();
- await fetchMetadata(browsingMediaId[1]);
- return await presence.setActivity({
- details: metadata.data.webfrontTitleStage.titleName,
- state: metadata.data.webfrontTitleStage.attractions.slice(0, 128),
- largeImageKey: !showCover
- ? "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png"
- : `https://${metadata.data.webfrontTitleStage.thumbnail.standard}`,
- ...(showSmallImages && {
- smallImageKey: Assets.Reading,
- }),
- smallImageText: strings.browse,
- buttons: [
- {
- label: metadata.data.webfrontTitleStage.keyEpisodes.current
- .existsRelatedEpisode
- ? strings.viewMovies
- : strings.viewSeries,
- url: document.location.href,
- },
- ],
- });
- }
- //* Match /play/sid/ed and get ed
- const watchingMediaId = path.match(/\/play\/(\w+)\/(\w+)/);
- if (watchingMediaId) {
- await fetchMetadata(watchingMediaId[1]);
- const video = document.querySelector("video");
- if (!video) return;
- const { paused } = video,
- [startTimestamp, endTimestamp] = presence.getTimestampsfromMedia(video);
- if (
- metadata.data.webfrontTitleStage.keyEpisodes.current
- .existsRelatedEpisode &&
- showSeries
- ) {
- if (privacyMode) {
- return await presence.setActivity({
- type: ActivityType.Watching,
- details: strings.watchingSeries,
- largeImageKey:
- "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png",
- });
- }
- const episode = metadata.data.webfrontTitleTitleEpisodes.episodes.find(
- e => !e.completeFlag
- );
- return await presence.setActivity({
- type: ActivityType.Watching,
- details: metadata.data.webfrontTitleStage.titleName,
- state: strings.seriesDisplayShort
- .replace("{0}", episode.displayNo)
- .replace("{1}", episode.episodeName),
- largeImageKey: !showCover
- ? "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png"
- : `https://${metadata.data.webfrontTitleStage.thumbnail.standard}`,
- ...(showSmallImages && {
- smallImageKey: paused ? Assets.Pause : Assets.Play,
- }),
- smallImageText: paused ? strings.pause : strings.play,
- ...(showTimestamp &&
- !paused && {
- startTimestamp,
- endTimestamp,
- }),
- ...(usePresenceName && {
- name: metadata.data.webfrontTitleStage.titleName,
- details: episode.episodeName,
- state: strings.seriesDisplayFull.replace("{0}", episode.displayNo),
- }),
- buttons: [
- {
- label: strings.watchEpisode,
- url: document.location.href.split("?")[0],
- },
- {
- label: strings.viewSeries,
- url: `https://video.unext.jp/?td=${metadata.data.webfrontTitleStage.id}`,
- },
- ],
- });
- }
- if (
- !metadata.data.webfrontTitleStage.keyEpisodes.current
- .existsRelatedEpisode &&
- showMovies
- ) {
- if (privacyMode) {
- return await presence.setActivity({
- type: ActivityType.Watching,
- details: strings.watchingMovie,
- largeImageKey:
- "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png",
- });
- }
- return await presence.setActivity({
- type: ActivityType.Watching,
- details: metadata.data.webfrontTitleStage.titleName,
- state: strings.movieDisplay
- .replace("{0}", metadata.data.webfrontTitleStage.productionYear)
- .replace(
- "{1}",
- Math.floor(
- metadata.data.webfrontTitleStage.keyEpisodes.current.duration / 60
- ).toString()
- ),
- largeImageKey: !showCover
- ? "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png"
- : `https://${metadata.data.webfrontTitleStage.thumbnail.standard}`,
- ...(showSmallImages && {
- smallImageKey: paused ? Assets.Pause : Assets.Play,
- }),
- smallImageText: paused ? strings.pause : strings.play,
- ...(showTimestamp &&
- !paused && {
- startTimestamp,
- endTimestamp,
- }),
- ...(usePresenceName && {
- name: metadata.data.webfrontTitleStage.titleName,
- }),
- buttons: [
- {
- label: strings.watchMovie,
- url: document.location.href.split("?")[0],
- },
- ],
- });
- }
- //* show Series & Movies disabled, clearactivity, nothing to show?
- return presence.clearActivity();
- }
- //* Reset because no data can be fetched
- clearMetadata();
- //* Match /livedetail/liv and get liv (When you load the page / reload while browsing)
- const browsingLiveId =
- path.match(/\/livedetail\/(\w+)/) ??
- //* ?lc=lc when normally browsing and clicking on smth
- path.match(/lc=(\w+)/);
- if (browsingLiveId) {
- if (privacyMode) return presence.clearActivity();
- await fetchLiveMetadata(browsingLiveId[1]);
- return await presence.setActivity({
- details: liveMetadata.data.webfrontGetLive.name,
- state: liveMetadata.data.webfrontGetLive.attractions.slice(0, 128),
- largeImageKey: !showCover
- ? "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png"
- : `https://${liveMetadata.data.webfrontGetLive.notices[0].thumbnail.standard}`,
- ...(showSmallImages && {
- smallImageKey: Assets.Reading,
- }),
- smallImageText: strings.browse,
- buttons: [
- {
- label: strings.watchStream,
- url: document.location.href,
- },
- ],
- });
- }
- //* Match /live/liv and get liv
- const watchingLiveId = path.match(/\/live\/(\w+)/);
- if (watchingLiveId) {
- await fetchLiveMetadata(watchingLiveId[1]);
- const video = document.querySelector("video");
- if (!video) return;
- const { paused } = video,
- [startTimestamp, endTimestamp] = presence.getTimestampsfromMedia(video);
- if (privacyMode) {
- return await presence.setActivity({
- type: ActivityType.Watching,
- details: strings.watchingLive,
- largeImageKey:
- "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png",
- });
- }
- return await presence.setActivity({
- type: ActivityType.Watching,
- details: liveMetadata.data.webfrontGetLive.name,
- state: strings.liveDisplay
- .replace(
- "{0}",
- new Date(liveMetadata.data.webfrontGetLive.deliveryStartDateTime)
- .getFullYear()
- .toString()
- )
- .replace(
- "{1}",
- Math.floor(
- (new Date(
- liveMetadata.data.webfrontGetLive.deliveryEndDateTime
- ).getTime() -
- new Date(
- liveMetadata.data.webfrontGetLive.deliveryStartDateTime
- ).getTime()) /
- 1000 /
- 60
- ).toString()
- ),
- largeImageKey: !showCover
- ? "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png"
- : `https://${liveMetadata.data.webfrontGetLive.notices[0].thumbnail.standard}`,
- ...(showSmallImages && {
- smallImageKey: paused ? Assets.Pause : Assets.Play,
- }),
- smallImageText: paused ? strings.pause : strings.play,
- ...(showTimestamp &&
- !paused && {
- startTimestamp,
- endTimestamp,
- }),
- ...(usePresenceName && {
- name: liveMetadata.data.webfrontGetLive.name,
- }),
- buttons: [
- {
- label: strings.watchStream,
- url: document.location.href.split("?")[0],
- },
- ],
- });
- }
- //* Reset because no data can be fetched
- clearLiveMetadata();
- if (showBrowsingStatus && !privacyMode) {
- return await presence.setActivity({
- details: strings.browse,
- largeImageKey:
- "https://cdn.rcd.gg/PreMiD/websites/U/U-NEXT/assets/logo.png",
- smallImageKey: Assets.Reading,
- smallImageText: strings.browse,
- });
- }
- return presence.clearActivity();
- });
|