presence.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Application ID on Discord developer page
  2. const presence = new Presence({
  3. clientId: '719373053028728894',
  4. })
  5. // time spent on current URL
  6. const timeElapsed = Math.floor(Date.now() / 1000)
  7. presence.on('UpdateData', async () => {
  8. // default settings
  9. const presenceData: PresenceData = {
  10. details: document.title,
  11. largeImageKey: 'https://cdn.rcd.gg/PreMiD/websites/L/LeetCode/assets/logo.png',
  12. startTimestamp: timeElapsed,
  13. }
  14. // edit attributes based on path
  15. if (document.location.pathname === '/') {
  16. presenceData.details = 'Homepage'
  17. }
  18. else if (document.location.pathname.startsWith('/problemset')) {
  19. presenceData.details = 'Viewing Problems'
  20. }
  21. else if (document.location.pathname.startsWith('/problems')) {
  22. // Read problem name based on title, HTML is too messy to parse
  23. presenceData.details = document.title.slice(0, -10)
  24. }
  25. else if (document.location.pathname.startsWith('/explore')) {
  26. presenceData.details = 'Explore'
  27. if (document.querySelectorAll('.question-title')) {
  28. presenceData.state = document.querySelectorAll('.question-title')[0]?.textContent
  29. }
  30. }
  31. else if (document.location.pathname.startsWith('/contest')) {
  32. presenceData.details = 'In a Contest'
  33. }
  34. else if (document.location.pathname.startsWith('/articles')) {
  35. presenceData.details = 'Reading Solutions'
  36. }
  37. else if (document.location.pathname.startsWith('/discuss')) {
  38. presenceData.details = 'Browsing Forums'
  39. }
  40. else if (document.location.pathname.startsWith('/interview')) {
  41. presenceData.details = 'Mock Interviewing'
  42. }
  43. presence.setActivity(presenceData)
  44. })