presence.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const presence = new Presence({
  2. clientId: "937622209260826664",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. let microsoft = {
  6. OneNoteSubtopic: "Selecting a subtopic",
  7. OneNoteTitle: "Selecting a note",
  8. WordStatus: "Page ? of ?",
  9. PptCurrentSlide: "Slide ? of ?",
  10. ExcelActiveTab: "",
  11. };
  12. const enum Assets {
  13. Microsoft = "https://cdn.rcd.gg/PreMiD/websites/M/Microsoft%20365/assets/logo.png",
  14. }
  15. enum OtherAssets {
  16. Excel = "https://cdn.rcd.gg/PreMiD/websites/M/Microsoft%20365/assets/0.png",
  17. OneNote = "https://cdn.rcd.gg/PreMiD/websites/M/Microsoft%20365/assets/1.png",
  18. PowerPoint = "https://cdn.rcd.gg/PreMiD/websites/M/Microsoft%20365/assets/2.png",
  19. Word = "https://cdn.rcd.gg/PreMiD/websites/M/Microsoft%20365/assets/3.png",
  20. }
  21. presence.on(
  22. "iFrameData",
  23. (data: {
  24. OneNoteSubtopic: string;
  25. OneNoteTitle: string;
  26. WordStatus: string;
  27. PptCurrentSlide: string;
  28. ExcelActiveTab: string;
  29. }) => {
  30. microsoft = data;
  31. }
  32. );
  33. presence.on("UpdateData", async () => {
  34. const presenceData: PresenceData = {
  35. largeImageKey: Assets.Microsoft,
  36. startTimestamp: browsingTimestamp,
  37. },
  38. { pathname, hostname } = document.location,
  39. privacy = await presence.getSetting<boolean>("privacy"),
  40. appIcon = document.querySelector('link[rel~="icon"]')?.getAttribute("href");
  41. if (hostname === "www.office.com") {
  42. if (
  43. pathname.startsWith("/launch/") ||
  44. document.querySelector('[aria-pressed="true"]')
  45. ) {
  46. presenceData.largeImageKey =
  47. OtherAssets[
  48. document.querySelector('[aria-pressed="true"]')
  49. ?.textContent as keyof typeof OtherAssets
  50. ] ?? Assets.Microsoft;
  51. presenceData.details = `Browsing ${
  52. document.title.split("|")[0]
  53. } documents`;
  54. presenceData.smallImageKey = Assets.Reading;
  55. presenceData.smallImageText = "Browsing";
  56. } else if (pathname.startsWith("/search")) {
  57. presenceData.smallImageKey = Assets.Search;
  58. presenceData.smallImageText = "Searching";
  59. presenceData.details = "Searching...";
  60. if (!privacy) {
  61. presenceData.details = "Searching for:";
  62. presenceData.state = document.querySelector<HTMLInputElement>(
  63. "#ms-searchux-input-0"
  64. ).value;
  65. }
  66. }
  67. } else if (
  68. appIcon.match(/(word)|(onenote)|(microsoft365)|(ppt)|(excel)|(word)/gm)
  69. ) {
  70. const appName = appIcon
  71. .substring(appIcon.lastIndexOf("/") + 9)
  72. .replace(".ico", "")
  73. .toLowerCase()
  74. .replace("onenote", "OneNote")
  75. .replace("ppt", "PowerPoint")
  76. .replace("word", "Word")
  77. .replace("excel", "Excel");
  78. presenceData.largeImageKey =
  79. OtherAssets[appName as keyof typeof OtherAssets] ?? Assets.Microsoft;
  80. presenceData.smallImageKey = Assets.Writing;
  81. presenceData.smallImageText = "Editing";
  82. if (appName === "OneNote") {
  83. presenceData.details = "Editing a Note";
  84. if (!privacy) {
  85. presenceData.details = `Editing ${microsoft.OneNoteTitle}`;
  86. presenceData.state = `in ${microsoft.OneNoteSubtopic}`;
  87. }
  88. } else if (
  89. appName === "Word" ||
  90. appName === "Excel" ||
  91. appName === "PowerPoint"
  92. ) {
  93. presenceData.details = `Editing ${appName
  94. .replace("Word", "a Word")
  95. .replace("Excel", "an Excel")
  96. .replace("PowerPoint", "a PowerPoint")} document`;
  97. if (!privacy) {
  98. presenceData.details = `Editing ${
  99. document.querySelector('[name="fileName"]')?.getAttribute("value") ??
  100. document.querySelector("title")?.textContent?.split(".")[0]
  101. }`;
  102. if (appName === "Word") presenceData.state = microsoft.WordStatus;
  103. else if (appName === "PowerPoint")
  104. presenceData.state = microsoft.PptCurrentSlide;
  105. else if (appName === "Excel" && !microsoft.ExcelActiveTab)
  106. presenceData.state = microsoft.ExcelActiveTab;
  107. }
  108. }
  109. }
  110. if (presenceData.details) presence.setActivity(presenceData);
  111. });