Skip to content

Instantly share code, notes, and snippets.

@undergroundmonorail
Last active August 29, 2015 14:04
Show Gist options
  • Save undergroundmonorail/0a269e64e4bf40588796 to your computer and use it in GitHub Desktop.
Save undergroundmonorail/0a269e64e4bf40588796 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name TagPro Chat Macros Userscript
// @namespace http://www.reddit.com/user/contact_lens_linux/
// @description Help your team with quick chat macros.
// @include http://tagpro-*.koalabeast.com:*
// @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @author steppin, Watball, Some Ball -1, monorail
// @version 0.4
// ==/UserScript==
(function() {
function contentEval(source) {
// Check for function input.
if ('function' == typeof source) {
// Execute this function with no arguments, by adding parentheses.
// One set around the function, required for valid syntax, and a
// second empty set calls the surrounded function.
source = '(' + source + ')();'
}
// Create a script node holding this source code.
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = source;
// Insert the script node into the page, so it will run, and immediately
// remove it to clean up.
document.body.appendChild(script);
document.body.removeChild(script);
}
tagpro.timeLeft = function(kind){
var end = tagpro.gameEndsAt;
var curr = new Date();
var ms = end.getMilliseconds()-curr.getMilliseconds();
end = end.getMinutes()*60+end.getSeconds();
curr = curr.getMinutes()*60+curr.getSeconds();
if(curr>end) //stradling over an hour mark
{
end += 60*60; //add an hour to it
}
var min = Math.floor((end-curr)/60);
var sec = Math.floor((end-curr)%60+ms/1000);
if(ms<0)
{
ms += 1000;
}
ms = Math.round(ms/100);
if(min<10)
{
min = '0'+min;
}
if(sec<10)
{
sec = '0'+sec;
}
if(kind==1) //just min
{
return min;
}
else if(kind==2) //just sec
{
return sec;
}
return min+':'+sec+'.'+ms;
}
function actualScript() {
var macros = {};
// HERE'S THAT BIT I TOLD YOU TO LOOK FOR
// Game bindings overriding adapted from JohnnyPopcorn's NeoMacro https://gist.github.com/JohnnyPopcorn/8150909
var handlerbtn = $('#macrohandlerbutton');
handlerbtn.keydown(keydownHandler)
.keyup(keyupHandler);
handlerbtn.focus();
$(document).keydown(documentKeydown);
function documentKeydown(event) {
if (!tagpro.disableControls) {
handlerbtn.focus(); // The handler button should be always focused
}
}
// Prevent TagPro binds from firing on the same stuff as we have bound
function keyupHandler(event) {
if (event.keyCode in macros && !tagpro.disableControls) {
event.preventDefault();
event.stopPropagation();
}
}
var lastMessage = 0;
function chat(chatMessage) {
function convert(msg) {
msg = msg.replace(/\/t/g,tagpro.timeLeft(0));
msg = msg.replace(/\/m/g,tagpro.timeLeft(1));
msg = msg.replace(/\/s/g,tagpro.timeLeft(2));
return msg;
}
var limit = 500 + 10;
var now = new Date();
var timeDiff = now - lastMessage;
if (timeDiff > limit) {
tagpro.socket.emit("chat", {
message: convert(chatMessage.message),
toAll: chatMessage.allchat
});
lastMessage = new Date();
} else if (timeDiff >= 0) {
setTimeout(chat, limit - timeDiff, chatMessage)
}
}
function keydownHandler(event) {
var code = event.keyCode || event.which;
if (code in macros && !tagpro.disableControls) {
for (var i = 0; i < macros[code].message.split('/n').length; i++) {
chat({ message:macros[code].message.split('/n')[i], allchat:macros[code].allchat });
}
event.preventDefault();
event.stopPropagation();
//console.log(macros[code]);
} else if (code == 191) {
var press = $.Event('keydown');
press.which = 84;
press.keyCode = 84;
handlerbtn.trigger(press);
$('#chat').val("");
}
}
}
// This dummy input will handle macro keypresses
var btn = document.createElement("input");
btn.style.opacity = 0;
btn.style.position = "absolute";
btn.style.top = "-100px";
btn.style.left = "-100px";
btn.id = "macrohandlerbutton";
document.body.appendChild(btn);
contentEval(actualScript);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment