presence.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. const presence = new Presence({
  2. clientId: "632013978608074764",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. let user: HTMLElement | Element | string,
  6. title: HTMLElement | Element | string,
  7. search: HTMLElement | Element | string,
  8. playing: boolean,
  9. paused: boolean,
  10. progress: HTMLElement | Element | string | number,
  11. lastState: HTMLElement | Element | string;
  12. lastState = null;
  13. presence.on("UpdateData", async () => {
  14. const presenceData: PresenceData = {
  15. largeImageKey:
  16. "https://cdn.rcd.gg/PreMiD/websites/M/Monstercat/assets/logo.png",
  17. };
  18. if (document.location.hostname === "www.monstercat.com") {
  19. progress = document.querySelector(".progress"); //Catches the progress bar
  20. progress = (progress as HTMLStyleElement).style.cssText
  21. .replace("width: ", "")
  22. .replace("%;", ""); //Replace everything so only "x.xxxx" is left (x standing for numbers)
  23. if (lastState === progress && progress !== "0" && progress !== "100") {
  24. playing = true;
  25. paused = true;
  26. } else if (progress === "0" || progress === "100") {
  27. playing = false;
  28. paused = true;
  29. } else {
  30. lastState = progress;
  31. playing = true;
  32. paused = false;
  33. }
  34. progress = Number(progress);
  35. progress = Math.round(progress);
  36. if (playing === true && paused === false) {
  37. title = document.querySelector(
  38. "body > header > div.container.player > div.flex.controls.push-right.playing > a > span"
  39. );
  40. presenceData.details = (title as HTMLElement).textContent;
  41. presenceData.state = `${progress}% progressed`;
  42. presenceData.smallImageKey = Assets.Play;
  43. presenceData.smallImageText = "Playing";
  44. } else if (playing === true && paused === true) {
  45. title = document.querySelector(
  46. "body > header > div.container.player > div.flex.controls.push-right.playing > a > span"
  47. );
  48. presenceData.details = (title as HTMLElement).textContent;
  49. presenceData.state = `${progress}% progressed`;
  50. presenceData.smallImageKey = Assets.Pause;
  51. presenceData.smallImageText = "Paused";
  52. } else {
  53. //If there is no song playing display site information
  54. presenceData.startTimestamp = browsingTimestamp;
  55. if (document.location.pathname.includes("/release/")) {
  56. title = document.querySelector(
  57. "body > section > div:nth-child(1) > div.container.flex > div > h1"
  58. );
  59. user = document.querySelector(
  60. "body > section > div:nth-child(1) > div.container.flex > div > h3"
  61. );
  62. presenceData.details = "Viewing release:";
  63. presenceData.state = `${(title as HTMLElement).textContent} by ${
  64. (user as HTMLElement).textContent
  65. }`;
  66. } else if (document.location.pathname.includes("/artist/")) {
  67. user = document.querySelector(
  68. "body > section > div.top-banner > div.container.flex > div > div > h1"
  69. );
  70. presenceData.details = "Viewing artist:";
  71. presenceData.state = (user as HTMLElement).textContent;
  72. } else if (document.location.pathname.includes("/music"))
  73. presenceData.details = "Browsing music releases...";
  74. else if (document.location.pathname.includes("/browse"))
  75. presenceData.details = "Browsing...";
  76. else if (document.location.pathname.includes("/catalog"))
  77. presenceData.details = "Viewing catalog";
  78. else if (document.location.pathname.includes("/artists"))
  79. presenceData.details = "Viewing artists";
  80. else if (document.location.pathname.includes("/playlist/")) {
  81. title = document.querySelector("body > section > div > h1");
  82. presenceData.details = "Viewing playlist:";
  83. presenceData.state = (title as HTMLElement).textContent;
  84. } else if (document.location.pathname.includes("/playlists"))
  85. presenceData.details = "Viewing their playlists";
  86. else if (document.location.pathname.includes("/events"))
  87. presenceData.details = "Viewing events";
  88. else if (document.location.pathname.includes("/event/")) {
  89. title = document.querySelector(
  90. "body > section > div.event-page-header > div > div.container.container--event-header.flex > div > a.silent.no-hover > h1"
  91. );
  92. presenceData.details = "Reading about event:";
  93. if ((title as HTMLElement).textContent.length > 128) {
  94. presenceData.state = `${(title as HTMLElement).textContent.substring(
  95. 0,
  96. 125
  97. )}...`;
  98. } else presenceData.state = (title as HTMLElement).textContent;
  99. presenceData.smallImageKey = Assets.Reading;
  100. } else if (document.location.pathname.includes("/publishing"))
  101. presenceData.details = "Viewing publishing";
  102. else if (document.location.pathname.includes("/cotw"))
  103. presenceData.details = "Viewing radio";
  104. else if (document.location.pathname.includes("/gold"))
  105. presenceData.details = "Viewing Monstercat Gold";
  106. else if (document.location.pathname.includes("/account"))
  107. presenceData.details = "Viewing their account";
  108. else if (document.location.pathname.includes("/blog/")) {
  109. if (document.location.pathname.includes("/tags/")) {
  110. title = document.querySelector("head > title");
  111. title = (title as HTMLElement).textContent.replace(
  112. " Posts - Monstercat",
  113. ""
  114. );
  115. presenceData.details = "Blog - Viewing tag:";
  116. presenceData.state = title;
  117. } else {
  118. title = document.querySelector(
  119. "body > section > div.panel.panel--article > header > h1"
  120. );
  121. presenceData.details = "Reading article:";
  122. if ((title as HTMLElement).textContent.length > 128) {
  123. presenceData.state = `${(
  124. title as HTMLElement
  125. ).textContent.substring(0, 125)}...`;
  126. } else presenceData.state = (title as HTMLElement).textContent;
  127. presenceData.smallImageKey = Assets.Reading;
  128. }
  129. } else if (document.location.pathname.includes("/blog"))
  130. presenceData.details = "Viewing blog posts";
  131. else if (document.location.pathname.includes("/search")) {
  132. search = document.querySelector(
  133. "body > header > div.container.player > div.col-xs-hidden.col-md-visible.global-search > form > input[type=text]"
  134. );
  135. presenceData.details = "Searching for:";
  136. presenceData.state = (search as HTMLInputElement).value;
  137. presenceData.smallImageKey = Assets.Search;
  138. } else if (document.location.pathname === "/")
  139. presenceData.details = "Viewing homepage";
  140. }
  141. } else if (document.location.hostname === "shop.monstercat.com") {
  142. presenceData.startTimestamp = browsingTimestamp;
  143. if (document.location.pathname.includes("/products/")) {
  144. presenceData.details = "Shop - Viewing product:";
  145. title = document.querySelector(
  146. "#product-description > div:nth-child(1) > h1"
  147. );
  148. if ((title as HTMLElement).textContent.length > 128) {
  149. presenceData.state = `${(title as HTMLElement).textContent.substring(
  150. 0,
  151. 125
  152. )}...`;
  153. } else presenceData.state = (title as HTMLElement).textContent;
  154. } else if (document.location.pathname.includes("/collections/")) {
  155. presenceData.details = "Shop - Viewing collection:";
  156. title = document.querySelector("#collection-description > h1");
  157. presenceData.state = (title as HTMLElement).textContent;
  158. } else if (document.location.pathname.includes("/cart"))
  159. presenceData.details = "Shop - Viewing cart";
  160. else if (document.location.pathname === "/")
  161. presenceData.details = "Viewing store front";
  162. }
  163. if (presenceData.details) presence.setActivity(presenceData);
  164. else presence.setActivity();
  165. });