presence.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const presence = new Presence({
  2. clientId: "632936001269923880",
  3. }),
  4. pages: { [k: string]: string } = {
  5. "/elemental-by-medium": "Elemental by Medium",
  6. "/one-zero": "One Zero by Medium",
  7. "/gen": "Gen by Medium",
  8. "/zora": "ZORA by Medium",
  9. "/forge": "Forge by Medium",
  10. "/human-parts": "Humand Parts by Medium",
  11. "/marker": "Marker by Medium",
  12. "/topic/self": "Self Topics",
  13. "/topic/technology": "Technology Topics",
  14. "/heated": "Heated by Medium x Mark Bittman",
  15. "/modus": "Modus by Medium",
  16. "/topics": "Topics",
  17. "/me/stories/drafts": "My Stories",
  18. "/me/stories": "My Stories",
  19. "/me/series/drafts": "My Series",
  20. "/me/series": "My Series",
  21. "/me/stats": "My Stats",
  22. "/creators": "Creators",
  23. "/me/list/bookmarks": "Bookmarks",
  24. "/me/publications": "Publications",
  25. "/me/settings": "Settings",
  26. };
  27. presence.on("UpdateData", async () => {
  28. const page = document.location.pathname,
  29. title = document.querySelector("article div section div div div h1"),
  30. author =
  31. document.querySelector("div span div div a[href*=post_page---] p") ||
  32. document.querySelector("span div span a[href*=post_page---]"),
  33. [, ...hrefRest] = document.location.href.split("//"),
  34. href = hrefRest.join(""),
  35. presenceData: PresenceData = {
  36. largeImageKey:
  37. "https://cdn.rcd.gg/PreMiD/websites/M/Medium/assets/logo.png",
  38. startTimestamp: Math.floor(Date.now() / 1000),
  39. };
  40. if ((page && pages[page]) || (page && pages[page.slice(0, -1)])) {
  41. presenceData.details = "Viewing a page:";
  42. presenceData.state = pages[page] || pages[page.slice(0, -1)];
  43. } else if (
  44. href.includes("medium.com/@") &&
  45. href.match("[^/]*$")[0].includes("@") &&
  46. href.match("[^/]*$")[0] === href.slice(-href.match("[^/]*$")[0].length)
  47. ) {
  48. presenceData.details = "Viewing a profile:";
  49. presenceData.state =
  50. document.querySelector("div div div div h2")?.textContent ??
  51. "Unknown User";
  52. } else if (
  53. (title?.textContent && author?.textContent) ||
  54. (author?.textContent && document.title.includes(`${author?.textContent}-`))
  55. ) {
  56. presenceData.details =
  57. title?.textContent ||
  58. document.title.replace(`${author.textContent}-`, "");
  59. presenceData.state = `by ${author.textContent}`;
  60. presenceData.smallImageKey = Assets.Reading;
  61. presenceData.smallImageText = "Reading a story...";
  62. } else if (page.includes("/search")) {
  63. presenceData.details = "Searching for:";
  64. presenceData.state =
  65. new URLSearchParams(location.search).get("q") ?? "Something...";
  66. presenceData.smallImageKey = Assets.Search;
  67. } else {
  68. presenceData.details = "Viewing a page:";
  69. presenceData.state = "Home";
  70. }
  71. presence.setActivity(presenceData);
  72. });