presence.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { Assets } from 'premid'
  2. const presence = new Presence({
  3. clientId: '673322920809988120',
  4. })
  5. function parseQueryString(queryString?: string): { [key: string]: string } {
  6. queryString ??= window.location.search.substring(1)
  7. const params: { [key: string]: string } = {}
  8. const queries = queryString.split('&')
  9. for (const indexQuery of queries) {
  10. const indexPair = indexQuery.split('=')
  11. params[decodeURIComponent(indexPair[0]!)] = decodeURIComponent(
  12. indexPair.length > 1 ? indexPair[1]! : '',
  13. )
  14. }
  15. return params
  16. }
  17. enum ActivityAssets {
  18. Logo = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/logo.png',
  19. Ctf = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/0.png',
  20. Chall = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/1.png',
  21. Commu = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/2.png',
  22. Docu = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/3.png',
  23. Infos = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/4.png',
  24. Tools = 'https://cdn.rcd.gg/PreMiD/websites/R/RootMe/assets/5.png',
  25. }
  26. presence.on('UpdateData', async () => {
  27. const presenceData: PresenceData = {
  28. largeImageKey: ActivityAssets.Logo,
  29. }
  30. const route = document.location.pathname.split('/')
  31. if (document.location.pathname === '/') {
  32. presenceData.details = 'Home'
  33. if (!parseQueryString(document.location.hash).page) {
  34. presenceData.state = 'Watching home page'
  35. }
  36. else if (parseQueryString(document.location.hash).page === 'news') {
  37. presenceData.state = parseQueryString(document.location.hash).inc
  38. ? `Watching ${parseQueryString(document.location.hash).page} page (${
  39. document.querySelector('dl.tabs > dd.active')?.textContent
  40. } )`
  41. : `Watching ${parseQueryString(document.location.hash).page} page`
  42. }
  43. else if (parseQueryString(document.location.hash).page === 'structure') {
  44. if (
  45. parseQueryString(document.location.hash).inc === 'inclusions/services'
  46. ) {
  47. presenceData.state = document.querySelector('.row > h1')?.textContent
  48. }
  49. else {
  50. presenceData.state = document.querySelector('.ajaxbloc > h1')?.textContent
  51. }
  52. }
  53. else if (parseQueryString(document.location.hash).page === 'contact') {
  54. presenceData.state = document.querySelector('.t-body > h1')?.textContent
  55. }
  56. else if (parseQueryString(document.location.hash).page === 'plan') {
  57. presenceData.state = document.querySelector('.t-body > h1')?.textContent
  58. }
  59. else if (parseQueryString(document.location.hash).page === 'faq') {
  60. presenceData.state = document.querySelector('h1.crayon')?.textContent
  61. }
  62. presenceData.smallImageKey = Assets.Reading
  63. switch (
  64. document.querySelector('img.grayscale')?.getAttribute('alt')
  65. || document.querySelector('img.grayscale')?.getAttribute('title')
  66. ) {
  67. case 'fr':
  68. presenceData.smallImageText = 'Language : français'
  69. break
  70. case 'es':
  71. presenceData.smallImageText = 'Language : espanol'
  72. break
  73. case 'de':
  74. presenceData.smallImageText = 'Language : deutsch'
  75. break
  76. case 'en':
  77. presenceData.smallImageText = 'Language : english'
  78. break
  79. default:
  80. break
  81. }
  82. }
  83. else if (document.location.pathname.includes('/Challenges/')) {
  84. presenceData.smallImageKey = ActivityAssets.Chall
  85. presenceData.smallImageText = 'Challenges'
  86. presenceData.details = route[3]
  87. ? `${route[2]} - ${route[3].replaceAll('-', ' ')}`
  88. : `${route[2]}`
  89. presenceData.state = !route[4]
  90. ? 'Navigating...'
  91. : document.querySelector('.crayon')?.textContent
  92. }
  93. else if (document.location.pathname.includes('/Capture-The-Flag/')) {
  94. presenceData.smallImageKey = ActivityAssets.Ctf
  95. presenceData.smallImageText = 'Capture The Flag'
  96. presenceData.details = route[3]
  97. ? `${route[2]?.replaceAll('-', ' ')} - ${route[3].replaceAll('-', ' ')}`
  98. : route[2]?.replaceAll('-', ' ')
  99. presenceData.state = 'Navigating...'
  100. }
  101. else if (
  102. document.location.pathname.includes('/Communaute/')
  103. || document.location.pathname.includes('/Comunidad/')
  104. || document.location.pathname.includes('/Community/')
  105. ) {
  106. presenceData.smallImageKey = ActivityAssets.Commu
  107. presenceData.smallImageText = 'Communaute'
  108. presenceData.details = route[3]
  109. ? `${route[2]} - ${route[3].replaceAll('-', ' ')}`
  110. : route[2]
  111. presenceData.state = 'Navigating...'
  112. }
  113. else if (
  114. document.location.pathname.includes('/Documentation/')
  115. || document.location.pathname.includes('/Materialien/')
  116. || document.location.pathname.includes('/Documentacion/')
  117. || document.location.pathname.includes('/Docs/')
  118. ) {
  119. presenceData.smallImageKey = ActivityAssets.Docu
  120. presenceData.smallImageText = 'Documentation'
  121. if (route[3] !== 'Reseaux') {
  122. presenceData.details = route[3]
  123. ? `${route[2]} - ${route[3].replaceAll('-', ' ')}`
  124. : `${route[2]}`
  125. presenceData.state = !route[4]
  126. ? 'Navigating...'
  127. : route[4].replaceAll('-', ' ')
  128. }
  129. else {
  130. presenceData.details = route[4]
  131. ? `${route[2]} - ${route[3]} > ${route[4]}`
  132. : `${route[2]} - ${route[3]}`
  133. presenceData.state = !route[5]
  134. ? 'Navigating...'
  135. : route[5].replaceAll('-', ' ')
  136. }
  137. }
  138. else if (
  139. document.location.pathname.includes('/Informations/')
  140. || document.location.pathname.includes('/Information/')
  141. || document.location.pathname.includes('/Info/')
  142. ) {
  143. presenceData.smallImageKey = ActivityAssets.Infos
  144. presenceData.smallImageText = 'Informations'
  145. presenceData.details = `${route[2]}`
  146. presenceData.state = !route[3]
  147. ? 'Navigating...'
  148. : document.querySelector('.crayon')?.textContent
  149. }
  150. else if (
  151. document.location.pathname.includes('/Tools/')
  152. || document.location.pathname.includes('/Herramientas/')
  153. || document.location.pathname.includes('/Outils/')
  154. ) {
  155. presenceData.smallImageKey = ActivityAssets.Tools
  156. presenceData.smallImageText = 'Tools'
  157. presenceData.details = route[3]
  158. ? `${route[2]} - ${route[3].replaceAll('-', ' ')}`
  159. : `${route[2]}`
  160. presenceData.state = route[4] ?? 'Navigating...'
  161. }
  162. else {
  163. presenceData.details = `Watching member : ${
  164. document.querySelector('span.forum')?.textContent
  165. }`
  166. presenceData.state = parseQueryString(document.location.hash).inc
  167. ? `Page : ${parseQueryString(document.location.hash).inc}`
  168. : 'Page : profil'
  169. }
  170. if (!presenceData.details) {
  171. presence.setActivity()
  172. }
  173. else {
  174. if (!presenceData.state)
  175. presenceData.state = 'Navigating...'
  176. presence.setActivity(presenceData)
  177. }
  178. })