presence.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const presence = new Presence({
  2. clientId: "1014298173314961481",
  3. });
  4. interface Video {
  5. currentTime: number;
  6. duration: number;
  7. paused: boolean;
  8. }
  9. function timeToString(nbr: number): string {
  10. let nbrCopy = nbr,
  11. nbrString = "",
  12. quotient = 0,
  13. remainder = 0;
  14. if (nbrCopy >= 3600) {
  15. quotient = Math.floor(nbrCopy / 3600);
  16. if (isNaN(quotient)) quotient = 0;
  17. remainder = nbrCopy % 3600;
  18. if (quotient > 9) nbrString += `${quotient.toString()}:`;
  19. else nbrString += `0${quotient.toString()}:`;
  20. nbrCopy = remainder;
  21. }
  22. quotient = Math.floor(nbrCopy / 60);
  23. if (isNaN(quotient)) quotient = 0;
  24. remainder = nbrCopy % 60;
  25. if (quotient > 9) nbrString += `${quotient.toString()}:`;
  26. else nbrString += `0${quotient.toString()}:`;
  27. nbrCopy = remainder;
  28. if (isNaN(nbrCopy)) nbrCopy = 0;
  29. if (nbrCopy > 9) nbrString += nbrCopy.toString();
  30. else nbrString += `0${nbrCopy.toString()}`;
  31. return nbrString;
  32. }
  33. const websiteDomain = "https://animecat.net";
  34. let video: Video = null;
  35. const enum Assets {
  36. Logo = "https://cdn.rcd.gg/PreMiD/websites/N/Neko-sama.fr/assets/logo.png",
  37. }
  38. presence.on("iFrameData", (data: Video) => {
  39. video = data;
  40. });
  41. presence.on("UpdateData", async () => {
  42. const presenceData: PresenceData = {
  43. largeImageKey: Assets.Logo,
  44. details: "Navigue sur Neko-sama",
  45. },
  46. { pathname } = document.location,
  47. pathSplit = pathname.split("/");
  48. switch (pathSplit[1]) {
  49. case "anime":
  50. switch (pathSplit[2]) {
  51. case "episode": {
  52. const episodeImage: string = document.querySelector<HTMLMetaElement>(
  53. 'meta[property="og:image"]'
  54. ).content,
  55. animeImage: string =
  56. document.querySelector<HTMLImageElement>("a.cover img").src,
  57. defaultThumbnail = `${websiteDomain}/images/default_thumbnail.png`;
  58. presenceData.largeImageKey =
  59. episodeImage === defaultThumbnail
  60. ? animeImage === defaultThumbnail
  61. ? Assets.Logo
  62. : animeImage
  63. : episodeImage;
  64. if (video === null) {
  65. presenceData.details = `Regarde ${
  66. document.querySelector<HTMLMetaElement>(
  67. 'meta[property="og:title"]'
  68. ).content
  69. }`;
  70. presenceData.buttons = [
  71. {
  72. label: "Voir Épisode",
  73. url: document.URL,
  74. },
  75. ];
  76. break;
  77. }
  78. const { paused, currentTime, duration } = video;
  79. if (!paused) {
  80. [presenceData.startTimestamp, presenceData.endTimestamp] =
  81. presence.getTimestamps(currentTime, duration);
  82. }
  83. presenceData.state = `${timeToString(
  84. Math.floor(currentTime)
  85. )}/${timeToString(Math.floor(duration))}`;
  86. presenceData.details = `Regarde ${
  87. document.querySelector<HTMLMetaElement>('meta[property="og:title"]')
  88. .content
  89. }`;
  90. presenceData.smallImageKey = paused ? Assets.Pause : Assets.Play;
  91. presenceData.smallImageText = paused
  92. ? "En pause"
  93. : "Lecture en cours";
  94. presenceData.buttons = [
  95. {
  96. label: "Voir Épisode",
  97. url: document.URL,
  98. },
  99. ];
  100. break;
  101. }
  102. case "info": {
  103. const animeImage: string = document.querySelector<HTMLMetaElement>(
  104. 'meta[property="og:image"]'
  105. ).content;
  106. presenceData.details = "Regarde la page d'un animé :";
  107. presenceData.state =
  108. document.querySelector("h1").firstChild.textContent;
  109. presenceData.largeImageKey =
  110. animeImage === `${websiteDomain}/images/default_thumbnail.png`
  111. ? Assets.Logo
  112. : animeImage;
  113. presenceData.buttons = [
  114. {
  115. label: "Voir Animé",
  116. url: document.URL,
  117. },
  118. ];
  119. break;
  120. }
  121. default:
  122. presenceData.details = "Cherche un animé en VOSTFR";
  123. break;
  124. }
  125. break;
  126. case "anime-vf":
  127. presenceData.details = "Cherche un animé en VF";
  128. break;
  129. }
  130. presence.setActivity(presenceData);
  131. });