presence.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const presence = new Presence({
  2. clientId: "1007688410875564062",
  3. });
  4. presence.on("UpdateData", async () => {
  5. let presenceData: PresenceData = {};
  6. const showButtons = await presence.getSetting("showButtons"),
  7. { hostname, pathname, origin, href, search } = document.location,
  8. searchParams = new URLSearchParams(search);
  9. if (hostname === "jojowiki.com") {
  10. presenceData = {
  11. largeImageKey:
  12. "https://cdn.rcd.gg/PreMiD/websites/J/JoJo's%20Bizarre%20Encyclopedia/assets/0.png",
  13. smallImageKey:
  14. "https://cdn.rcd.gg/PreMiD/websites/J/JoJo's%20Bizarre%20Encyclopedia/assets/1.png",
  15. smallImageText: "jojowiki.com",
  16. };
  17. presenceData.details = "Browsing JoJoWiki";
  18. if (
  19. pathname === "" ||
  20. pathname === "/" ||
  21. pathname === "/JoJo_Wiki" ||
  22. (pathname === "/index.php" && !searchParams.get("action"))
  23. )
  24. presenceData.state = "Viewing homepage";
  25. else if (pathname.toLowerCase().includes("/index.php")) {
  26. const search = searchParams.get("search"),
  27. action = searchParams.get("action");
  28. let title;
  29. if (searchParams.get("title"))
  30. title = urlToTitle(searchParams.get("title"));
  31. else title = "Homepage";
  32. if (search?.length > 0) presenceData.state = `Searching "${search}"`;
  33. else if (action === "history") {
  34. presenceData.state = `Viewing history: ${title}`;
  35. presenceData.buttons = [
  36. {
  37. label: "View Page",
  38. url: `${origin}/${titleToUrl(title)}`,
  39. },
  40. ];
  41. } else if (action === "purge") {
  42. presenceData.state = `Purging: ${title}`;
  43. presenceData.buttons = [
  44. {
  45. label: "View Page",
  46. url: `${origin}/${titleToUrl(title)}`,
  47. },
  48. ];
  49. }
  50. } else if (pathname.toLowerCase().startsWith("/user:")) {
  51. presenceData.state = `Viewing User: ${pathname.match(/\/user:(.*)/i)[1]}`;
  52. presenceData.buttons = [
  53. {
  54. label: "View User",
  55. url: href,
  56. },
  57. ];
  58. } else if (pathname.toLowerCase().startsWith("/category:")) {
  59. presenceData.state = `Viewing Category: ${urlToTitle(
  60. pathname.match(/\/category:(.*)/i)[1]
  61. )}`;
  62. presenceData.buttons = [
  63. {
  64. label: "View Category",
  65. url: href,
  66. },
  67. ];
  68. } else if (pathname.toLowerCase().startsWith("/file:")) {
  69. presenceData.state = `Viewing File: ${pathname.match(/\/file:(.*)/i)[1]}`;
  70. presenceData.buttons = [
  71. {
  72. label: "View File",
  73. url: href,
  74. },
  75. ];
  76. } else if (pathname.toLowerCase().startsWith("/special:")) {
  77. if (pathname.toLowerCase().startsWith("/special:movepage/")) {
  78. const title = urlToTitle(pathname.substring(18));
  79. presenceData.state = `Moving: ${title}`;
  80. presenceData.buttons = [
  81. {
  82. label: "View Page",
  83. url: `${origin}/${titleToUrl(title)}`,
  84. },
  85. ];
  86. } else if (pathname.toLowerCase().startsWith("/special:notifications"))
  87. presenceData.state = "Checking Notifications";
  88. else if (pathname.toLowerCase().startsWith("/special:preferences"))
  89. presenceData.state = "Viewing Settings";
  90. } else if (
  91. pathname.toLowerCase().startsWith("/user_talk:") ||
  92. pathname.toLowerCase().startsWith("/userwiki:") ||
  93. pathname.toLowerCase().startsWith("/jojo_wiki:")
  94. ) {
  95. // nothing here, just used to ignore these as long as no functionality is applied to them.
  96. } else if (
  97. pathname.toLowerCase().startsWith("/list_of_references_to_jojo")
  98. ) {
  99. presenceData.state = "Browsing JoJo References 👀";
  100. presenceData.buttons = [
  101. {
  102. label: "Join The Fun!",
  103. url: href,
  104. },
  105. ];
  106. } else if (pathname.toLowerCase().startsWith("/talk:")) {
  107. presenceData.state = `Discussing: ${document
  108. .querySelector("#firstHeading")
  109. .innerHTML.replace(/^Talk:/, "")}`;
  110. presenceData.buttons = [
  111. {
  112. label: "Join The Discussion!",
  113. url: href,
  114. },
  115. ];
  116. } else {
  117. const title = urlToTitle(location.pathname.substring(1));
  118. if (
  119. searchParams.get("veswitched")?.length ||
  120. searchParams.get("veaction") === "edit"
  121. ) {
  122. presenceData.state = `Editing: ${title}`;
  123. presenceData.buttons = [
  124. {
  125. label: "View Page",
  126. url: `${origin}/${titleToUrl(title)}`,
  127. },
  128. ];
  129. } else {
  130. presenceData.state = `Reading: ${title}`;
  131. presenceData.buttons = [
  132. {
  133. label: "View Page",
  134. url: href,
  135. },
  136. ];
  137. }
  138. }
  139. }
  140. if (hostname === "jojo-news.com") {
  141. presenceData = {
  142. largeImageKey:
  143. "https://cdn.rcd.gg/PreMiD/websites/J/JoJo's%20Bizarre%20Encyclopedia/assets/0.png",
  144. smallImageKey:
  145. "https://cdn.rcd.gg/PreMiD/websites/J/JoJo's%20Bizarre%20Encyclopedia/assets/2.png",
  146. smallImageText: "jojo-news.com",
  147. };
  148. presenceData.details = "Browsing News";
  149. if (pathname === "" || pathname === "/" || pathname === "/JoJo_Wiki")
  150. presenceData.state = "Viewing: Homepage";
  151. else if (pathname.match(/^\/\d+\/\d+\/\d+\//)) {
  152. presenceData.state = `Reading ${
  153. document.querySelectorAll(".entry-title.entry--item.h2")[0].innerHTML
  154. }`;
  155. presenceData.buttons = [
  156. {
  157. label: "Read Article",
  158. url: href,
  159. },
  160. ];
  161. } else if (pathname.toLowerCase() === "/fun/jojodle/") {
  162. presenceData.state = "Playing JoJodle!";
  163. presenceData.buttons = [
  164. {
  165. label: "Play Along!",
  166. url: href,
  167. },
  168. ];
  169. } else if (pathname.toLowerCase().includes("/category")) {
  170. let category = pathname.toLowerCase().match(/^\/category\/(.+)/)[1];
  171. category = newsUrlToCategory(category);
  172. presenceData.state = `Browsing category: ${category}`;
  173. }
  174. }
  175. if (!showButtons) delete presenceData.buttons;
  176. presence.setActivity(presenceData);
  177. });
  178. function titleToUrl(title: string) {
  179. return encodeURI(title.replace(/\s/g, "_"));
  180. }
  181. function urlToTitle(url: string) {
  182. return decodeURI(url.replaceAll("_", " "));
  183. }
  184. function toTitleCase(phrase: string) {
  185. return phrase
  186. .toLowerCase()
  187. .split(" ")
  188. .map(word => word.charAt(0).toUpperCase() + word.slice(1))
  189. .join(" ");
  190. }
  191. function newsUrlToCategory(url: string) {
  192. url = url.replace(/(^\/|\/$)/g, "");
  193. const urlSplit = url.split("/");
  194. // Get last sub category in category list
  195. if (urlSplit.length > 1) url = urlSplit[urlSplit.length - 1];
  196. url = url.replaceAll("-", " ");
  197. url = toTitleCase(url);
  198. url = url.replace(/\bjojos\b/i, "JoJo's");
  199. url = url.replace(/^interview$/i, "Interviews");
  200. url = url.replace(/^exclusive$/i, "Exclusives");
  201. url = url.replace(/^analysis$/i, "Analyses");
  202. return url;
  203. }