SHARE
TWEET

Untitled

a guest Jun 17th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { EventEmitter } from "events";
  2. import { PonyTownMessage } from "./PonyTownMessage";
  3. //const MutationObserver = require('mutation-observer');
  4.  
  5. export class PonyTownObserver extends EventEmitter {
  6.  
  7.     private chatLog = false;
  8.     private ponyBox = false
  9.     private ponyBoxRect: any = null;
  10.     private currentMessage: HTMLElement = null;
  11.     private eventListinerAddToButton = false;
  12.  
  13.     constructor() {
  14.         super();
  15.  
  16.         let chatlogObserver = new ChatlogObserver((_li, _o) => {
  17.             this.newMsg();
  18.         });
  19.         console.log('test1');
  20.         let gameStartObserver = new GameStartObserver((_li, _o) => {
  21.             console.log('test2');
  22.             //eventListinerAddToButton
  23.             if (!this.eventListinerAddToButton) {
  24.                 if (document.querySelector('.btn-success')) {
  25.                     let button = <HTMLButtonElement>document.querySelector('.btn-success');
  26.                     if (button) {
  27.                         button.addEventListener('click', () => {
  28.                             let namebox = <HTMLInputElement>document.querySelector('.form-control');
  29.                             if (namebox) {
  30.                                 this.emit('selectedName', namebox.value.trim());
  31.                             }
  32.                         });
  33.                         this.eventListinerAddToButton = true;
  34.                     }
  35.                 }
  36.             }
  37.             console.log("This message never appeared on firefox");
  38.             if (document.querySelector(".pony-box-rect")) {
  39.                 //if pone box appear
  40.                 let name = document.querySelector(".pony-box-name").textContent;
  41.  
  42.                 let socialMedia = null;
  43.                 let socialMediaUrl = null;
  44.                 let socialMediaHtml = document.querySelector('.site-info')
  45.  
  46.                 if (socialMediaHtml) {
  47.                     socialMedia = socialMediaHtml.textContent;
  48.                     //@ts-ignore  
  49.                     socialMediaUrl = socialMediaHtml.href;
  50.                 }
  51.  
  52.                 let obj = {
  53.                     name: name,
  54.                     socialMediaName: socialMedia,
  55.                     socialMediaUrl: socialMediaUrl
  56.                 }
  57.  
  58.                 if (!this.ponyBox) this.emit('ponyInfoBoxCreate', obj);
  59.                 if (this.ponyBoxRect !== socialMediaHtml) {
  60.                     this.ponyBoxRect = socialMediaHtml;
  61.                     this.emit('ponyInfoBoxUpdate', obj);
  62.                 }
  63.                 this.ponyBox = true;
  64.             }
  65.             else {
  66.                 if (this.ponyBox) this.emit('ponyInfoBoxDestroy')
  67.                 this.ponyBoxRect = null;
  68.                 this.ponyBox = false;
  69.             }
  70.             console.log('tesasdt');
  71.             if (document.querySelector(".chat-log-scroll-inner-inner")) {
  72.  
  73.                 if (!this.chatLog) this.emit('chatlogCreate');
  74.                 this.chatLog = true;
  75.  
  76.                 // The chatlog exists, we can observe it
  77.                 chatlogObserver.observe();
  78.  
  79.             } else {
  80.                 // The chatlog no longer exist, we'd better stop watching it
  81.                 // I'm not sure that anything bad would happen if we didn't, but
  82.                 // I hate having to guess where a bug comes from
  83.  
  84.                 chatlogObserver.disconnect();
  85.                 if (this.chatLog) this.emit('chatlogDestory');
  86.                 this.chatLog = false;
  87.             }
  88.         });
  89.         gameStartObserver.observe();
  90.     }
  91.  
  92.     //Complicated function
  93.     public newMsg() {
  94.  
  95.         let messages = document.getElementsByClassName('chat-line') as HTMLCollectionOf<HTMLDivElement>;
  96.         if (!messages) return;
  97.         // We have to find last message because sometimes there can
  98.         // appear two or more messages which can lead us to message skipping.
  99.  
  100.         //Using revers loop becuse that how message system works. get index of last
  101.         //message that was send
  102.  
  103.         //if currentMessage is not defiend we read full chatlog
  104.         if (this.currentMessage === null) return this.getMessagesFromPos(messages, 0);
  105.         if (this.currentMessage === messages[messages.length - 1]) return;
  106.         for (let i = messages.length - 1; i >= 0; i--) {
  107.  
  108.             //searching for last message if found reverse loop
  109.             if (this.currentMessage === messages[i]) {
  110.                 if (!messages[i + 1]) return this.getMessagesFromPos(messages, 0);
  111.                 if (!messages[i + 1]) return;
  112.                 return this.getMessagesFromPos(messages, i + 1);
  113.             }
  114.         }
  115.         //if not found we read all messages
  116.         return this.getMessagesFromPos(messages, 0);
  117.     }
  118.  
  119.     //get messages from position
  120.     private getMessagesFromPos(messages: HTMLCollectionOf<HTMLDivElement>, position: number) {
  121.         for (let i = position; i < messages.length; i++) {
  122.             this.newMessage(messages[i]);
  123.         }
  124.     }
  125.  
  126.     //messages
  127.     private newMessage(message: HTMLDivElement) {
  128.         //saves last message
  129.         this.currentMessage = message;
  130.         this.emit('newMessage', new PonyTownMessage(message));
  131.     }
  132. }
  133.  
  134.  
  135.  
  136. /**
  137.  * ChatlogObserver
  138.  *
  139.  * Observe the chatlog. If the chatlog changes, update the observed chatlog when
  140.  * .observe() is called
  141.  */
  142. class ChatlogObserver extends MutationObserver {
  143.     chatContainer: Element;
  144.  
  145.     observe() {
  146.         let chatContainer = document.querySelector(".chat-log-scroll-inner-inner");
  147.         if (this.chatContainer === chatContainer) return;
  148.         this.chatContainer = chatContainer;
  149.         //Detect chatlog
  150.         super.observe(chatContainer, {
  151.             childList: true
  152.         });
  153.     }
  154.     disconnect() {
  155.         super.disconnect();
  156.         this.chatContainer = null;
  157.     }
  158. }
  159.  
  160. class GameStartObserver extends MutationObserver {
  161.  
  162.     observe() {
  163.         super.observe(document.body, {
  164.             childList: true,
  165.             subtree: true
  166.         });
  167.     }
  168. }
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