presence.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const presence = new Presence({
  2. clientId: "863173597941727282",
  3. }),
  4. browsingTimestamp = Math.floor(Date.now() / 1000);
  5. let productName, productBrand, blogTitle, blogAuthor;
  6. presence.on("UpdateData", async () => {
  7. const presenceData: PresenceData = {
  8. largeImageKey:
  9. "https://cdn.rcd.gg/PreMiD/websites/L/Lime%20Pro%20Gaming/assets/logo.png",
  10. startTimestamp: browsingTimestamp,
  11. };
  12. if (window.location.pathname === "/")
  13. presenceData.details = "Browsing the Home Page";
  14. if (window.location.pathname.includes("/collections")) {
  15. if (window.location.pathname.includes("/products")) {
  16. productName = document.querySelector(
  17. "#shopify-section-product-template > div:nth-child(1) > div.container.container-fluid-mobile > div > div:nth-child(2) > div > h1"
  18. ).textContent;
  19. productBrand = document.querySelector(
  20. "#same_product_height > div.tt-breadcrumb > div > ul > li:nth-child(2) > a"
  21. ).textContent;
  22. presenceData.details = productName;
  23. presenceData.state = productBrand;
  24. presenceData.buttons = [
  25. { label: "View Product", url: document.location.href },
  26. {
  27. label: "View Category",
  28. url:
  29. document.location.origin +
  30. document
  31. .querySelector(
  32. "#same_product_height > div.tt-breadcrumb > div > ul > li:nth-child(2) > a"
  33. )
  34. .getAttribute("href"),
  35. },
  36. ];
  37. } else if (
  38. window.location.pathname === "/collections" ||
  39. window.location.pathname === "/collections/"
  40. ) {
  41. presenceData.details = "Viewing Brands";
  42. presenceData.state = document.querySelector(
  43. "#shopify-section-list-collections-template > div > div > div.tt-block-title > div"
  44. ).textContent;
  45. } else {
  46. presenceData.details = `Viewing: ${
  47. document.querySelector(
  48. "#same_product_height > div.tt-breadcrumb > div > ul > li:nth-child(2)"
  49. ).textContent
  50. }`;
  51. presenceData.state = `${
  52. document.querySelector(
  53. "#usf_container > div.usf-sr-container.usf-nosearch > div.tt-filters-options > h1 > span > b"
  54. ).textContent
  55. } Products`;
  56. }
  57. } else if (window.location.pathname.includes("/products/")) {
  58. productName = document.querySelector(
  59. "#shopify-section-product-template > div:nth-child(1) > div.container.container-fluid-mobile > div > div:nth-child(2) > div > h1"
  60. ).textContent;
  61. presenceData.details = productName;
  62. presenceData.buttons = [
  63. { label: "View Product", url: document.location.href },
  64. ];
  65. } else if (window.location.pathname.includes("/pages")) {
  66. presenceData.details = `Viewing: ${
  67. document.querySelector(
  68. "#same_product_height > div.tt-breadcrumb > div > ul > li:nth-child(2)"
  69. ).textContent
  70. }`;
  71. presenceData.buttons = [
  72. {
  73. label: "View Page",
  74. url: document.location.href,
  75. },
  76. ];
  77. } else if (window.location.pathname.includes("/search")) {
  78. presenceData.details = `Searching: ${
  79. document.querySelector(
  80. "#usf_container > div.usf-sr-container > div.tt-filters-options > h1 > span > b:nth-child(2)"
  81. ).textContent
  82. }`;
  83. presenceData.state = `${
  84. document.querySelector(
  85. "#usf_container > div.usf-sr-container > div.tt-filters-options > h1 > span > b:nth-child(1)"
  86. ).textContent
  87. } Results`;
  88. presenceData.smallImageKey = Assets.Search;
  89. } else {
  90. switch (window.location.pathname) {
  91. case "/cart": {
  92. presenceData.details = "Viewing cart";
  93. break;
  94. }
  95. case "/account": {
  96. presenceData.details = "Viewing account";
  97. break;
  98. }
  99. case "/apps/subscriptions": {
  100. presenceData.details = "Viewing subscriptions";
  101. break;
  102. }
  103. default:
  104. if (window.location.pathname.includes("/checkouts/"))
  105. presenceData.details = "Ordering";
  106. else if (window.location.pathname.includes("/blogs/")) {
  107. blogTitle =
  108. document.querySelector(
  109. "#shopify-section-article-template > div:nth-child(1) > div > div > div > div > h1"
  110. )?.textContent || "Viewing News";
  111. blogAuthor =
  112. document.querySelector(
  113. "#shopify-section-article-template > div:nth-child(1) > div > div > div > div > div.tt-autor"
  114. )?.textContent || null;
  115. presenceData.details = blogTitle;
  116. if (blogAuthor) presenceData.state = blogAuthor;
  117. if (blogAuthor) presenceData.smallImageKey = Assets.Reading;
  118. } else presence.setActivity();
  119. }
  120. }
  121. presence.setActivity(presenceData);
  122. });