presence.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const presence = new Presence({
  2. clientId: "988699263775154176",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000),
  5. deckList = [
  6. "/novel/",
  7. "/vocabulary-list/",
  8. "/textbook/",
  9. "/anime/",
  10. "/youtube-video/",
  11. "/live-action/",
  12. "/video-game/",
  13. "/web-novel/",
  14. "/visual-novel/",
  15. "/audio/",
  16. ];
  17. presence.on("UpdateData", async () => {
  18. const presenceData: PresenceData = {
  19. largeImageKey: "https://cdn.rcd.gg/PreMiD/websites/J/jpdb/assets/logo.png",
  20. startTimestamp: browsingTimestamp,
  21. };
  22. if (document.location.pathname.includes("/learn")) {
  23. presenceData.details = "Viewing learn page";
  24. const dueCountElem =
  25. document.querySelector('a[href="/learn"]').firstElementChild;
  26. if (dueCountElem.getAttribute("style") === "color: green;")
  27. presenceData.state = `New: ${dueCountElem.textContent} items`;
  28. else presenceData.state = `Due: ${dueCountElem.textContent} items`;
  29. } else if (document.location.pathname.includes("/review")) {
  30. presenceData.details = "Reviewing cards";
  31. const dueCountElem =
  32. document.querySelector('a[href="/learn"]').firstElementChild;
  33. if (dueCountElem.getAttribute("style") === "color: green;")
  34. presenceData.state = `New: ${dueCountElem.textContent} items`;
  35. else presenceData.state = `Due : ${dueCountElem.textContent} items`;
  36. } else if (document.location.pathname.includes("/search")) {
  37. if (document.querySelector("div.results.search")) {
  38. presenceData.details = "Searching:";
  39. presenceData.state = encodeURI(
  40. new URLSearchParams(document.location.search).get("q")
  41. );
  42. } else if (document.querySelector("div.result.kanji")) {
  43. presenceData.details = "Viewing a kanji:";
  44. presenceData.state = `${new URLSearchParams(document.location.search).get(
  45. "q"
  46. )} - ${decodeURI(
  47. document.querySelector(
  48. "div.result.kanji > div.vbox.gap > div.hbox > div.vbox.gap > div > div"
  49. ).textContent
  50. )}`;
  51. } else if (document.querySelector("div.result.vocabulary")) {
  52. presenceData.details = "Viewing a word:";
  53. presenceData.state = decodeURI(
  54. new URLSearchParams(document.location.search).get("q")
  55. );
  56. }
  57. } else if (document.location.pathname.includes("/prebuilt_decks")) {
  58. presenceData.details = "Finding pre-built deck";
  59. presenceData.state =
  60. new URLSearchParams(document.location.search).get("q") || "";
  61. } else if (document.location.pathname.includes("/vocabulary/")) {
  62. presenceData.details = "Viewing a word:";
  63. presenceData.state = decodeURI(document.location.pathname.split("/")[3]);
  64. } else if (document.location.pathname.includes("/kanji/")) {
  65. presenceData.details = "Viewing a kanji:";
  66. presenceData.state = decodeURI(document.location.pathname.split("/")[2]);
  67. } else if (document.location.pathname.includes("/conjugate")) {
  68. presenceData.details = "Viewing conjugations:";
  69. presenceData.state = decodeURI(document.location.pathname.split("/")[3]);
  70. } else if (document.location.pathname.includes("/conjugation")) {
  71. presenceData.details = "Reading about conjugation:";
  72. presenceData.state = decodeURI(document.location.pathname.split("/")[3]);
  73. } else if (deckList.some(deck => document.location.pathname.includes(deck))) {
  74. presenceData.details = "Viewing pre-built deck:";
  75. if (document.location.pathname.includes("/vocabulary-list")) {
  76. presenceData.state = document
  77. .querySelector("h4")
  78. .textContent.substring(17);
  79. } else presenceData.state = document.querySelector("h3").textContent;
  80. } else if (document.location.pathname.includes("/anki-import/deck")) {
  81. presenceData.details = "Viewing an imported deck:";
  82. presenceData.state = document
  83. .querySelector("div.container.bugfix")
  84. .firstElementChild.textContent.substring(15);
  85. } else if (document.location.pathname.includes("/deck")) {
  86. presenceData.details = "Viewing a deck:";
  87. presenceData.state = document.querySelector(
  88. "div.container.bugfix > div:nth-child(2)"
  89. ).textContent;
  90. } else if (document.location.pathname.includes("/stats")) {
  91. presenceData.details = "Viewing stats";
  92. presenceData.state = document.querySelector(
  93. "div.container.bugfix > p"
  94. ).textContent;
  95. } else if (document.location.pathname.includes("/anki-import/overview")) {
  96. presenceData.details = "Importing from Anki";
  97. presenceData.state = `Total: ${
  98. document
  99. .querySelector("div.container.bugfix > table > tbody > tr:nth-child(2)")
  100. .textContent.substring(5)
  101. .split("/")[0]
  102. } items`;
  103. } else if (
  104. document.location.pathname.includes("/add_to_deck_from_text_confirm")
  105. ) {
  106. presenceData.details = "Adding vocabulary from text";
  107. presenceData.state = `Total: ${document
  108. .querySelector("div.container.bugfix > p")
  109. .textContent.substring(76, 78)} items`;
  110. } else if (document.location.pathname.includes("/add_to_deck_from_text"))
  111. presenceData.details = "Adding vocabulary from text";
  112. else if (
  113. document.location.pathname.includes("/add-to-deck-from-satori-reader")
  114. )
  115. presenceData.details = "Importing from Satori Reader";
  116. else if (
  117. document.location.pathname.includes("/add-to-deck-from-shirabe-jisho")
  118. )
  119. presenceData.details = "Importing from Shirabe Jisho";
  120. else if (document.location.pathname.includes("/anki-import"))
  121. presenceData.details = "Importing from Anki";
  122. else if (document.location.pathname.includes("/add-empty-deck"))
  123. presenceData.details = "Creating a deck";
  124. else if (document.location.pathname.includes("/new_deck_from_text"))
  125. presenceData.details = "Creating a deck from text";
  126. else if (document.location.pathname.includes("/new-deck-from-top-vocabulary"))
  127. presenceData.details = "Creating a deck from top vocabulary";
  128. else if (document.location.pathname === "/")
  129. presenceData.details = "Viewing home page";
  130. else if (document.location.pathname.includes("/quiz"))
  131. presenceData.details = "Taking quizzes";
  132. else if (document.location.pathname.includes("/leaderboard"))
  133. presenceData.details = "Viewing leaderboard";
  134. else if (document.location.pathname.includes("/settings"))
  135. presenceData.details = "Viewing settings page";
  136. else if (document.location.pathname.includes("/contact-us"))
  137. presenceData.details = "Viewing contact page";
  138. else if (document.location.pathname.includes("/labs"))
  139. presenceData.details = "Viewing labs page";
  140. else if (document.location.pathname.includes("/anime-difficulty-list"))
  141. presenceData.details = "Viewing anime difficulty list";
  142. else if (document.location.pathname.includes("/live-action-difficulty-list"))
  143. presenceData.details = "Viewing live action difficulty list";
  144. else if (document.location.pathname.includes("/visual-novel-difficulty-list"))
  145. presenceData.details = "Viewing visual novel difficulty list";
  146. else if (document.location.pathname.includes("/novel-difficulty-list"))
  147. presenceData.details = "Viewing novel difficulty list";
  148. else if (document.location.pathname.includes("/web-novel-difficulty-list"))
  149. presenceData.details = "Viewing web novel difficulty list";
  150. else if (document.location.pathname.includes("/kanji-by-frequency"))
  151. presenceData.details = "Viewing kanji by frequency list";
  152. else if (document.location.pathname.includes("/kanken-kanji"))
  153. presenceData.details = "Viewing Kanken kanji list";
  154. else if (document.location.pathname.includes("/analyze-text"))
  155. presenceData.details = "Analyzing Japanese text";
  156. else if (document.location.pathname.includes("/about"))
  157. presenceData.details = "Reading the about page";
  158. else if (document.location.pathname.includes("/faq"))
  159. presenceData.details = "Reading FAQ";
  160. else if (document.location.pathname.includes("/privacy-policy"))
  161. presenceData.details = "Reading privacy policy";
  162. else if (document.location.pathname.includes("/terms-of-use"))
  163. presenceData.details = "Reading terms of use";
  164. else if (document.location.pathname.includes("/changelog"))
  165. presenceData.details = "Reading changelog";
  166. else if (document.location.pathname.includes("/login"))
  167. presenceData.details = "Logging in";
  168. if (!presenceData.details) presence.setActivity();
  169. else presence.setActivity(presenceData);
  170. });