Advertisement
Guest User

MC Dynmap Chat Log

a guest
Jul 10th, 2016
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var frozenIceWrapper = function(){
  2.  
  3.     this.run = function() {
  4.         console.log("ok");
  5.         if (!this.supports_html5_storage()) {
  6.             alert('Der Browser unterschtützt diese Technologie nicht!');
  7.         } else {
  8.             console.log("ok2");
  9.             this.insertButtons();
  10.             this.bindToChat();
  11.         }
  12.    
  13.     };
  14.  
  15.  
  16.     this.supports_html5_storage = function() { //http://stackoverflow.com/questions/17956343/can-you-save-load-a-file-via-javascript
  17.      try
  18.       {
  19.         return 'localStorage' in window && window['localStorage'] !== null;
  20.       }
  21.       catch (e)
  22.       {
  23.         return false;
  24.       }
  25.     }
  26.    
  27.     this.bindToChat = function() {
  28.         var saveToLog = this.saveToLog;
  29.         $('.messagelist').bind('DOMNodeInserted', function(event) {
  30.         var targetNode = event.target;
  31.         var messagerow = targetNode.getElementsByClassName('messagerow');
  32.         var textNodes = targetNode.getElementsByClassName('messagetext');
  33.        
  34.         var playerName = null;
  35.         var message = "";
  36.        
  37.         for (node in textNodes) {
  38.             //console.log(node + ": " + textNodes[node].innerHTML);
  39.            
  40.             if (node.substring(0,4) == "ms__") {
  41.                 console.log(node + ": " + textNodes[node].innerHTML);
  42.                 if (playerName == null) {
  43.                     playerName = textNodes[node].innerHTML;
  44.                 } else {
  45.                     message += textNodes[node].innerHTML + " ";
  46.                 }
  47.                
  48.             }
  49.            
  50.         }
  51.        
  52.         if (playerName != null) {
  53.             var today = new Date();
  54.             console.log("(" + today.getHours() + ":" + today.getMinutes() + ") " + playerName + "  " + message);
  55.             saveToLog("(" + today.getHours() + ":" + today.getMinutes() + ") " + playerName + "  " + message + "<br>");
  56.         }
  57.         });
  58.     };
  59.    
  60.    
  61.     this.saveToLog = function(text) {
  62.         var myDataString = localStorage.getItem("chatLogFrozen");
  63.         if (myDataString == null) {myDataString = "";}
  64.         myDataString += "\n" + text;
  65.         localStorage.setItem("chatLogFrozen", myDataString);
  66.     };
  67.  
  68.     this.clearLog = function() {
  69.         if (confirm("Wollen sie den ChatLog löschen?")) {
  70.             localStorage.removeItem("chatLogFrozen");
  71.         }
  72.     };
  73.  
  74.     this.downloadLog = function() {
  75.         var temp = window.open('about:blank', 'example', '');
  76.         var div = temp.document.createElement("div");
  77.         div.innerHTML = "Logs<br>" + localStorage.getItem("chatLogFrozen");
  78.         temp.document.body.appendChild( div );
  79.     };
  80.  
  81.     this.insertButtons = function() {
  82.         var divs = document.getElementsByClassName('dynmap-link leaflet-control');
  83.         var div = divs[0];
  84.         var divDownload = document.createElement("a");
  85.         var divDelete = document.createElement("a");
  86.         //divDownload.className = "dynmap-link-button";
  87.         divDownload.title="Download Chat Log";
  88.         divDownload.innerHTML = "Download";
  89.        
  90.         divDelete.title = "Delete Chat Log";
  91.         divDelete.innerHTML = "Delete";
  92.        
  93.         div.appendChild(divDownload);
  94.         div.appendChild(divDelete);
  95.        
  96.        
  97.         divDownload.addEventListener("click", this.downloadLog);
  98.         divDelete.addEventListener("click", this.clearLog);
  99.        
  100.     };
  101.  
  102. }
  103.  
  104.  
  105. var chatLog = new frozenIceWrapper();
  106. chatLog.run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement