presence.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Initialize the presence
  2. const presence = new Presence({
  3. clientId: "697983563857002517",
  4. });
  5. // Global variables
  6. let startTime = Date.now(),
  7. videoPlayer: HTMLVideoElement,
  8. videoDuration: number,
  9. cuTime: number,
  10. endTime: number,
  11. videoState = "paused", // Default
  12. metadata: string,
  13. // Set the default presence data for when the video is loading
  14. presenceData: PresenceData = {
  15. largeImageKey:
  16. "https://cdn.rcd.gg/PreMiD/websites/V/VUDU/assets/0.png" /*The key (file name) of the Large Image on the presence. These are uploaded and named in the Rich Presence section of your application, called Art Assets*/,
  17. details: "Browsing VUDU",
  18. };
  19. // Get the title
  20. function grabMetadata(): void {
  21. // Get the close button first (Don't worry this will make sense)
  22. const closeButton = document.querySelector('[aria-label="Close"]');
  23. // If there's not a close button, then the user isn't watching anything
  24. if (!closeButton) return;
  25. // Get all the elements inside of the parent that are span (there should only be one) and get the innerHTML
  26. metadata = closeButton.parentElement.querySelectorAll("span")[0].textContent;
  27. }
  28. // Get the video player element
  29. function getVideoPlayer(): void {
  30. // VUDU plays movies in an iFrame. Cool! Let's get that iFrame
  31. const VUDUIFrame = document.querySelector(
  32. "#contentPlayerFrame"
  33. ) as HTMLIFrameElement,
  34. // Finally... get the video
  35. videoPlayer = (
  36. VUDUIFrame.contentDocument || VUDUIFrame.contentWindow.document
  37. ).querySelector("#videoPlayer") as HTMLVideoElement;
  38. videoDuration = videoPlayer.duration; // duration of movie in seconds
  39. cuTime = videoPlayer.currentTime; // where the user is currently at
  40. }
  41. function calculateEndTime(): void {
  42. videoState = "playing"; // Tell PreMID the video is playing correctly (aka don't update the end time again)
  43. startTime = Date.now(); // Get the point where the user started watching
  44. endTime = startTime + (videoDuration - cuTime) * 1000; // Get the end of the video (time left)
  45. if (isNaN(endTime)) {
  46. // If the video DIDN'T load correctly then endTime will be NaN
  47. videoState = "loading"; // Video is still loading, calculate again
  48. calculateEndTime();
  49. }
  50. }
  51. function pausePresence(): void {
  52. videoState = "paused"; // Tell PreMID the user paused, this lets us calculate endTime again when the user starts playing
  53. }
  54. setInterval(grabMetadata, 10000); // Metadata shouldn't ever really change... so this is fine
  55. setInterval(getVideoPlayer, 1000); // If I was dumb enough to run this every frame I would, but I'm considerate and so it shall only run every second
  56. presence.on("UpdateData", () => {
  57. // Video doesn't exist, and there's no metadata either.
  58. // Aka, user isn't watching anything.
  59. if (videoPlayer && metadata) {
  60. // When the video pauses
  61. if (videoPlayer.paused) {
  62. // Only run this once
  63. if (videoState !== "paused") pausePresence();
  64. // Discord says "hey nice pause"
  65. presenceData = {
  66. largeImageKey:
  67. "https://cdn.rcd.gg/PreMiD/websites/V/VUDU/assets/0.png" /*The key (file name) of the Large Image on the presence. These are uploaded and named in the Rich Presence section of your application, called Art Assets*/,
  68. details: `Watching ${metadata}`, //The upper section of the presence text
  69. state: "Paused",
  70. };
  71. } else {
  72. // Only run this once
  73. if (videoState !== "playing") calculateEndTime();
  74. // Set presence to movie data
  75. presenceData = {
  76. largeImageKey:
  77. "https://cdn.rcd.gg/PreMiD/websites/V/VUDU/assets/0.png" /*The key (file name) of the Large Image on the presence. These are uploaded and named in the Rich Presence section of your application, called Art Assets*/,
  78. smallImageKey:
  79. "https://cdn.rcd.gg/PreMiD/websites/V/VUDU/assets/1.png" /*The key (file name) of the Large Image on the presence. These are uploaded and named in the Rich Presence section of your application, called Art Assets*/,
  80. smallImageText: "Watching movies", //The text which is displayed when hovering over the small image
  81. details: `Watching ${metadata}`, //The upper section of the presence text
  82. startTimestamp: startTime, //The unix epoch timestamp for when to start counting from
  83. endTimestamp: endTime, //If you want to show Time Left instead of Elapsed, this is the unix epoch timestamp at which the timer ends
  84. };
  85. }
  86. // Send that activity to discord
  87. presence.setActivity(presenceData);
  88. } else {
  89. // Clear activity
  90. presence.setActivity();
  91. }
  92. });