presence.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const presence = new Presence({
  2. clientId: "1216150866122244096",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. const enum Assets {
  6. Logo = "https://cdn.rcd.gg/PreMiD/websites/U/uwowocosplay/assets/logo.jpeg",
  7. }
  8. presence.on("UpdateData", async () => {
  9. const presenceData: PresenceData = {
  10. details: "Uwowo Cosplay Homepage",
  11. largeImageKey: Assets.Logo,
  12. startTimestamp: browsingTimestamp,
  13. },
  14. { pathname, href } = document.location;
  15. switch (pathname) {
  16. case "/": {
  17. presenceData.details = "Browsing the Homepage";
  18. break;
  19. }
  20. case "/en-de": {
  21. presenceData.details = "Browsing the Homepage";
  22. break;
  23. }
  24. case "/collections/shop-all": {
  25. presenceData.details = "Browsing the shop";
  26. break;
  27. }
  28. case "/en-de/collections/shop-all": {
  29. presenceData.details = "Browsing the shop";
  30. break;
  31. }
  32. case "/cart": {
  33. presenceData.details = "Viewing their cart";
  34. break;
  35. }
  36. case "/account": {
  37. presenceData.details = "Viewing their account";
  38. break;
  39. }
  40. default: {
  41. presenceData.details = getStyle(pathname);
  42. break;
  43. }
  44. }
  45. if (
  46. pathname.startsWith("/products/") ||
  47. pathname.startsWith("/en-de/products/")
  48. )
  49. setProduct(presenceData, href);
  50. presence.setActivity(presenceData);
  51. });
  52. function getStyle(pathname: string) {
  53. const path = pathname.split("/collections/")[1];
  54. if (path !== null) {
  55. return `Browsing the ${path} Collection`
  56. .split("-")
  57. .join(" ")
  58. .replace("Collections", "")
  59. .trim();
  60. } else return "Unknown Page";
  61. }
  62. function setProduct(presenceData: PresenceData, href: string) {
  63. const product = document
  64. .querySelector(".h2.product-single__title")
  65. ?.textContent?.replace(/.*Uwowo/, "")
  66. ?.replace(/Costume/g, "");
  67. presenceData.details = `Viewing ${
  68. product
  69. ?.split(" ")
  70. ?.slice(0, product?.split(" ").length / 2)
  71. ?.join(" ") ?? "Unknown product"
  72. }`;
  73. presenceData.state =
  74. product
  75. ?.split(" ")
  76. ?.slice(product?.split(" ")?.length / 2)
  77. ?.join(" ") ?? "";
  78. presenceData.buttons = [
  79. {
  80. label: "View Cosplay",
  81. url: href,
  82. },
  83. ];
  84. presenceData.largeImageKey =
  85. document
  86. .querySelector<HTMLMetaElement>('[property="og:image"]')
  87. ?.content?.split("?")?.[0] ?? Assets.Logo;
  88. if (presenceData.largeImageKey !== Assets.Logo)
  89. presenceData.smallImageKey = Assets.Logo;
  90. }