presence.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const presence = new Presence({
  2. clientId: "651135297756856339",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. let title;
  6. const actionURL = new URL(document.location.href),
  7. title2URL = new URL(document.location.href);
  8. presence.on("UpdateData", async () => {
  9. const presenceData: PresenceData = {
  10. largeImageKey:
  11. "https://cdn.rcd.gg/PreMiD/websites/T/Touhou%20Wiki/assets/logo.png",
  12. };
  13. title = document.querySelector("h1#firstHeading");
  14. const actionResult = actionURL.searchParams.get("action"),
  15. title2Result = title2URL.searchParams.get("title");
  16. if (
  17. document.location.pathname === "/wiki/Touhou_Wiki" ||
  18. document.location.pathname === "/wiki/Заглавная_страница" ||
  19. document.location.pathname === "/wiki/东方维基" ||
  20. document.location.pathname === "/wiki/동방위키:대문"
  21. ) {
  22. presenceData.state = "Main Page | Home";
  23. presenceData.startTimestamp = browsingTimestamp;
  24. } else if (title && document.location.pathname.includes("/wiki/")) {
  25. presenceData.details = "Reading about:";
  26. presenceData.state = title.textContent;
  27. presenceData.startTimestamp = browsingTimestamp;
  28. } else if (
  29. actionResult === "history" &&
  30. title2Result &&
  31. document.location.pathname.includes("/w/")
  32. ) {
  33. presenceData.details = "Viewing revision history of:";
  34. if (title2Result.includes("_"))
  35. presenceData.state = title2Result.replaceAll("_", " ");
  36. else presenceData.state = title2Result;
  37. presenceData.startTimestamp = browsingTimestamp;
  38. } else if (
  39. actionResult === "edit" &&
  40. title2Result &&
  41. document.location.pathname.includes("/w/")
  42. ) {
  43. presenceData.details = "Editing a page:";
  44. if (title2Result.includes("_"))
  45. presenceData.state = title2Result.replaceAll("_", " ");
  46. else presenceData.state = title2Result;
  47. presenceData.startTimestamp = browsingTimestamp;
  48. }
  49. if (presenceData.details) presence.setActivity(presenceData);
  50. else presence.setActivity();
  51. });