presence.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. type PageAction = {
  2. id: string;
  3. path: string;
  4. text: string;
  5. icon?: string;
  6. };
  7. type VideoContext = {
  8. elapsed: number;
  9. duration: number;
  10. ended: boolean;
  11. paused: boolean;
  12. };
  13. const presence = new Presence({
  14. clientId: "707389880505860156",
  15. }),
  16. strings = presence.getStrings({
  17. playing: "general.playing",
  18. paused: "general.paused",
  19. browsing: "general.browsing",
  20. episode: "general.viewEpisode",
  21. });
  22. let video: VideoContext = null,
  23. lastVideoOption = 1;
  24. const enum Assets {
  25. Season = "https://cdn.rcd.gg/PreMiD/websites/M/Monos%20Chinos/assets/0.png",
  26. Directory = "https://cdn.rcd.gg/PreMiD/websites/M/Monos%20Chinos/assets/1.png",
  27. Logo = "https://cdn.rcd.gg/PreMiD/websites/M/Monos%20Chinos/assets/logo.png",
  28. }
  29. presence.on("iFrameData", async (context: VideoContext) => {
  30. video = context;
  31. });
  32. presence.on("UpdateData", async () => {
  33. const presenceData: PresenceData = {
  34. largeImageKey: Assets.Logo,
  35. },
  36. browsingData: PresenceData = {
  37. largeImageKey: Assets.Logo,
  38. details: (await strings).browsing,
  39. smallImageKey: Assets.Viewing,
  40. smallImageText: (await strings).browsing,
  41. },
  42. actions: PageAction[] = [
  43. {
  44. id: "episode",
  45. path: "/ver/",
  46. text: (await strings).playing,
  47. },
  48. {
  49. id: "seasonList",
  50. path: "/emision",
  51. text: "viendo lista de emisión",
  52. icon: Assets.Season,
  53. },
  54. {
  55. id: "directory",
  56. path: "/animes",
  57. text: "viendo el directorio",
  58. icon: Assets.Directory,
  59. },
  60. {
  61. id: "seasonCalendar",
  62. path: "/calendario",
  63. text: "viendo el calendario",
  64. icon: Assets.Season,
  65. },
  66. {
  67. id: "directoryAnime",
  68. path: "/anime/",
  69. text: "viendo lista de episodios",
  70. icon: Assets.Directory,
  71. },
  72. {
  73. id: "search",
  74. path: "/buscar",
  75. text: "buscando animes:",
  76. icon: Assets.Search,
  77. },
  78. {
  79. id: "profile",
  80. path: "/mi-perfil",
  81. text: "Viendo perfil",
  82. },
  83. ];
  84. let action: PageAction = null;
  85. for (const [i, info] of actions.entries()) {
  86. if (document.location.pathname.startsWith(info.path)) {
  87. action = actions[i];
  88. break;
  89. }
  90. }
  91. if (action === null) Object.assign(presenceData, browsingData);
  92. else if (action.id === "episode") {
  93. const detailsMatch = document
  94. .querySelector(".heromain_h1")
  95. .textContent.replace(/- /gm, "")
  96. .match(/^([^\d]+)(\d+)/);
  97. if (!detailsMatch) return presence.setActivity(browsingData);
  98. const [title, episode] = detailsMatch.slice(1);
  99. Object.assign(presenceData, {
  100. details: title,
  101. state: `${(await strings).episode} ${episode}`,
  102. smallImageKey: Assets.Viewing,
  103. smallImageText: "viendo el capitulo",
  104. });
  105. const currentOptionElement = document.querySelector(
  106. ".TPlayerNv > .Button.Current"
  107. ),
  108. currentOption = currentOptionElement
  109. ? parseInt(
  110. currentOptionElement
  111. .getAttribute("data-tplayernv")
  112. .match(/Opt(\d+)/i)[1]
  113. )
  114. : -1;
  115. if (currentOption !== -1 && currentOption !== lastVideoOption) {
  116. lastVideoOption = currentOption;
  117. video = null;
  118. }
  119. if (!video || (video && video.ended))
  120. return presence.setActivity(presenceData);
  121. const [startTimestamp, endTimestamp] = presence.getTimestamps(
  122. Math.floor(video.elapsed),
  123. Math.floor(video.duration)
  124. );
  125. Object.assign(presenceData, {
  126. smallImageKey: video.paused ? Assets.Pause : Assets.Play,
  127. smallImageText: (await strings)[video.paused ? "paused" : "playing"],
  128. } as PresenceData);
  129. if (!video.paused) {
  130. Object.assign(presenceData, {
  131. startTimestamp,
  132. endTimestamp,
  133. });
  134. }
  135. } else {
  136. if (
  137. document.location.pathname.includes("/anime/") &&
  138. document.querySelector("div.chapterdetails h1")
  139. ) {
  140. presenceData.state = document.querySelector(
  141. "div.chapterdetails h1"
  142. ).textContent;
  143. }
  144. if (
  145. document.location.pathname.includes("/buscar") &&
  146. document.querySelector("div.heroarea h1 span")
  147. ) {
  148. presenceData.state = document.querySelector(
  149. "div.heroarea h1 span"
  150. ).textContent;
  151. }
  152. if (
  153. document.location.pathname.includes("/mi-perfil") &&
  154. document.querySelector("div.profile div.promain h1")
  155. ) {
  156. presenceData.state = document.querySelector(
  157. "div.profile div.promain h1"
  158. ).textContent;
  159. }
  160. Object.assign(presenceData, {
  161. details: action.text,
  162. smallImageKey: action.icon,
  163. smallImageText: action.text,
  164. } as PresenceData);
  165. }
  166. presence.setActivity(presenceData);
  167. });