presence.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const presence = new Presence({
  2. clientId: "468420510632509473",
  3. });
  4. presence.on("UpdateData", async () => {
  5. const { pathname, href } = document.location,
  6. presenceData: PresenceData = {
  7. largeImageKey:
  8. "https://cdn.rcd.gg/PreMiD/websites/M/MyAnimeList/assets/logo.png",
  9. };
  10. if (pathname === "/") presenceData.details = "Viewing the homepage";
  11. else if (
  12. pathname === "/anime.php" ||
  13. pathname.startsWith("/topanime") ||
  14. pathname.startsWith("/watch")
  15. )
  16. presenceData.details = "Looking for anime";
  17. else if (
  18. pathname === "/manga.php" ||
  19. pathname.startsWith("/topmanga") ||
  20. pathname.startsWith("/store")
  21. )
  22. presenceData.details = "Looking for manga";
  23. else if (pathname.startsWith("/forum")) {
  24. presenceData.details = "Viewing the forums";
  25. presenceData.state = document
  26. .querySelector("meta[property='og:title']")
  27. .getAttribute("content");
  28. } else if (pathname.startsWith("/clubs.php")) {
  29. if (document.querySelectorAll(".normal_header")[1]) {
  30. presenceData.details = "Viewing a club";
  31. presenceData.state = document.querySelector(".h1").textContent;
  32. presenceData.buttons = [{ label: "View Club", url: href }];
  33. } else if (
  34. document.querySelector(".h1-title").textContent === "Invitations"
  35. )
  36. presenceData.details = "Viewing club Invitations";
  37. else if (document.querySelector(".h1-title").textContent === "My Clubs")
  38. presenceData.details = "Viewing my clubs";
  39. else presenceData.details = "Looking for clubs";
  40. } else if (pathname.startsWith("/blog.php"))
  41. presenceData.details = "Viewing the blog";
  42. else if (pathname.startsWith("/users.php"))
  43. presenceData.details = "Searching for users";
  44. else if (pathname.startsWith("/news"))
  45. presenceData.details = "Viewing the news";
  46. else if (pathname.startsWith("/featured")) {
  47. if (
  48. document
  49. .querySelector("meta[property='og:title']")
  50. .getAttribute("content")
  51. .includes("Featured Articles")
  52. )
  53. presenceData.details = "Viewing featured articles";
  54. else {
  55. presenceData.details = "Viewing an article";
  56. presenceData.state = document.querySelector(".title").textContent;
  57. presenceData.buttons = [{ label: "Read Article", url: href }];
  58. }
  59. } else if (pathname.startsWith("/people")) {
  60. if (document.querySelector(".h1").textContent === "People")
  61. presenceData.details = "Viewing people";
  62. else {
  63. presenceData.details = "Viewing a person";
  64. presenceData.state = document
  65. .querySelector(".title-name")
  66. .textContent.replace(/(<([^>]+)>)/gi, "");
  67. presenceData.buttons = [{ label: "View Person", url: href }];
  68. }
  69. } else if (pathname.startsWith("/character")) {
  70. if (document.querySelector(".h1").textContent === "Characters")
  71. presenceData.details = "Looking for characters";
  72. else {
  73. presenceData.details = "Viewing a character";
  74. presenceData.state = document
  75. .querySelectorAll(".normal_header")[2]
  76. .textContent.replace(/(<([^>]+)>)/gi, "");
  77. presenceData.buttons = [{ label: "View Character", url: href }];
  78. }
  79. } else if (pathname.startsWith("/profile")) {
  80. presenceData.details = "Viewing a profile";
  81. presenceData.state = pathname.split("/")[2];
  82. presenceData.buttons = [{ label: "View Profile", url: href }];
  83. } else if (pathname.startsWith("/animelist")) {
  84. presenceData.details = "Viewing an anime list";
  85. presenceData.state = pathname.split("/")[2];
  86. presenceData.buttons = [{ label: "View List", url: href }];
  87. } else if (pathname.startsWith("/mangalist")) {
  88. presenceData.details = "Viewing a manga list";
  89. presenceData.state = pathname.split("/")[2];
  90. presenceData.buttons = [{ label: "View List", url: href }];
  91. } else if (pathname.startsWith("/anime")) {
  92. // TODO: The if loop to check if the user is really on the page of an anime is currently always true for some reason which results in the presence going away when the user is for example in the anime directory
  93. if (document.querySelector(".js-anime-edit-info-button")) {
  94. const englishTitle =
  95. document.querySelector(".title-english")?.textContent;
  96. presenceData.details = "Viewing an anime";
  97. presenceData.state = `${
  98. document.querySelector(".title-name").textContent
  99. } ${englishTitle ? `| ${englishTitle}` : ""}`.trim();
  100. presenceData.buttons = [{ label: "View Anime", url: href }];
  101. } else presenceData.details = "Looking for anime";
  102. } else if (pathname.startsWith("/manga")) {
  103. // TODO: The if loop to check if the user is really on the page of an anime is currently always true for some reason which results in the presence going away when the user is for example in the anime directory
  104. if (document.querySelector(".js-manga-edit-info-button")) {
  105. const englishTitle =
  106. document.querySelector(".title-english")?.textContent;
  107. presenceData.details = "Viewing a manga";
  108. presenceData.state = `${
  109. document.querySelector(".title-name").textContent
  110. } ${englishTitle ? ` | ${englishTitle}` : ""}`.trim();
  111. presenceData.buttons = [{ label: "View Mange", url: href }];
  112. } else presenceData.details = "Looking for manga";
  113. }
  114. if (presenceData.details) presence.setActivity(presenceData);
  115. else presence.setActivity();
  116. });