presence.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. const presence = new Presence({
  2. clientId: "733216821758525460",
  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/Wikidata/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/Wikidata/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. let title: string;
  49. const actionResult = (): string =>
  50. getURLParam("action") || getURLParam("veaction"),
  51. titleFromURL = (): string => {
  52. return decodeURI(
  53. (currentPath[1] === "index.php"
  54. ? getURLParam("title")
  55. : currentPath.slice(1).join("/")
  56. ).replaceAll("_", " ")
  57. );
  58. };
  59. try {
  60. title = document.querySelector(".wikibase-title")
  61. ? document.querySelector(".wikibase-title-label").textContent.trim()
  62. : document.querySelector("h1").textContent;
  63. } catch (e) {
  64. title = titleFromURL();
  65. }
  66. /**
  67. * Returns details based on the namespace.
  68. * @link https://www.wikidata.org/wiki/Help:Namespaces
  69. */
  70. const namespaceDetails = (): string => {
  71. const details: { [index: string]: string } = {
  72. "-2": "Viewing a media",
  73. "-1": "Viewing a special page",
  74. 0: "Reading an article",
  75. 1: "Viewing a talk page",
  76. 2: "Viewing a user page",
  77. 3: "Viewing a user talk page",
  78. 4: "Viewing a project page",
  79. 5: "Viewing a project talk page",
  80. 6: "Viewing a file",
  81. 7: "Viewing a file talk page",
  82. 8: "Viewing an interface page",
  83. 9: "Viewing an interface talk page",
  84. 10: "Viewing a template",
  85. 11: "Viewing a template talk page",
  86. 12: "Viewing a help page",
  87. 13: "Viewing a help talk page",
  88. 14: "Viewing a category",
  89. 15: "Viewing a category talk page",
  90. 120: "Viewing a propery",
  91. 121: "Viewing a propery talk page",
  92. 122: "Viewing a query",
  93. 123: "Viewing a query talk page",
  94. 146: "Viewing a lexeme",
  95. 147: "Viewing a lexeme talk page",
  96. 640: "Viewing a schema",
  97. 641: "Viewing a schema talk page",
  98. 828: "Viewing a module",
  99. 829: "Viewing a module talk page",
  100. 2300: "Viewing a gadget",
  101. 2301: "Viewing a gadget talk page",
  102. 2302: "Viewing a gadget definition page",
  103. 2303: "Viewing a gadget definition talk page",
  104. 2600: "Viewing a topic",
  105. };
  106. return (
  107. details[
  108. [...document.querySelector("body").classList]
  109. .find(v => /ns--?\d/.test(v))
  110. .slice(3)
  111. ] || "Viewing a page"
  112. );
  113. };
  114. if (
  115. (
  116. document.querySelector<HTMLAnchorElement>("#n-mainpage a") ||
  117. document.querySelector<HTMLAnchorElement>("#p-navigation a") ||
  118. document.querySelector<HTMLAnchorElement>(".mw-wiki-logo")
  119. ).href === currentURL.href
  120. )
  121. presenceData.details = "On the main page";
  122. else if (document.querySelector("#wpLoginAttempt"))
  123. presenceData.details = "Logging in";
  124. else if (document.querySelector("#wpCreateaccount"))
  125. presenceData.details = "Creating an account";
  126. else if (document.querySelector(".searchresults")) {
  127. presenceData.details = "Searching for a page";
  128. presenceData.state = (
  129. document.querySelector("input[type=search]") as HTMLInputElement
  130. ).value;
  131. } else if (actionResult() === "history") {
  132. presenceData.details = "Viewing revision history";
  133. presenceData.state = titleFromURL();
  134. } else if (getURLParam("diff")) {
  135. presenceData.details = "Viewing difference between revisions";
  136. presenceData.state = titleFromURL();
  137. } else if (getURLParam("oldid")) {
  138. presenceData.details = "Viewing an old revision of a page";
  139. presenceData.state = titleFromURL();
  140. } else if (document.querySelector("#ca-ve-edit") || getURLParam("veaction")) {
  141. presenceData.state = `${
  142. title.toLowerCase() === titleFromURL().toLowerCase()
  143. ? `${title}`
  144. : `${title} (${titleFromURL()})`
  145. }`;
  146. updateCallback.function = (): void => {
  147. if (actionResult() === "edit" || actionResult() === "editsource")
  148. presenceData.details = "Editing a page";
  149. else presenceData.details = namespaceDetails();
  150. };
  151. } else if (actionResult() === "edit") {
  152. presenceData.details = document.querySelector("#ca-edit")
  153. ? "Editing a page"
  154. : "Viewing source";
  155. presenceData.state = titleFromURL();
  156. } else {
  157. presenceData.details = namespaceDetails();
  158. presenceData.state = `${
  159. title.toLowerCase() === titleFromURL().toLowerCase()
  160. ? `${title}`
  161. : `${title} (${titleFromURL()})`
  162. }`;
  163. }
  164. })();
  165. if (updateCallback.present) {
  166. const defaultData = { ...presenceData };
  167. presence.on("UpdateData", async () => {
  168. resetData(defaultData);
  169. updateCallback.function();
  170. presence.setActivity(presenceData);
  171. });
  172. } else {
  173. presence.on("UpdateData", async () => {
  174. presence.setActivity(presenceData);
  175. });
  176. }