presence.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. const presence = new Presence({
  2. clientId: "684174415415476240",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. const enum Assets {
  6. Logo = "https://cdn.rcd.gg/PreMiD/websites/N/Niantic%20Wayfarer/assets/logo.png",
  7. Pin = "https://cdn.rcd.gg/PreMiD/websites/N/Niantic%20Wayfarer/assets/0.png",
  8. }
  9. const shortenedURLs: Record<string, string> = {};
  10. async function getShortURL(url: string) {
  11. if (url.length < 256) return url;
  12. if (shortenedURLs[url]) return shortenedURLs[url];
  13. try {
  14. shortenedURLs[url] = Assets.Logo;
  15. const pdURL = await (
  16. await fetch(`https://pd.premid.app/create/${url}`)
  17. ).text();
  18. shortenedURLs[url] = pdURL;
  19. return pdURL;
  20. } catch (err) {
  21. presence.error(err);
  22. return url;
  23. }
  24. }
  25. presence.on("UpdateData", async () => {
  26. const presenceData: PresenceData = {
  27. largeImageKey: Assets.Logo,
  28. startTimestamp: browsingTimestamp,
  29. },
  30. { pathname } = document.location;
  31. if (pathname.includes("/review")) {
  32. const title = document.querySelector(
  33. "app-title-and-description-b .wf-review-card__body a > div"
  34. ),
  35. description = document.querySelector(
  36. "app-title-and-description-b .wf-review-card__body a+div"
  37. ),
  38. location = document.querySelector(
  39. "app-photo-b .wf-review-card__body > div > div:last-child"
  40. );
  41. if (title && description && location) {
  42. presenceData.largeImageKey = await getShortURL(
  43. document.querySelector<HTMLImageElement>(
  44. "app-photo-b .wf-image-modal > img"
  45. ).src
  46. );
  47. presenceData.smallImageKey = Assets.Pin;
  48. presenceData.details = `Reviewing: ${title.childNodes[0].textContent.trim()}`;
  49. presenceData.state = `Description: ${description.textContent.trim()}`;
  50. presenceData.smallImageText = `Address: ${location.textContent
  51. .split(":")[1]
  52. .trim()}`;
  53. } else if (document.querySelector("app-review-photo")) {
  54. presenceData.details = "Reviewing photos";
  55. presenceData.state = `for ${
  56. document.querySelector(
  57. "app-review-photo .review-photo__info > div > div:first-child"
  58. ).textContent
  59. }`;
  60. } else if (document.querySelector("app-review-edit")) {
  61. presenceData.details = "Reviewing a Wayspot edit";
  62. presenceData.state = `for ${document
  63. .querySelector("app-review-edit-info .review-edit-info__info")
  64. .textContent.trim()}`;
  65. } else {
  66. presenceData.details = "Getting ready to";
  67. presenceData.state = "review a location";
  68. }
  69. } else if (pathname.includes("/settings"))
  70. presenceData.details = "Managing settings";
  71. else if (pathname.includes("/help")) {
  72. const article = document
  73. .querySelector("wf-page-header h2 a + span")
  74. ?.textContent.split(">")[1]
  75. .trim();
  76. presenceData.smallImageKey = Assets.Reading;
  77. if (article) {
  78. presenceData.details = "Reading article:";
  79. presenceData.state = article;
  80. } else presenceData.details = "Browsing the Help Center";
  81. } else if (pathname.includes("/criteria")) {
  82. presenceData.details = "Reading criteria";
  83. presenceData.state = document
  84. .querySelector("wf-subnavigation li li[class*=selected]")
  85. .textContent.trim();
  86. } else if (pathname.includes("/login")) presenceData.details = "Logging in";
  87. else if (pathname.includes("/profile")) {
  88. presenceData.details = "Viewing their profile";
  89. presenceData.state = `Rating: ${document
  90. .querySelector("wf-rating-bar section[class*=active]")
  91. .textContent.trim()}`;
  92. presenceData.smallImageKey = await getShortURL(
  93. document.querySelector<SVGImageElement>("wf-upgrade-visualization image")
  94. .href.baseVal
  95. );
  96. const agreements = [
  97. ...document.querySelectorAll(
  98. "wf-profile-stats > div > div:not([class]):not(:last-child) .wf-profile-stats__stat"
  99. ),
  100. ].reduce((value, element) => {
  101. return value + +element.children[1].textContent.trim();
  102. }, 0),
  103. total = +document
  104. .querySelector(
  105. "wf-profile-stats > div > .wf-profile-stats__stat > div:last-child"
  106. )
  107. .textContent.trim();
  108. presenceData.smallImageText = `Total: ${total}, Agreements: ${agreements} (${(
  109. (agreements / total) *
  110. 100
  111. ).toFixed(1)}%)`;
  112. } else if (pathname.includes("/nominations"))
  113. presenceData.details = "Viewing their nominations";
  114. else if (pathname.includes("/showcase"))
  115. presenceData.details = "Viewing the showcased wayspots";
  116. if (presenceData.details) presence.setActivity(presenceData);
  117. else presence.setActivity();
  118. });