Comrade: Stack Bot for Zoom

Bot that manages stack for meetings. "Stack" puts you on stack and "Pop" drops the oldest person.

当前为 2021-01-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Comrade: Stack Bot for Zoom
  3. // @description Bot that manages stack for meetings. "Stack" puts you on stack and "Pop" drops the oldest person.
  4. // @version 1.0
  5. // @grant none
  6. // @include https://zoom.us/j/*
  7. // @include https://*.zoom.us/j/*
  8. // @include https://zoom.us/s/*
  9. // @include https://*.zoom.us/s/*
  10. // @include https://*.zoom.us/wc/*
  11. // @namespace https://greasyfork.org/users/22981
  12. // ==/UserScript==
  13.  
  14.  
  15. /*
  16. ANTI-CAPITALIST SOFTWARE LICENSE (v 1.4)
  17.  
  18. Copyright © 2021 Adam Novak
  19.  
  20. This is anti-capitalist software, released for free use by individuals and
  21. organizations that do not operate by capitalist principles.
  22.  
  23. Permission is hereby granted, free of charge, to any person or organization
  24. (the "User") obtaining a copy of this software and associated documentation
  25. files (the "Software"), to use, copy, modify, merge, distribute, and/or sell
  26. copies of the Software, subject to the following conditions:
  27.  
  28. 1. The above copyright notice and this permission notice shall be included in
  29. all copies or modified versions of the Software.
  30.  
  31. 2. The User is one of the following:
  32. a. An individual person, laboring for themselves
  33. b. A non-profit organization
  34. c. An educational institution
  35. d. An organization that seeks shared profit for all of its members, and
  36. allows non-members to set the cost of their labor
  37.  
  38. 3. If the User is an organization with owners, then all owners are workers and
  39. all workers are owners with equal equity and/or equal vote.
  40.  
  41. 4. If the User is an organization, then the User is not law enforcement or
  42. military, or working for or under either.
  43.  
  44. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY
  45. KIND, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  46. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
  47. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  48. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  49. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  50. */
  51.  
  52. //// CONFIG
  53.  
  54. const BOT_NAME = 'Comrade'
  55. const QUEUE_KEYWORD = 'stack'
  56. const DEQUEUE_KEYWORD = 'pop'
  57. const GIVEUP_KEYWORD = 'unstack'
  58. const REMIND_KEYWORD = 'who'
  59. const HELP_KEYWORD = 'help'
  60.  
  61. const HELP_TEXT = `
  62. ${BOT_NAME} is a bot who can stack. Type:
  63. 1. "${QUEUE_KEYWORD}" to put yourself on stack.
  64. 2. "${DEQUEUE_KEYWORD}" when you are done so the bot can announce who is next.
  65. 3. "${GIVEUP_KEYWORD}" if you are on stack but don't want to be.
  66. 4. "${REMIND_KEYWORD}" if you forgot who is on stack.
  67.  
  68. Type "${HELP_KEYWORD}" to see this message again.
  69. `
  70.  
  71. //// LIBRARY
  72.  
  73. // Let async code wait.
  74. // See: <https://stackoverflow.com/a/39914235>
  75. function sleep(ms) {
  76. return new Promise(resolve => setTimeout(resolve, ms));
  77. }
  78.  
  79. // Find a button by text. Returns the button element, or undefined.
  80. function findButton(text) {
  81. let all_buttons = document.getElementsByTagName('button')
  82. var this_button = undefined
  83. for (let button of all_buttons) {
  84. if (!this_button && button.innerText.includes(text)) {
  85. this_button = button
  86. }
  87. }
  88. return this_button
  89. }
  90.  
  91. //// ENSURE WEB CLIENT ACCESSIBLE
  92.  
  93. function showWebClientLink() {
  94. let results = document.getElementsByClassName('webclient')
  95. if (results[0]) {
  96. results[0].classList.remove('hideme')
  97. }
  98. }
  99.  
  100. // We may be on a non-meeting page. Make sure people can join.
  101. showWebClientLink()
  102.  
  103.  
  104. //// BOT
  105.  
  106. // Wait for the client to start, open necessary panes, and set name
  107. async function botStartup() {
  108. try {
  109. while (true) {
  110. // Wait until ready. We assume we are ready when the join audio button comes up
  111. console.log('Waiting for join audio button...')
  112. let audio_button = document.getElementsByClassName('join-audio-by-voip__join-btn')[0]
  113. if (audio_button && audio_button.offsetParent != null) {
  114. // It exists and is visible
  115. break
  116. }
  117. await sleep(1000)
  118. }
  119. console.log('Audio button visible')
  120. while (true) {
  121. // Wait for the chat and participants buttons
  122. // They may be in the bore button.
  123. console.log('Waiting for chat and participants buttons...')
  124. let more_button = document.getElementsByClassName('more-button')[0]
  125. let participants_button = findButton('Participants')
  126. let chat_button = findButton('Chat')
  127.  
  128. // TODO: look in more menu
  129. if (participants_button && chat_button) {
  130. // The buttons exist, so click them and move on
  131. participants_button.click()
  132. chat_button.click()
  133. break
  134. }
  135. // Otherwise try again
  136. await sleep(1000)
  137. }
  138. // We only need to rename sometimes
  139. var named_right = false;
  140. while (true) {
  141. // When the user list comes up, find and hover over ourselves
  142. console.log('Waiting for own participant entry...')
  143. // We are always at the top
  144. let user_entry = document.getElementById('participants-list-0')
  145. if (user_entry) {
  146. // The entry exists
  147. if (user_entry.innerText.startsWith(BOT_NAME)) {
  148. // No need to change name
  149. named_right = true;
  150. } else {
  151. // Hover user entry to create rename button
  152. // We can't hit the react hover button because it's hiding in a Symbol-named property or something.
  153. // This fake event doesn't always work...
  154. let event = new MouseEvent('mouseover', {
  155. 'view': window,
  156. 'bubbles': true,
  157. 'cancelable': true
  158. })
  159. user_entry.dispatchEvent(event)
  160. }
  161. break
  162. }
  163. // Otherwise try again
  164. await sleep(1000)
  165. }
  166. while (!named_right) {
  167. // When the user buttons come up, find and click the rename button
  168. console.log('Waiting for rename button...')
  169. let rename_button = findButton('Rename')
  170. if (rename_button) {
  171. // The button exists, so click it and move on
  172. rename_button.click()
  173. break
  174. }
  175. // Otherwise try again
  176. await sleep(1000)
  177. }
  178. while (!named_right) {
  179. // When the rename dialog comes up, enter our name and click save.
  180. console.log('Waiting for rename controls...')
  181. let new_name_field = document.getElementById('newname')
  182. let save_button = findButton('Save')
  183. if (new_name_field && save_button) {
  184. // The controls exist, so operate them and move on
  185. console.log('Setting name')
  186. new_name_field.value = BOT_NAME
  187. console.log('Sending change')
  188. let event = new Event('change', {
  189. 'view': window,
  190. 'bubbles': true,
  191. 'cancelable': true
  192. })
  193. new_name_field.dispatchEvent(event)
  194. save_button.click()
  195. named_right = true
  196. break
  197. }
  198. // Otherwise try again
  199. await sleep(1000)
  200. }
  201. // Now grab the chat log
  202. let chat_log = document.getElementsByClassName('chat-virtualized-list')[0]
  203. console.log('Chat log: ', chat_log)
  204. // Watch for chats
  205. let chat_watcher = new MutationObserver(chatChange)
  206. chat_watcher.observe(chat_log, {childList: true, subtree: true, characterDataOldValue: true})
  207. console.log('Watching with: ', chat_watcher)
  208. console.log(BOT_NAME + ' is ready.')
  209. await sleep(3000)
  210. say(BOT_NAME + ' is ready.')
  211. showHelp()
  212. } catch (e) {
  213. console.error('Comrade initialization error: ', e)
  214. }
  215. }
  216.  
  217. // Handle changes to the chat log and translate them into internal chat message calls
  218. function chatChange(mutations, chat_watcher) {
  219. try {
  220. for (let record of mutations) {
  221. console.log(record)
  222. // We may get new nodes, or changed text.
  223. if (record.type == 'characterData') {
  224. // New message from the same person as last time. Assume it is an append.
  225. let chat_entry = record.target.parentElement.parentElement
  226. let chat_sender_item = chat_entry.getElementsByClassName('chat-item__sender')[0]
  227. if (chat_sender_item) {
  228. let chat_sender = chat_sender_item.innerText
  229. // Trim off the old text and the intervening newline
  230. let chat_content = record.target.textContent.substr(record.oldValue.length + 1)
  231. console.log(chat_sender + ' also says: ' + chat_content)
  232. onChat(chat_sender, chat_content)
  233. }
  234. }
  235. for (let new_node of record.addedNodes) {
  236. let chat_sender_item = new_node.getElementsByClassName('chat-item__sender')[0]
  237. let chat_contant_item = new_node.getElementsByTagName('pre')[0]
  238. if (chat_sender_item && chat_content_item) {
  239. let chat_sender = chat_sender_item.innerText
  240. let chat_content = chat_contant_item.innerText
  241. if (chat_sender !== undefined && chat_content !== undefined) {
  242. console.log(chat_sender + ' says: ' + chat_content)
  243. onChat(chat_sender, chat_content)
  244. }
  245. }
  246. }
  247. }
  248. } catch (e) {
  249. console.error('Comrade element watch error: ', e)
  250. }
  251.  
  252. }
  253.  
  254. // Keep track of the meeting stack
  255. var stack = []
  256.  
  257. // Put a person on the stack, if not on stack already
  258. function addToStack(who) {
  259. if (stack.includes(who)) {
  260. say(who + ' is already on stack.')
  261. } else {
  262. stack.push(who)
  263. reportStack()
  264. }
  265. }
  266.  
  267. // Remove the oldest person from the stack
  268. function popFromStack() {
  269. removed = stack[0]
  270. stack = stack.slice(1)
  271. if (removed) {
  272. say('Removed ' + removed + ' from stack.')
  273. }
  274. reportStack()
  275. }
  276.  
  277. // Drop the given person from stack
  278. function removeFromStack(who) {
  279. let new_stack = []
  280. var removed = false
  281. for (let person of stack) {
  282. if (person != who) {
  283. new_stack.push(person)
  284. } else {
  285. removed = true
  286. }
  287. }
  288. stack = new_stack
  289. if (removed) {
  290. say('Removed ' + who + ' from stack')
  291. } else {
  292. say(who + 'was not on stack')
  293. }
  294. reportStack()
  295. }
  296.  
  297. // Read out the stack
  298. function reportStack() {
  299. if (stack.length == 0) {
  300. say('Stack is empty')
  301. } else {
  302. say('Next on stack is: ' + stack[0])
  303. if (stack.length > 1) {
  304. say('After that:')
  305. for (let i = 1; i < stack.length; i++) {
  306. say(i + '. ' + stack[i])
  307. }
  308. }
  309. }
  310. }
  311.  
  312. // Print the help text
  313. function showHelp() {
  314. say(HELP_TEXT)
  315. }
  316. function onChat(sender, message) {
  317. try {
  318. command = message.toLowerCase()
  319. if (command == 'ping') {
  320. say('pong')
  321. } else if (command == QUEUE_KEYWORD) {
  322. addToStack(sender)
  323. } else if (command == DEQUEUE_KEYWORD) {
  324. popFromStack()
  325. } else if (command == GIVEUP_KEYWORD) {
  326. removeFromStack(sender)
  327. } else if (command == REMIND_KEYWORD) {
  328. reportStack()
  329. } else if (command == HELP_KEYWORD) {
  330. showHelp()
  331. }
  332. } catch (e) {
  333. console.error('Comrade message interpretation error: ', e)
  334. }
  335. }
  336.  
  337. // Type in the chat.
  338. async function say(message) {
  339. try {
  340. console.log('Sending: ' + message)
  341. // Need to wait for React to settle from the user doing things
  342. await sleep(100)
  343. let chat_box = document.getElementsByClassName('chat-box__chat-textarea')[0]
  344. console.log('Chat box: ', chat_box)
  345. chat_box.value = message
  346. let change_event = new Event('change', {
  347. 'view': window,
  348. 'bubbles': true,
  349. 'cancelable': true
  350. })
  351. chat_box.dispatchEvent(change_event)
  352. // All the keyboard event properties are read only so we have to set them up front.
  353. let enter_event = new KeyboardEvent('keydown', {
  354. bubbles: true,
  355. cancelable: true,
  356. code: "Enter",
  357. key: "Enter",
  358. keyCode: 13,
  359. which: 13
  360. })
  361. chat_box.dispatchEvent(enter_event)
  362. } catch (e) {
  363. console.error('Comrade message transmission error: ', e)
  364. }
  365. }
  366.  
  367.  
  368. botStartup()
  369.  
  370.  
  371.  
  372.  
  373.