SHARE
TWEET

Untitled

a guest Mar 26th, 2020 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const CollabChatAPI = (()=>{
  2.     const version = 1;
  3.  
  4.     const onMessage = (()=>{
  5.         const messageCallbacks = [];
  6.  
  7.         // chat omits a persons name if they send consecutive messages, so the
  8.         // last person who sent a message is stored here so that every message
  9.         // object has a "from".
  10.         let lastFrom = null;
  11.  
  12.         const observer = new MutationObserver(mutations => {
  13.             mutations.forEach(mutation => {
  14.                 if (mutation.type != "childList") return;
  15.                 mutation.addedNodes.forEach(node => {
  16.                     if (node.tagName != "LI") return;
  17.  
  18.                     // extract information
  19.                     lastFrom = $(".participant-name", node).text() || lastFrom;
  20.                     const contents = $(".activity-body", node).text();
  21.  
  22.                     // construct message
  23.                     const message = {
  24.                         from: lastFrom,
  25.                         contents,
  26.                     };
  27.  
  28.                     // call callbacks
  29.                     messageCallbacks.forEach(callback => callback(message));
  30.                 });
  31.             })
  32.         });
  33.  
  34.         // changing breakout group, minimizing the chat, etc, causes a new chat
  35.         // element to be created, meaning the observer is now observing the old
  36.         // chat. This loop lazily rebinds the observer to the current chat
  37.         // every second.
  38.         setInterval(()=>{
  39.             observer.disconnect();
  40.             const chatRoot = $("#chat-channel-history");
  41.             if (chatRoot.length == 0) return;
  42.             observer.observe(chatRoot[0], { childList: true });
  43.         }, 1000);
  44.  
  45.         function onMessage(callback) {
  46.             messageCallbacks.push(callback);
  47.         }
  48.  
  49.         return onMessage;
  50.     })();
  51.  
  52.     function sendMessage(message) {
  53.         const messageInput = $("#message-input");
  54.         if (messageInput.length == 0) {
  55.             console.warn("CollabChatAPI attempted to sendMessage but no chat was open!");
  56.             return false;
  57.         }
  58.         const pressEnter = $.Event("keypress");
  59.         pressEnter.which = 13;
  60.         messageInput.val(message);
  61.         messageInput.trigger("change");
  62.         messageInput.trigger(pressEnter);
  63.         return true;
  64.     }
  65.  
  66.     return {
  67.         version,
  68.         onMessage,
  69.         sendMessage,
  70.     };
  71.  
  72. })();
  73.  
  74. /*
  75.  
  76. // send a message from your account
  77.  
  78. CollabChatAPI.sendMessage("hello!");
  79.  
  80. // do something when a new message is received:
  81.  
  82. CollabChatAPI.onMessage(msg => {
  83.     // do something with msg
  84. });
  85.  
  86. // msg in onMessage is the following object
  87. {
  88.     from: "John Bobington",
  89.     contents: "Hey there!"
  90. }
  91.  
  92. // example !roll command
  93. CollabChatAPI.onMessage(msg => {
  94.     if (msg.contents.toLowerCase() == "!roll") {
  95.         const roll = ~~(Math.random()*100);
  96.         CollabChatAPI.sendMessage(`[bot] you rolled ${roll}!`);
  97.     }
  98. });
  99.  
  100. // example logging all messages
  101. CollabChatAPI.onMessage(console.log);
  102.  
  103. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top