presence.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const presence = new Presence({
  2. clientId: "1310452380915077170",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. let data;
  6. presence.on("UpdateData", async () => {
  7. const presenceData: PresenceData = {
  8. largeImageKey:
  9. "https://cdn.rcd.gg/PreMiD/websites/L/LessWrong/assets/0.png",
  10. startTimestamp: browsingTimestamp,
  11. };
  12. if (document.location.pathname === "/") {
  13. presenceData.details = "Browsing LessWrong";
  14. presenceData.state = "at Homepage";
  15. } else if (document.location.pathname.startsWith("/posts/")) {
  16. data = document.location.pathname.split("/");
  17. const postTitle =
  18. document.querySelector(".PostsPageTitle-link")?.textContent ||
  19. document.querySelector(".PostsPage-secondSplashPageHeader")
  20. ?.textContent ||
  21. "Unknown Post";
  22. presenceData.details = `Reading ${data[1]}`;
  23. presenceData.state = postTitle;
  24. presenceData.buttons = [
  25. {
  26. label: "View Post",
  27. url: new URL(document.location.pathname, document.location.origin).href,
  28. },
  29. ];
  30. } else if (document.location.pathname.startsWith("/users/")) {
  31. data = document.location.pathname.split("/");
  32. presenceData.details = "Viewing User";
  33. presenceData.state = `${
  34. document.querySelector(".UsersProfile-usernameTitle")?.textContent ||
  35. "Loading User"
  36. }'s profile`;
  37. presenceData.buttons = [
  38. {
  39. label: "View Profile",
  40. url: new URL(document.location.pathname, document.location.origin).href,
  41. },
  42. ];
  43. } else presenceData.details = "Browsing LessWrong";
  44. if (presenceData.details) presence.setActivity(presenceData);
  45. else presence.setActivity();
  46. });