presence.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. const presence = new Presence({
  2. clientId: "629473655218241557",
  3. }),
  4. strings = presence.getStrings({
  5. play: "general.playing",
  6. pause: "general.paused",
  7. });
  8. function getTime(timegone: string[], timetotal: string[]): number[] {
  9. return [
  10. parseInt(timegone[0]) + parseInt(timegone[1]) * 60,
  11. parseInt(timetotal[0]) + parseInt(timetotal[1]) * 60,
  12. ];
  13. }
  14. function getTimeLeft(Time: string[]): number[] {
  15. const parsedAudioDuration = getTime(
  16. Time[0].split(":").reverse(),
  17. Time[1].split(":").reverse()
  18. );
  19. return [parsedAudioDuration[0], parsedAudioDuration[1]];
  20. }
  21. presence.on("UpdateData", async () => {
  22. const presenceData: PresenceData = {
  23. largeImageKey:
  24. "https://cdn.rcd.gg/PreMiD/websites/M/Musescore/assets/logo.png",
  25. };
  26. // ALL ON THE .ORG website
  27. if (document.location.pathname.endsWith("/forum")) {
  28. presenceData.details = "Looking at";
  29. presenceData.state = "the forums.";
  30. }
  31. if (document.location.pathname.endsWith("/download")) {
  32. presenceData.details = "Looking how to";
  33. presenceData.state = "download Musescore.";
  34. }
  35. if (document.location.pathname.includes("/handbook")) {
  36. presenceData.details = "Looking at";
  37. presenceData.state = "the handbook.";
  38. }
  39. if (document.location.pathname.endsWith("/plugins")) {
  40. presenceData.details = "Looking at";
  41. presenceData.state = "plugins.";
  42. }
  43. if (document.location.pathname.endsWith("/services")) {
  44. presenceData.details = "Looking at";
  45. presenceData.state = "services.";
  46. }
  47. if (
  48. document.location.pathname.includes("/tutorials") ||
  49. document.location.pathname.includes("/howto")
  50. ) {
  51. presenceData.details = "Looking at";
  52. presenceData.state = "tutorials.";
  53. }
  54. if (document.location.pathname.endsWith("/faq")) {
  55. presenceData.details = "Looking at";
  56. presenceData.state = "the FAQ.";
  57. }
  58. if (document.location.pathname.endsWith("/tracker")) {
  59. presenceData.details = "Looking at";
  60. presenceData.state = "recent content.";
  61. }
  62. // Main stuff
  63. if (document.location.pathname.includes("/dashboard")) {
  64. presenceData.details = "Looking at";
  65. presenceData.state = "their dashboard.";
  66. }
  67. if (document.location.pathname.startsWith("/piano-tutorial")) {
  68. presenceData.details = "Looking at";
  69. presenceData.state = "piano tutorials.";
  70. }
  71. if (document.location.pathname.startsWith("/community")) {
  72. presenceData.details = "Looking at";
  73. presenceData.state = "communities.";
  74. }
  75. if (document.location.pathname.includes("/sheetmusic")) {
  76. presenceData.details = "Looking at";
  77. presenceData.state = "sheetmusic.";
  78. }
  79. if (document.location.pathname.startsWith("/upload")) {
  80. presenceData.details = "Uploading";
  81. presenceData.state = "their music.";
  82. }
  83. if (document.location.pathname.startsWith("/hub")) {
  84. presenceData.details = "Looking at";
  85. presenceData.state = `${
  86. document.location.pathname.split("/")[2]
  87. } sheetmusic.`;
  88. }
  89. if (document.location.pathname.endsWith("/my-scores")) {
  90. presenceData.details = "Looking at";
  91. presenceData.state = "their sheetmusic.";
  92. }
  93. if (document.location.pathname.startsWith("/upgrade")) {
  94. presenceData.details = "Considering";
  95. presenceData.state = "upgrading to pro.";
  96. }
  97. if (document.location.pathname.startsWith("/checkout")) {
  98. presenceData.details = "Checking something";
  99. presenceData.state = "out.";
  100. }
  101. if (document.location.pathname.endsWith("/group/create")) {
  102. presenceData.details = "Creating a";
  103. presenceData.state = "new group.";
  104. }
  105. if (document.location.pathname.endsWith("/community-guidelines")) {
  106. presenceData.details = "Browsing the";
  107. presenceData.state = "community guidelines.";
  108. }
  109. if (
  110. document.location.pathname.endsWith("/press") ||
  111. document.location.pathname.endsWith("/news")
  112. ) {
  113. presenceData.details = "Browsing the";
  114. presenceData.state = "press.";
  115. }
  116. if (document.location.pathname.endsWith("/jobs")) {
  117. presenceData.details = "Looking at";
  118. presenceData.state = "job opportunities.";
  119. }
  120. if (document.location.pathname.endsWith("/about")) {
  121. presenceData.details = "Looking at";
  122. presenceData.state = "the about section.";
  123. }
  124. if (document.location.pathname.startsWith("/hc")) {
  125. presenceData.details = "Looking at";
  126. presenceData.state = "the help center.";
  127. }
  128. if (document.location.pathname.startsWith("/contact")) {
  129. presenceData.details = "Contacting";
  130. presenceData.state = "Musescore.";
  131. }
  132. // This is here since some profiles dont have user.
  133. if (
  134. document.querySelector(
  135. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  136. )
  137. ) {
  138. presenceData.details = "Browing";
  139. presenceData.state = `${
  140. document.querySelector(
  141. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  142. ).textContent
  143. }'s profile`;
  144. }
  145. // MUSIC PLAYING / PAUSING
  146. if (
  147. document.querySelector(
  148. "#jmuse-container > div.viewer > div.viewerWrapper > div"
  149. )
  150. ) {
  151. if (
  152. `${document
  153. .querySelector(
  154. "#jmuse-container > div:nth-child(1) > div > div > div > div._1DDmo.undefined > div:nth-child(1) > div > div > div.oY3Aa > button"
  155. )
  156. .getAttribute("state")}` === "default"
  157. ) {
  158. presenceData.smallImageKey = Assets.Pause;
  159. presenceData.details = "Looking at";
  160. presenceData.state = document.querySelector(
  161. "body > div.page.page-score > div.container > div.row > main > article > div > div.score-right > h1"
  162. ).textContent;
  163. presenceData.smallImageText = (await strings).pause;
  164. } else if (
  165. `${document
  166. .querySelector(
  167. "#jmuse-container > div:nth-child(1) > div > div > div > div._1DDmo.undefined > div:nth-child(1) > div > div > div.oY3Aa > button"
  168. )
  169. .getAttribute("state")}` === "primary"
  170. ) {
  171. const time = getTimeLeft([
  172. document
  173. .querySelector(
  174. "#jmuse-container > div:nth-child(1) > div > div > div > div._1DDmo.undefined > div:nth-child(1) > div > div > div._3vWaq > span"
  175. )
  176. .textContent.split("/")[0],
  177. document
  178. .querySelector(
  179. "#jmuse-container > div:nth-child(1) > div > div > div > div._1DDmo.undefined > div:nth-child(1) > div > div > div._3vWaq > span"
  180. )
  181. .textContent.split("/")[1],
  182. ]);
  183. [presenceData.startTimestamp, presenceData.endTimestamp] =
  184. presence.getTimestamps(time[0], time[1]);
  185. presenceData.smallImageKey = Assets.Play;
  186. presenceData.details = "Listening to";
  187. presenceData.state = document.querySelector(
  188. "body > div.page.page-score > div.container > div.row > main > article > div > div.score-right > h1"
  189. ).textContent;
  190. presenceData.smallImageText = (await strings).play;
  191. }
  192. }
  193. if (document.location.pathname.includes("/user")) {
  194. if (document.location.pathname.includes("/edit")) {
  195. presenceData.details = "Editing";
  196. presenceData.state = "their account.";
  197. }
  198. if (document.location.pathname.includes("settings/profile")) {
  199. presenceData.details = "Editing";
  200. presenceData.state = "their profile.";
  201. }
  202. if (document.location.pathname.includes("subscription")) {
  203. presenceData.details = "Viewing a";
  204. presenceData.state = "subscription.";
  205. }
  206. if (document.location.pathname.includes("billing")) {
  207. presenceData.details = "Viewing their";
  208. presenceData.state = "billing history.";
  209. }
  210. if (document.location.pathname.includes("gifts")) {
  211. presenceData.details = "Viewing their";
  212. presenceData.state = "gifts.";
  213. }
  214. if (document.location.pathname.includes("notifications")) {
  215. presenceData.details = "Viewing their";
  216. presenceData.state = "notifications.";
  217. }
  218. if (document.location.pathname.includes("/message")) {
  219. presenceData.details = "Looking at";
  220. presenceData.state = "messages.";
  221. }
  222. if (document.location.pathname.includes("/followers")) {
  223. presenceData.details = "Looking at";
  224. presenceData.state = `${
  225. document.querySelector(
  226. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  227. ).textContent
  228. }'s followers.`;
  229. }
  230. if (document.location.pathname.includes("/following")) {
  231. presenceData.details = "Looking who";
  232. presenceData.state = `${
  233. document.querySelector(
  234. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  235. ).textContent
  236. } is following.`;
  237. }
  238. if (document.location.pathname.includes("/invite")) {
  239. presenceData.details = "Inviting";
  240. presenceData.state = "some friends.";
  241. }
  242. if (
  243. document.querySelector(
  244. "body > div.page.js-user-profile-page > div.content-header.collapsed > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  245. )
  246. ) {
  247. presenceData.details = "Browing";
  248. presenceData.state = `${
  249. document.querySelector(
  250. "body > div.page.js-user-profile-page > div.content-header.collapsed > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  251. ).textContent
  252. }'s profile`;
  253. }
  254. } else if (
  255. document.querySelector(
  256. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  257. )
  258. ) {
  259. if (document.location.pathname.includes("/sheetmusic")) {
  260. presenceData.details = "Looking at";
  261. presenceData.state = `${
  262. document.querySelector(
  263. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  264. ).textContent
  265. }'s sheetmusic.`;
  266. }
  267. if (document.location.pathname.includes("/favorites")) {
  268. presenceData.details = "Looking at";
  269. presenceData.state = `${
  270. document.querySelector(
  271. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  272. ).textContent
  273. }'s favorites.`;
  274. }
  275. if (document.location.pathname.includes("/sets")) {
  276. presenceData.details = "Looking at";
  277. presenceData.state = `${
  278. document.querySelector(
  279. "body > div.page.js-user-profile-page > div.content-header > div.banner-header.group-header > div > div.info-wrapper.group-info-wrapper.user-info-wrapper > div > h1 > a"
  280. ).textContent
  281. }'s sets.`;
  282. }
  283. }
  284. presence.setActivity(presenceData);
  285. });