presence.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const presence = new Presence({
  2. clientId: "733216877240647690",
  3. });
  4. let currentURL = new URL(document.location.href),
  5. currentPath = currentURL.pathname.replace(/^\/|\/$/g, "").split("/");
  6. const browsingTimestamp = Math.floor(Date.now() / 1000);
  7. let presenceData: PresenceData = {
  8. details: "Viewing an unsupported page",
  9. largeImageKey:
  10. "https://cdn.rcd.gg/PreMiD/websites/W/Wikiquote/assets/logo.png",
  11. startTimestamp: browsingTimestamp,
  12. };
  13. const updateCallback = {
  14. _function: null as () => void,
  15. get function(): () => void {
  16. return this._function;
  17. },
  18. set function(parameter) {
  19. this._function = parameter;
  20. },
  21. get present(): boolean {
  22. return this._function !== null;
  23. },
  24. },
  25. /**
  26. * Initialize/reset presenceData.
  27. */
  28. resetData = (
  29. defaultData: PresenceData = {
  30. details: "Viewing an unsupported page",
  31. largeImageKey:
  32. "https://cdn.rcd.gg/PreMiD/websites/W/Wikiquote/assets/logo.png",
  33. startTimestamp: browsingTimestamp,
  34. }
  35. ): void => {
  36. currentURL = new URL(document.location.href);
  37. currentPath = currentURL.pathname.replace(/^\/|\/$/g, "").split("/");
  38. presenceData = { ...defaultData };
  39. },
  40. /**
  41. * Search for URL parameters.
  42. * @param urlParam The parameter that you want to know about the value.
  43. */
  44. getURLParam = (urlParam: string): string => {
  45. return currentURL.searchParams.get(urlParam);
  46. };
  47. ((): void => {
  48. if (currentURL.hostname === "www.wikiquote.org")
  49. presenceData.details = "On the home page";
  50. else {
  51. let title: string;
  52. const actionResult = (): string =>
  53. getURLParam("action") || getURLParam("veaction"),
  54. [lang] = currentURL.hostname.split("."),
  55. titleFromURL = (): string => {
  56. return decodeURI(
  57. (currentPath[1] === "index.php"
  58. ? getURLParam("title")
  59. : currentPath.slice(1).join("/")
  60. ).replaceAll("_", " ")
  61. );
  62. };
  63. try {
  64. title = document.querySelector("h1").textContent;
  65. } catch (e) {
  66. title = titleFromURL();
  67. }
  68. /**
  69. * Returns details based on the namespace.
  70. * @link https://en.wikiquote.org/wiki/Help:Namespace
  71. */
  72. const namespaceDetails = (): string => {
  73. const details: { [index: string]: string } = {
  74. "-2": "Viewing a media",
  75. "-1": "Viewing a special page",
  76. 0: "Reading an article",
  77. 1: "Viewing a talk page",
  78. 2: "Viewing a user page",
  79. 3: "Viewing a user talk page",
  80. 4: "Viewing a project page",
  81. 5: "Viewing a project talk page",
  82. 6: "Viewing a file",
  83. 7: "Viewing a file talk page",
  84. 8: "Viewing an interface page",
  85. 9: "Viewing an interface talk page",
  86. 10: "Viewing a template",
  87. 11: "Viewing a template talk page",
  88. 12: "Viewing a help page",
  89. 13: "Viewing a help talk page",
  90. 14: "Viewing a category",
  91. 15: "Viewing a category talk page",
  92. 100: "Viewing a portal",
  93. 101: "Viewing a portal talk page",
  94. 828: "Viewing a module",
  95. 829: "Viewing a module talk page",
  96. 2300: "Viewing a gadget",
  97. 2301: "Viewing a gadget talk page",
  98. 2302: "Viewing a gadget definition page",
  99. 2303: "Viewing a gadget definition talk page",
  100. 2600: "Viewing a topic",
  101. };
  102. return (
  103. details[
  104. [...document.querySelector("body").classList]
  105. .find(v => /ns--?\d/.test(v))
  106. .slice(3)
  107. ] || "Viewing a page"
  108. );
  109. };
  110. //
  111. // Important note:
  112. //
  113. // When checking for the current location, avoid using the URL.
  114. // The URL is going to be different in other languages.
  115. // Use the elements on the page instead.
  116. //
  117. if (
  118. (
  119. (document.querySelector("#n-mainpage a") ||
  120. document.querySelector("#p-navigation a") ||
  121. document.querySelector(".mw-wiki-logo")) as HTMLAnchorElement
  122. ).href === currentURL.href
  123. )
  124. presenceData.details = "On the main page";
  125. else if (document.querySelector("#wpLoginAttempt"))
  126. presenceData.details = "Logging in";
  127. else if (document.querySelector("#wpCreateaccount"))
  128. presenceData.details = "Creating an account";
  129. else if (document.querySelector(".searchresults")) {
  130. presenceData.details = "Searching for a page";
  131. presenceData.state = (
  132. document.querySelector("input[type=search]") as HTMLInputElement
  133. ).value;
  134. } else if (actionResult() === "history") {
  135. presenceData.details = "Viewing revision history";
  136. presenceData.state = titleFromURL();
  137. } else if (getURLParam("diff")) {
  138. presenceData.details = "Viewing difference between revisions";
  139. presenceData.state = titleFromURL();
  140. } else if (getURLParam("oldid")) {
  141. presenceData.details = "Viewing an old revision of a page";
  142. presenceData.state = titleFromURL();
  143. } else if (
  144. document.querySelector("#ca-ve-edit") ||
  145. getURLParam("veaction")
  146. ) {
  147. presenceData.state = `${
  148. title.toLowerCase() === titleFromURL().toLowerCase()
  149. ? `${title}`
  150. : `${title} (${titleFromURL()})`
  151. }`;
  152. updateCallback.function = (): void => {
  153. if (actionResult() === "edit" || actionResult() === "editsource")
  154. presenceData.details = "Editing a page";
  155. else presenceData.details = namespaceDetails();
  156. };
  157. } else if (actionResult() === "edit") {
  158. presenceData.details = document.querySelector("#ca-edit")
  159. ? "Editing a page"
  160. : "Viewing source";
  161. presenceData.state = titleFromURL();
  162. } else {
  163. presenceData.details = namespaceDetails();
  164. presenceData.state = `${
  165. title.toLowerCase() === titleFromURL().toLowerCase()
  166. ? `${title}`
  167. : `${title} (${titleFromURL()})`
  168. }`;
  169. }
  170. if (lang !== "en") {
  171. if (presenceData.state) presenceData.state += ` (${lang})`;
  172. else presenceData.details += ` (${lang})`;
  173. }
  174. }
  175. })();
  176. if (updateCallback.present) {
  177. const defaultData = { ...presenceData };
  178. presence.on("UpdateData", async () => {
  179. resetData(defaultData);
  180. updateCallback.function();
  181. presence.setActivity(presenceData);
  182. });
  183. } else {
  184. presence.on("UpdateData", async () => {
  185. presence.setActivity(presenceData);
  186. });
  187. }