briochemc

tagpropup chat macro

Aug 20th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          TagPro Chat Macros Userscript
  3. // @namespace     http://www.reddit.com/user/contact_lens_linux/
  4. // @description   Help your team with quick chat macros.
  5. // @include       http://tagpro-*.koalabeast.com:*
  6. // @include       http://tangent.jukejuice.com:*
  7. // @include       http://maptest.newcompte.fr:*
  8. // @license       GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  9. // @author        steppin, Watball
  10. // @version       0.4
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.  
  15.   function contentEval(source) {
  16.     // Check for function input.
  17.     if ('function' == typeof source) {
  18.       // Execute this function with no arguments, by adding parentheses.
  19.       // One set around the function, required for valid syntax, and a
  20.       // second empty set calls the surrounded function.
  21.       source = '(' + source + ')();'
  22.     }
  23.  
  24.     // Create a script node holding this  source code.
  25.     var script = document.createElement('script');
  26.     script.setAttribute("type", "application/javascript");
  27.     script.textContent = source;
  28.  
  29.     // Insert the script node into the page, so it will run, and immediately
  30.     // remove it to clean up.
  31.     document.body.appendChild(script);
  32.     document.body.removeChild(script);
  33.   }
  34.  
  35.   function actualScript() {
  36.     var macros = {};
  37.  
  38.     macros[87] = {"message": "top" , "toAll": false}; // W
  39.     macros[83] = {"message": "mid" , "toAll": false}; // S
  40.     macros[88] = {"message": "bot" , "toAll": false}; // X
  41.     macros[65] = {"message": "left" , "toAll": false}; // A
  42.     macros[68] = {"message": "right" , "toAll": false}; // D
  43.  
  44.     // Game bindings overriding adapted from JohnnyPopcorn's NeoMacro https://gist.github.com/JohnnyPopcorn/8150909
  45.     var handlerbtn = $('#macrohandlerbutton');
  46.     handlerbtn.keydown(keydownHandler)
  47.               .keyup(keyupHandler);
  48.     handlerbtn.focus();
  49.  
  50.     $(document).keydown(documentKeydown);
  51.     function documentKeydown(event) {
  52.       if (!tagpro.disableControls) {
  53.         handlerbtn.focus(); // The handler button should be always focused
  54.       }
  55.     }
  56.  
  57.     function keydownHandler(event) {
  58.       var code = event.keyCode || event.which;
  59.       if (code in macros && !tagpro.disableControls) {
  60.         chat(macros[code]);
  61.         event.preventDefault();
  62.         event.stopPropagation();
  63.         //console.log(macros[code]);
  64.       }
  65.     }
  66.  
  67.     function keyupHandler(event) {
  68.       if (event.keyCode in macros && !tagpro.disableControls) {
  69.         event.preventDefault();
  70.         event.stopPropagation();
  71.       }
  72.     }
  73.  
  74.     var lastMessage = 0;
  75.  
  76.     function chat(chatMessage) {
  77.       var limit = 500 + 10;
  78.       var now = new Date();
  79.       var timeDiff = now - lastMessage;
  80.        
  81.       var end = tagpro.gameEndsAt;
  82.       var seconds = Math.round((end.getTime() - now.getTime())/1000)%60 ;
  83.      
  84.         if (timeDiff > limit) {
  85.           tagpro.socket.emit("chat", {"message": chatMessage.message + ' ' + seconds , "toAll": false});
  86.           lastMessage = new Date();
  87.       } else if (timeDiff >= 0) {
  88.           setTimeout(chat, limit - timeDiff, {"message": chatMessage.message + ' ' + seconds , "toAll": false})
  89.       }
  90.            
  91.       //setTimeout(function () {
  92.       //    tagpro.socket.emit("chat", {"message": chatMessage.message + ' ' + seconds , "toAll": false});
  93.       //}, 45000);      
  94.      
  95.            
  96.            
  97.            
  98.      
  99.     }
  100.   }
  101.  
  102.   // This dummy input will handle macro keypresses
  103.   var btn = document.createElement("input");
  104.   btn.style.opacity = 0;
  105.   btn.style.position = "absolute";
  106.   btn.style.top = "-100px";
  107.   btn.style.left = "-100px";
  108.   btn.id = "macrohandlerbutton";
  109.   document.body.appendChild(btn);
  110.  
  111.   contentEval(actualScript);
  112. })();
Add Comment
Please, Sign In to add comment