Advertisement
Guest User

Untitled

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