presence.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const presence = new Presence({
  2. clientId: "748698437720997888",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. presence.on("UpdateData", async () => {
  6. const presenceData: PresenceData = {
  7. largeImageKey:
  8. "https://cdn.rcd.gg/PreMiD/websites/R/Reach%20Radio/assets/logo.png",
  9. },
  10. [info, elapsed, format1, format2] = await Promise.all([
  11. presence.getSetting<boolean>("sInfo"),
  12. presence.getSetting<boolean>("tElapsed"),
  13. presence.getSetting<string>("sFormat1"),
  14. presence.getSetting<string>("sFormat2"),
  15. ]),
  16. paused =
  17. document.querySelector<HTMLImageElement>(
  18. "#react-listen-content > div > div > div.pt-4.startpause.col > button > img"
  19. ).src ===
  20. "https://radiopanel.s3.nl-ams.scw.cloud/c9a65443-eed1-41ed-b9d2-743223b5ee75/a01dadcd-df3d-484b-8d20-4923156ce77a.svg";
  21. if (info && paused) {
  22. if (elapsed) presenceData.startTimestamp = browsingTimestamp;
  23. if (document.querySelector("#message > div")) {
  24. presenceData.details = "Requesting a song";
  25. presenceData.smallImageKey = Assets.Writing;
  26. } else if (document.location.pathname.includes("/about")) {
  27. presenceData.details = "Reading about Reach Radio";
  28. presenceData.smallImageKey = Assets.Reading;
  29. } else if (document.location.pathname.includes("/timetable")) {
  30. presenceData.details = "Viewing the timetable";
  31. presenceData.smallImageKey = Assets.Reading;
  32. } else if (document.location.pathname.includes("/team")) {
  33. presenceData.details = "Viewing the team";
  34. presenceData.smallImageKey = Assets.Reading;
  35. } else if (document.location.pathname.includes("/privacy")) {
  36. presenceData.details = "Reading Privacy Policy";
  37. presenceData.smallImageKey = Assets.Reading;
  38. } else if (document.location.pathname.includes("/alexa-terms")) {
  39. presenceData.details = "Reading Alexa Policy";
  40. presenceData.smallImageKey = Assets.Reading;
  41. } else if (document.location.pathname.includes("/contact")) {
  42. presenceData.details = "Writing to Reach Radio";
  43. presenceData.smallImageKey = Assets.Writing;
  44. } else if (document.location.pathname.includes("/all-news")) {
  45. presenceData.details = "Viewing the news";
  46. presenceData.smallImageKey = Assets.Reading;
  47. } else if (document.location.pathname.includes("/news/")) {
  48. presenceData.details = "Reading article:";
  49. presenceData.state = document.querySelector(
  50. "#news3 > section > div > div > div > h4"
  51. ).textContent;
  52. presenceData.smallImageKey = Assets.Reading;
  53. } else if (document.location.pathname.includes("/podcasts")) {
  54. presenceData.details = "Viewing podcasts";
  55. presenceData.smallImageKey = Assets.Reading;
  56. } else if (document.location.pathname.includes("/podcast/")) {
  57. presenceData.details = "Viewing podcast:";
  58. presenceData.state = document.querySelector(".pod-name").textContent;
  59. presenceData.smallImageKey = Assets.Reading;
  60. } else if (document.location.pathname.includes("/listen-to-podcast/")) {
  61. presenceData.details = "Listening to podcast:";
  62. presenceData.state = document.querySelector(".pod-name").textContent;
  63. if (
  64. document.querySelector(
  65. "#__layout > div > div.page > section > div > div.pod-player > div > div > div > button:nth-child(1)"
  66. ).className === "plyr__controls__item plyr__control"
  67. ) {
  68. presenceData.smallImageKey = Assets.Pause;
  69. delete presenceData.startTimestamp;
  70. delete presenceData.endTimestamp;
  71. } else {
  72. presenceData.smallImageKey = Assets.Play;
  73. let podcastDuration = 0;
  74. const timeArray = document
  75. .querySelector(".plyr__time")
  76. .textContent.split(":");
  77. switch (timeArray.length) {
  78. case 3: {
  79. podcastDuration =
  80. parseInt(timeArray[2]) +
  81. parseInt(timeArray[1].replace("-", "")) * 60 +
  82. parseInt(timeArray[0].replace("-", "")) * 60 * 60;
  83. break;
  84. }
  85. case 2: {
  86. podcastDuration =
  87. parseInt(timeArray[1]) +
  88. parseInt(timeArray[0].replace("-", "")) * 60;
  89. break;
  90. }
  91. case 1:
  92. {
  93. podcastDuration = parseInt(timeArray[1]);
  94. // No default
  95. }
  96. break;
  97. }
  98. presenceData.startTimestamp = Math.floor(Date.now() / 1000);
  99. presenceData.endTimestamp =
  100. Math.floor(Date.now() / 1000) + podcastDuration;
  101. }
  102. } else if (document.location.pathname === "/") {
  103. presenceData.details = "Browsing...";
  104. presenceData.smallImageKey = Assets.Reading;
  105. presenceData.smallImageText =
  106. document.querySelector("#current_song").textContent;
  107. }
  108. } else {
  109. if (paused) {
  110. presenceData.smallImageKey = Assets.Pause;
  111. delete presenceData.startTimestamp;
  112. delete presenceData.endTimestamp;
  113. } else {
  114. presenceData.smallImageKey = Assets.Play;
  115. if (elapsed) presenceData.startTimestamp = browsingTimestamp;
  116. }
  117. const [artist, title] = document
  118. .querySelector("#current_song")
  119. .textContent.split(" - "),
  120. presenter = document.querySelector(".show_time").textContent;
  121. presenceData.details = format1
  122. .replace("%title%", title)
  123. .replace("%artist%", artist)
  124. .replace("%presenter%", presenter);
  125. presenceData.state = format2
  126. .replace("%title%", title)
  127. .replace("%artist%", artist)
  128. .replace("%presenter%", presenter);
  129. if (document.querySelector("#message > div"))
  130. presenceData.smallImageText = "Requesting a song";
  131. else if (document.location.pathname.includes("/about"))
  132. presenceData.smallImageText = "Reading about Reach Radio";
  133. else if (document.location.pathname.includes("/timetable"))
  134. presenceData.smallImageText = "Viewing the timetable";
  135. else if (document.location.pathname.includes("/team"))
  136. presenceData.smallImageText = "Viewing the team";
  137. else if (document.location.pathname.includes("/privacy"))
  138. presenceData.smallImageText = "Reading Privacy Policy";
  139. else if (document.location.pathname.includes("/alexa-terms"))
  140. presenceData.smallImageText = "Reading Alexa Policy";
  141. else if (document.location.pathname.includes("/contact"))
  142. presenceData.smallImageText = "Writing to Reach Radio";
  143. else if (document.location.pathname.includes("/all-news"))
  144. presenceData.smallImageText = "Viewing the news";
  145. else if (document.location.pathname.includes("/news/")) {
  146. presenceData.smallImageText = `Reading article: ${
  147. document.querySelector("#news3 > section > div > div > div > h4")
  148. .textContent
  149. }`;
  150. } else if (document.location.pathname.includes("/podcasts"))
  151. presenceData.smallImageText = "Viewing podcasts";
  152. else if (document.location.pathname.includes("/podcast/")) {
  153. presenceData.smallImageText = `Viewing podcast: ${
  154. document.querySelector(".pod-name").textContent
  155. }`;
  156. } else if (document.location.pathname.includes("/listen-to-podcast/")) {
  157. presenceData.smallImageText = `Listening to podcast: ${
  158. document.querySelector(".pod-name").textContent
  159. }`;
  160. } else if (document.location.pathname === "/")
  161. presenceData.smallImageText = "Browsing...";
  162. }
  163. if (presenceData.details) presence.setActivity(presenceData);
  164. else presence.setActivity();
  165. });