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.1
  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. // How many pixels can we be scrolled from the bottom and still think all
  72. // messages we see are the latest ones?
  73. const END_OF_HISTORY_HEIGHT = 10
  74.  
  75. //// LIBRARY
  76.  
  77. // Let async code wait.
  78. // See: <https://stackoverflow.com/a/39914235>
  79. function sleep(ms) {
  80. return new Promise(resolve => setTimeout(resolve, ms));
  81. }
  82.  
  83. // Find a button by text. Returns the button element, or undefined.
  84. function findButton(text) {
  85. let all_buttons = document.getElementsByTagName('button')
  86. var this_button = undefined
  87. for (let button of all_buttons) {
  88. if (!this_button && button.innerText.includes(text)) {
  89. this_button = button
  90. }
  91. }
  92. return this_button
  93. }
  94.  
  95. //// ENSURE WEB CLIENT ACCESSIBLE
  96.  
  97. function showWebClientLink() {
  98. let results = document.getElementsByClassName('webclient')
  99. if (results[0]) {
  100. results[0].classList.remove('hideme')
  101. }
  102. }
  103.  
  104. // We may be on a non-meeting page. Make sure people can join.
  105. showWebClientLink()
  106.  
  107.  
  108. //// BOT
  109.  
  110. // Wait for the client to start, open necessary panes, and set name
  111. async function botStartup() {
  112. try {
  113. while (true) {
  114. // Wait until ready. We assume we are ready when the join audio button comes up
  115. console.log('Waiting for join audio button...')
  116. let audio_button = document.getElementsByClassName('join-audio-by-voip__join-btn')[0]
  117. if (audio_button && audio_button.offsetParent != null) {
  118. // It exists and is visible
  119. break
  120. }
  121. await sleep(1000)
  122. }
  123. console.log('Audio button visible')
  124. while (true) {
  125. // Wait for the chat and participants buttons
  126. // They may be in the bore button.
  127. console.log('Waiting for chat and participants buttons...')
  128. let more_button = document.getElementsByClassName('more-button')[0]
  129. let participants_button = findButton('Participants')
  130. let chat_button = findButton('Chat')
  131.  
  132. // TODO: look in more menu
  133. if (participants_button && chat_button) {
  134. // The buttons exist, so click them and move on
  135. participants_button.click()
  136. chat_button.click()
  137. break
  138. }
  139. // Otherwise try again
  140. await sleep(1000)
  141. }
  142. // We only need to rename sometimes
  143. var named_right = false;
  144. while (true) {
  145. // When the user list comes up, find and hover over ourselves
  146. console.log('Waiting for own participant entry...')
  147. // We are always at the top
  148. let user_entry = document.getElementById('participants-list-0')
  149. if (user_entry) {
  150. // The entry exists
  151. if (user_entry.innerText.startsWith(BOT_NAME)) {
  152. // No need to change name
  153. named_right = true;
  154. } else {
  155. // Hover user entry to create rename button
  156. // We can't hit the react hover button because it's hiding in a Symbol-named property or something.
  157. // This fake event doesn't always work...
  158. let event = new MouseEvent('mouseover', {
  159. 'view': window,
  160. 'bubbles': true,
  161. 'cancelable': true
  162. })
  163. user_entry.dispatchEvent(event)
  164. }
  165. break
  166. }
  167. // Otherwise try again
  168. await sleep(1000)
  169. }
  170. while (!named_right) {
  171. // When the user buttons come up, find and click the rename button
  172. console.log('Waiting for rename button...')
  173. let rename_button = findButton('Rename')
  174. if (rename_button) {
  175. // The button exists, so click it and move on
  176. rename_button.click()
  177. break
  178. }
  179. // Otherwise try again
  180. await sleep(1000)
  181. }
  182. while (!named_right) {
  183. // When the rename dialog comes up, enter our name and click save.
  184. console.log('Waiting for rename controls...')
  185. let new_name_field = document.getElementById('newname')
  186. let save_button = findButton('Save')
  187. if (new_name_field && save_button) {
  188. // The controls exist, so operate them and move on
  189. console.log('Setting name')
  190. new_name_field.value = BOT_NAME
  191. console.log('Sending change')
  192. let event = new Event('change', {
  193. 'view': window,
  194. 'bubbles': true,
  195. 'cancelable': true
  196. })
  197. new_name_field.dispatchEvent(event)
  198. save_button.click()
  199. named_right = true
  200. break
  201. }
  202. // Otherwise try again
  203. await sleep(1000)
  204. }
  205. // Now grab the chat log
  206. let chat_log = document.getElementsByClassName('chat-virtualized-list')[0]
  207. console.log('Chat log: ', chat_log)
  208. // Watch for chats
  209. let chat_watcher = new MutationObserver(chatChange)
  210. chat_watcher.observe(chat_log, {childList: true, subtree: true, characterDataOldValue: true})
  211. console.log('Watching with: ', chat_watcher)
  212. console.log(BOT_NAME + ' is ready.')
  213. await sleep(3000)
  214. say(BOT_NAME + ' is ready.')
  215. showHelp()
  216. } catch (e) {
  217. console.error('Comrade initialization error: ', e)
  218. }
  219. }
  220.  
  221. // Handle changes to the chat log and translate them into internal chat message calls
  222. function chatChange(mutations, chat_watcher) {
  223. try {
  224. // Find the chat scroll and see where it is scrolled to
  225. let chat_scroller = document.getElementsByClassName('chat-virtualized-list')[0]
  226. for (let record of mutations) {
  227. if ((record.addedNodes.length == 0 && record.type != 'characterData') || (record.addedNodes.length == 1 && record.addedNodes[0].classList.contains('chat-item__chat-info-time-stamp'))) {
  228. // These are noise
  229. continue
  230. }
  231. console.log(record)
  232. if (record.nextSibling) {
  233. // Zoom pages the chat messages in and out as we scroll the chat, and also when it feels like.
  234. // We can skip most of them here, but they will be re-seen if we are scrolling up and down.
  235. continue
  236. }
  237. // We may get new nodes, or changed text.
  238. if (record.type == 'characterData') {
  239. // New message from the same person as last time. Assume it is an append.
  240. let chat_entry = record.target.parentElement.parentElement
  241. let chat_sender_item = chat_entry.getElementsByClassName('chat-item__sender')[0]
  242. if (chat_sender_item) {
  243. let chat_sender = chat_sender_item.innerText
  244. // Trim off the old text and the intervening newline
  245. let chat_content = record.target.textContent.substr(record.oldValue.length + 1)
  246. console.log(chat_sender + ' also says: ' + chat_content)
  247. onChat(chat_sender, chat_content)
  248. }
  249. }
  250. if (chat_scroller && chat_scroller.scrollTopMax - chat_scroller.scrollTop > END_OF_HISTORY_HEIGHT) {
  251. // We are probably scrolling around the list and not getting new messages.
  252. console.log('Skipping new nodes as we are not at the end of history: ' + chat_scroller.scrollTop + '/' + chat_scroller.scrollTopMax)
  253. continue
  254. }
  255. for (let new_node of record.addedNodes) {
  256. let chat_sender_item = new_node.getElementsByClassName('chat-item__sender')[0]
  257. let chat_content_item = new_node.getElementsByTagName('pre')[0]
  258. if (chat_sender_item && chat_content_item) {
  259. let chat_sender = chat_sender_item.innerText
  260. let chat_content = chat_content_item.innerText
  261. if (chat_sender !== undefined && chat_content !== undefined) {
  262. console.log(chat_sender + ' says: ' + chat_content)
  263. onChat(chat_sender, chat_content)
  264. }
  265. }
  266. }
  267. }
  268. } catch (e) {
  269. console.error('Comrade element watch error: ', e)
  270. }
  271.  
  272. }
  273.  
  274. // Keep track of the meeting stack
  275. var stack = []
  276.  
  277. // Put a person on the stack, if not on stack already
  278. function addToStack(who) {
  279. if (stack.includes(who)) {
  280. say(who + ' is already on stack.')
  281. } else {
  282. stack.push(who)
  283. reportStack()
  284. }
  285. }
  286.  
  287. // Remove the oldest person from the stack
  288. function popFromStack() {
  289. removed = stack[0]
  290. stack = stack.slice(1)
  291. if (removed) {
  292. say('Removed ' + removed + ' from stack.')
  293. }
  294. reportStack()
  295. }
  296.  
  297. // Drop the given person from stack
  298. function removeFromStack(who) {
  299. let new_stack = []
  300. var removed = false
  301. for (let person of stack) {
  302. if (person != who) {
  303. new_stack.push(person)
  304. } else {
  305. removed = true
  306. }
  307. }
  308. stack = new_stack
  309. if (removed) {
  310. say('Removed ' + who + ' from stack')
  311. } else {
  312. say(who + 'was not on stack')
  313. }
  314. reportStack()
  315. }
  316.  
  317. // Read out the stack
  318. function reportStack() {
  319. if (stack.length == 0) {
  320. say('Stack is empty')
  321. } else {
  322. say('Next on stack is: ' + stack[0])
  323. if (stack.length > 1) {
  324. say('After that:')
  325. for (let i = 1; i < stack.length; i++) {
  326. say(i + '. ' + stack[i])
  327. }
  328. }
  329. }
  330. }
  331.  
  332. // Print the help text
  333. function showHelp() {
  334. say(HELP_TEXT)
  335. }
  336. function onChat(sender, message) {
  337. try {
  338. command = message.toLowerCase()
  339. if (command == 'ping') {
  340. say('pong')
  341. } else if (command == QUEUE_KEYWORD) {
  342. addToStack(sender)
  343. } else if (command == DEQUEUE_KEYWORD) {
  344. popFromStack()
  345. } else if (command == GIVEUP_KEYWORD) {
  346. removeFromStack(sender)
  347. } else if (command == REMIND_KEYWORD) {
  348. reportStack()
  349. } else if (command == HELP_KEYWORD) {
  350. showHelp()
  351. }
  352. } catch (e) {
  353. console.error('Comrade message interpretation error: ', e)
  354. }
  355. }
  356.  
  357. // Type in the chat.
  358. async function say(message) {
  359. try {
  360. console.log('Sending: ' + message)
  361. // Need to wait for React to settle from the user doing things
  362. await sleep(100)
  363. let chat_box = document.getElementsByClassName('chat-box__chat-textarea')[0]
  364. console.log('Chat box: ', chat_box)
  365. chat_box.value = message
  366. let change_event = new Event('change', {
  367. 'view': window,
  368. 'bubbles': true,
  369. 'cancelable': true
  370. })
  371. chat_box.dispatchEvent(change_event)
  372. // All the keyboard event properties are read only so we have to set them up front.
  373. let enter_event = new KeyboardEvent('keydown', {
  374. bubbles: true,
  375. cancelable: true,
  376. code: "Enter",
  377. key: "Enter",
  378. keyCode: 13,
  379. which: 13
  380. })
  381. chat_box.dispatchEvent(enter_event)
  382. } catch (e) {
  383. console.error('Comrade message transmission error: ', e)
  384. }
  385. }
  386.  
  387.  
  388. botStartup()
  389.  
  390.  
  391.  
  392.  
  393.  
  394.