Skip to content

Instantly share code, notes, and snippets.

@chrahunt
Forked from omicr0n/tagpro-chatenhancer.user.js
Created January 15, 2015 00:33
Show Gist options
  • Save chrahunt/ed993091c68f64791ab9 to your computer and use it in GitHub Desktop.
Save chrahunt/ed993091c68f64791ab9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name TagPro Chat Enhancer
// @namespace http://www.reddit.com/user/-omicron-/
// @description Disables ingame chat messages while logging messages outside the game environment with the same styling/formating but allowing for all kinds of extra functionality in doing so.
// @include http://tagpro-*.koalabeast.com:*
// @include http://tagpro-*.koalabeast.com/groups/*
// @include http://tangent.jukejuice.com:*
// @include http://tangent.jukejuice.com/groups/*
// @include http://maptest.newcompte.fr:*
// @include http://maptest.newcompte.fr/groups/*
// @include http://maptest2.newcompte.fr:*
// @include http://maptest2.newcompte.fr/groups/*
// @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @author OmicroN
// @version 3.0.0
// ==/UserScript==
tagpro.ready(function() {
tagpro.settings.ui.allChat = false;
tagpro.settings.ui.groupChat = false;
tagpro.settings.ui.systemChat = false;
tagpro.settings.ui.teamChat = false;
});
(function(window, undefined) {
// normalized window
var w;
if (unsafeWindow != "undefined") {
w = unsafeWindow;
} else {
w = window;
}
/**
* Inject the function given by `source` into the page.
* @param {function} source - The function to inject into the page.
*/
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);
}
// Function that, when injected, provides the in-game functionality.
function ceGameScript() {
//########## EDITABLE VARIABLES ##############//
// Whether to auto save the chat log at the end of every match
// automatically.
var saveLogEveryGame = false;
// Toggle when clicking the save chat download button if the
// download starts right away or when the game ends/window unloads
var startSaveOnGameEnd = true;
// Sets the snap point in pixels for resizing the chat windows
// height, this is to prevent you from resizing the window where
// words get cut off at the top when scrolling. Adjust according
// to your systems font and how the text is displayed ingame.
var resizeHeightSnap = 14;
// Toggle whether to save the chat window via absolute position
// of the top left corner of the browser (false) or relative
// position of the top left corner of the games viewport (true).
// Using true will make it so if you resize your browser the chat
// will be repositioned b
var relativeViewPort = true;
// Delay to fade the chat after no activity in milliseconds,
// default 10000 (10 seconds)
var fadeDelay = 10000;
// true = show auto complete of names automatically while being
// typed, false = only auto complete on tab press
var autocomplete = true;
//######## END EDITABLE VARIABLES ############//
// Function prototype to add format method to the Date object
// (author: (SO) meizz)
Date.prototype.format = function(format) {
var hours = this.getHours();
var ttime = "AM";
if (format.indexOf("t") > -1 && hours > 12) {
hours = hours - 12;
ttime = "PM";
}
var o = {
"M+": this.getMonth() + 1,
//month
"d+": this.getDate(),
//day
"h+": hours,
//hour
"m+": this.getMinutes(),
//minute
"s+": this.getSeconds(),
//second
"q+": Math.floor((this.getMonth() + 3) / 3),
//quarter
"S": this.getMilliseconds(),
//millisecond
"t+": ttime
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
if (format.replace(RegExp.$1, RegExp.$1.length == 1)) {
format = o[k];
} else {
format = ("00" + o[k]).substr(("" + o[k]).length);
}
}
}
return format;
}
var startMonitor = false;
var disableCheck = 0;
// For some reason after disabling ingame chats they somehow
// re-enable themselves, so this checks every 100ms until 10
// successful checks of them being disabled
function disableChats() {
if (disableCheck < 10) {
if (tagpro.settings.ui.allChat != false || tagpro.settings.ui.groupChat != false || tagpro.settings.ui.systemChat != false || tagpro.settings.ui.teamChat != false) {
disableCheck = 0;
tagpro.settings.ui.allChat = false;
tagpro.settings.ui.groupChat = false;
tagpro.settings.ui.systemChat = false;
tagpro.settings.ui.teamChat = false;
} else {
disableCheck++;
}
setTimeout(function() {
disableChats();
}, 100);
}
}
// Call the function to disable ingame chats
disableChats();
// Takes an object and returns the number of properties possessed
// by it.
function objectLength(obj) {
var result = 0;
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
result++;
}
}
return result;
}
// Function to save chat to txt file
function exportChat() {
var d = new Date();
window.URL = window.webkitURL || window.URL;
var bb = new Blob([$('<div>').html($('#message-logger .mCSB_container').html().replace(/<br\s?\/?>/gi, '_br2nl_')).text().replace(/_br2nl_/gi, '\r\n')], {
type: 'text/plain'
});
var event = document.createEvent('MouseEvents')
event.initEvent('click', true, false);
var a = document.createElement('a');
a.download = d.format("yyyy-MM-dd hh.mm.ss") + ' ' + tagpro.host.replace(':', ' ') + ' chatlog.txt';
a.href = window.URL.createObjectURL(bb);
a.dispatchEvent(event);
}
// Helper function to set cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
var domain = location.hostname.split('.');
if (domain.length != 2) domain.shift();
document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/;domain=" + domain.join('.');
}
// Helper function to get cookie
function getCookie(cname) {
var name = cname + "=",
ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
// Helper function to load/inject external javascript onto page
function getScript(url, success) {
var script = document.createElement('script'),
head = document.getElementsByTagName('head')[0],
done = false;
script.src = url;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
// Helper function to escape HTML characters to prevent code injection
function htmlEscape(str) {
return String(str).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
function gameScript() {
// Variables to store/load outside chat contains last position/size user defined
var ceheight, cetop, celeft, cewidth = getCookie('ce-width'),
firstruncheck = false;
// if width isn't set from cookie then first load so set defaults and save to cookie
if (cewidth == "") {
cewidth = 500;
ceheight = 70;
firstruncheck = true;
setCookie('ce-width', cewidth, 365);
setCookie('ce-height', ceheight, 365);
} else { // load last saved position/size of chat container into variables
cewidth = parseInt(getCookie('ce-width'));
ceheight = parseInt(getCookie('ce-height'));
cetop = parseInt(getCookie('ce-top'));
celeft = parseInt(getCookie('ce-left'));
}
// Check if setting to save chat log of every game is enabled
if (saveLogEveryGame) {
// Call save chat function on browser unload event
$(window).on('beforeunload', function() {
exportChat();
});
}
if (relativeViewPort) {
delay = null;
cetop = $('#viewport').offset().top + parseInt(getCookie('ce-top'));
celeft = $('#viewport').offset().left + parseInt(getCookie('ce-left'));
$(window).resize(function() {
delay && clearTimeout(delay), delay = setTimeout(function() {
delay = null;
cetop = $('#viewport').offset().top + parseInt(getCookie('ce-top'));
celeft = $('#viewport').offset().left + parseInt(getCookie('ce-left'));
$('#message-logger').css('top', cetop).css('left', celeft);
}, 1000)
});
}
chatFade = null
// Variable used to check if chat is scrolled all the way to the bottom and autoscroll new text
var autoscroll = true;
// Append our container to store messages to the page along with the variables defined above of position/size
$('body').append('<div id="message-logger" style="position: absolute; z-index: 100; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; padding: 5px 0px 5px 12px; left: ' + celeft + 'px; top: ' + cetop + 'px; width: ' + cewidth + 'px; height: ' + ceheight + 'px; font-family: Arial; font-size: 11px; color: #fff; text-shadow: 0px 0px 10px #000, 0px 0px 10px #000, 0px 0px 10px #000, -1px -1px 1px #000, 1px 1px 1px #000, -1px 1px 1px #000, 1px -1px 1px #000, 0px 0px 3px #000; font-weight: bold;"><div class="data" style="height: 100%; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;"> </div><div class="draghandle" title="Move Chat Area" style="z-index: 100; width: 16px; height: 16px; top: 0; left: 0; position: absolute; background: URL(\'http://reanimated.net/tagpro/move.png\') no-repeat; display: none; margin: 5px;"></div> <div class="savechat" title="Save Chat" style="cursor: pointer; z-index: 100; width: 16px; height: 16px; top: 20px; left: 0; position: absolute; background: URL(\'http://reanimated.net/tagpro/download.png\') no-repeat; display: none; margin: 5px;"></div><div class="resizehandle" title="Resize Chat Area" style="z-index: 100; width: 10px; height: 10px; bottom: 0; right: 0; position: absolute; background: URL(\'http://reanimated.net/tagpro/resize.png\') no-repeat; margin: 0px; cursor: nwse-resize; display: none;"></div></div>');
$('#chat').on('focus', function() {
$(this).css('top', $('#message-logger').offset().top + $('#message-logger').height() + 5).css('left', $('#message-logger').offset().left + 25);
clearTimeout(chatFade);
$('#message-logger .data').fadeIn();
});
//$('#chat').on('blur', function() {
// if (tagpro.disableControls) $('#message-logger .data').fadeOut();
//});
if (firstruncheck) {
function canvasPositionCheck() {
if ($('#viewport')[0].offsetParent != null) {
cetop = $('#viewport').offset().top + 25;
celeft = $('#viewport').offset().left - 8;
if (relativeViewPort) {
cetop = 25;
celeft = -8;
}
setCookie('ce-top', cetop, 365);
setCookie('ce-left', celeft, 365);
if (relativeViewPort) {
$('#message-logger').css('top', $('#viewport').offset().top + cetop).css('left', $('#viewport').offset().left + celeft);
} else {
$('#message-logger').css('top', cetop).css('left', celeft);
}
} else {
setTimeout(function() {
canvasPositionCheck();
}, 100);
}
}
canvasPositionCheck();
}
if (relativeViewPort && !firstruncheck) {
function setPositionOnLoad() {
if ($('#viewport')[0].offsetParent != null) {
cetop = $('#viewport').offset().top + parseInt(getCookie('ce-top'));
celeft = $('#viewport').offset().left + parseInt(getCookie('ce-left'));
$('#message-logger').css('top', cetop).css('left', celeft);
} else {
setTimeout(function() {
setPositionOnLoad();
}, 100);
}
}
setPositionOnLoad();
}
tagpro.socket.on("id", function(message) {
startMonitor = true;
});
maptestjoinfix = [];
tagpro.socket.on("p", function(data) {
if (!startMonitor) return;
if (tagpro.spectator) {
if (typeof data[0] !== "undefined") {
if (typeof data[0].name !== "undefined" && typeof data[0].points === "undefined") {
if (maptestjoinfix.indexOf(data.u[0].id) != -1) return;
maptestjoinfix.push(data.u[0].id);
$('#message-logger .data').fadeIn();
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") $("#message-logger .data").mCustomScrollbar("update");
$('#message-logger .mCSB_container').append(data[0].name + ' has joined the ' + (data[0].team == '1' ? '<span style="color: #FFB5BD;">red team</span>' : '<span style="color: #CFCFFF;">blue team</span>') + '.<br />');
// New message appended to chat container so update/inform custom scrollbar plugin of new lengths and auto scroll to bottom of chat container to show new message
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
if (autoscroll) $("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
}
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
}
}
} else {
if (typeof data.u !== "undefined") {
if (typeof data.u[0].name !== "undefined") {
if (maptestjoinfix.indexOf(data.u[0].id) != -1) return;
maptestjoinfix.push(data.u[0].id);
$('#message-logger .data').fadeIn();
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") $("#message-logger .data").mCustomScrollbar("update");
$('#message-logger .mCSB_container').append(data.u[0].name + ' has joined the ' + (data.u[0].team == '1' ? '<span style="color: #FFB5BD;">red team</span>' : '<span style="color: #CFCFFF;">blue team</span>') + '.<br />');
// New message appended to chat container so update/inform custom scrollbar plugin of new lengths and auto scroll to bottom of chat container to show new message
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
if (autoscroll) $("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
}
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
}
}
}
});
// Monitor all chat messages
tagpro.socket.on('chat', function(data) {
$('#message-logger .data').fadeIn();
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") $("#message-logger .data").mCustomScrollbar("update");
// Message has no from then its from the system, display in default white text
if (data.from == null) {
if (typeof data.c !== "undefined") { // If system message contains a c property then it has a custom color to display the message in (flair reward messages)
$('#message-logger .mCSB_container').append('<span style="color: ' + data.c + '">' + data.message + '</span><br />');
} else { // Message is a basic system message, show in white
var pattern = /(.+?) has (connected|joined the red team|joined the blue team)\./;
var results = pattern.exec(data.message);
if (results != null) {
/*
setInterval(function() {
for (var pID in tagpro.players) {
if (tagpro.players[pID].name == results[1]) {
$('#message-logger .mCSB_container').append(tagpro.players[pID].name + ' has joined the ' + (tagpro.players[pID].team == '1' ? '<span style="color: #FFB5BD;">red team</span>' : '<span style="color: #CFCFFF;">blue team</span>') + '.<br />');
break;
}
}
}, 100);
*/
} else {
var pattern = /'(.+?)' left the (red team|blue team|game)\./;
var results = pattern.exec(data.message);
if (results != null) {
// Player has left message found but player leaving chat message is handled elsewhere in the script so ignore the message
} else {
var pattern = /'(.+?)' has switched teams\./;
var results = pattern.exec(data.message);
if (results != null) {
for (var pID in tagpro.players) {
if (tagpro.players[pID].name == results[1]) {
$('#message-logger .mCSB_container').append(tagpro.players[pID].name + ' has switched to the ' + (tagpro.players[pID].team == '1' ? '<span style="color: #CFCFFF;">blue team</span>' : '<span style="color: #FFB5BD;">red team</span>') + '.<br />');
break;
}
}
} else {
$('#message-logger .mCSB_container').append(data.message + '<br />');
}
}
}
}
} else {
// Message to 'team' rather then 'all' so we need to display the whole message in team color rathern then just the player name
if (data.to == 'team') {
// Check to see what team the person who sent the message is from, if team = 1 then red and write message with FFB5BD else team = 2 blue so write with CFCFFF
if (tagpro.players[data.from].team == '1') { // Message from red team player to team, show in red
$('#message-logger .mCSB_container').append('<span style="color: #FFB5BD;"><span style="z-index: 10; cursor: pointer;" class="playeroptions" pid="' + data.from + '" pname="' + encodeURIComponent(tagpro.players[data.from].name) + '">' + (tagpro.players[data.from].auth == true ? '<span style="color: #BFFF00;">&#10003;</span> ' : '') + tagpro.players[data.from].name + '</span><span style="font-weight: normal">:</span> ' + Autolinker.link(htmlEscape(data.message).replace(/%20/g, ' '), {
stripPrefix: false
}) + '</span><br />');
} else { // Message from blue team player to team, show in blue
$('#message-logger .mCSB_container').append('<span style="color: #CFCFFF;"><span style="z-index: 10; cursor: pointer;" class="playeroptions" pid="' + data.from + '" pname="' + encodeURIComponent(tagpro.players[data.from].name) + '">' + (tagpro.players[data.from].auth == true ? '<span style="color: #BFFF00;">&#10003;</span> ' : '') + tagpro.players[data.from].name + '</span><span style="font-weight: normal">:</span> ' + Autolinker.link(htmlEscape(data.message).replace(/%20/g, ' '), {
stripPrefix: false
}) + '</span><br />');
}
} else {
// Check to see what team the person who sent the message is from, if team = 1 then red and write message in white but name with FFB5BD else team = 2 blue so write name with CFCFFF
if (tagpro.players[data.from].team == '1') { // Message from red team player, show in white but name in red
$('#message-logger .mCSB_container').append('<span style="color: #FFB5BD; z-index: 10; cursor: pointer;" class="playeroptions" pid="' + data.from + '" pname="' + encodeURIComponent(tagpro.players[data.from].name) + '">' + (tagpro.players[data.from].auth == true ? '<span style="color: #BFFF00;">&#10003;</span> ' : '') + tagpro.players[data.from].name + '</span><span style="font-weight: normal">:</span> ' + Autolinker.link(htmlEscape(data.message).replace(/%20/g, ' '), {
stripPrefix: false
}) + '<br />');
} else { // Message from blue team player, show in white but name in blue
$('#message-logger .mCSB_container').append('<span style="color: #CFCFFF; z-index: 10; cursor: pointer;" class="playeroptions" pid="' + data.from + '" pname="' + encodeURIComponent(tagpro.players[data.from].name) + '">' + (tagpro.players[data.from].auth == true ? '<span style="color: #BFFF00;">&#10003;</span> ' : '') + tagpro.players[data.from].name + '</span><span style="font-weight: normal">:</span> ' + Autolinker.link(htmlEscape(data.message).replace(/%20/g, ' '), {
stripPrefix: false
}) + '<br />');
}
}
}
// New message appended to chat container so update/inform custom scrollbar plugin of new lengths and auto scroll to bottom of chat container to show new message
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
if (autoscroll) $("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
}
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
});
var chatInputStyled = false;
$(document).keyup(function(t) {
if ($('#chat').is(':visible') && tagpro.disableControls) {
if ((t.keyCode == 13 || t.keyCode == 222 || t.keyCode == 84 || t.keyCode == 186 || t.keyCode == 71 || t.keyCode == 103 || t.keyCode == 116 && t.originalEvent.keyIdentifier != "F5") && tagpro.disableControls && !chatInputStyled) {
if ($("#name").is(":focus")) return;
$('#chat').css('outline', '#ccc solid 1px');
if (t.keyCode == 13) $('#chat').css('border', '2px solid #ffffff');
if (t.keyCode == 222 || t.keyCode == 84) {
if (tagpro.viewport.source.team == undefined) {
$('#chat').css('border', '2px solid #000000');
} else if (tagpro.viewport.source.team == 1) {
$('#chat').css('border', '2px solid #ff0000');
} else {
$('#chat').css('border', '2px solid #0000ff');
}
}
if (t.keyCode == 186 || t.keyCode == 71 || t.keyCode == 103) $('#chat').css('border', '2px solid gold');
chatInputStyled = true;
}
} else {
if ($('#chat').is(':hidden') && chatInputStyled) {
$('#chat').css('border', 'none');
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
chatInputStyled = false;
}
}
});
// Monitor all group chat messages if part of a group
if (tagpro.group.socket != null) {
tagpro.group.socket.on('chat', function(data) {
$('#message-logger .data').fadeIn();
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") $("#message-logger .data").mCustomScrollbar("update");
// Message from group so write entire message in yellow E7E700, if no from name then system group message of someone joining/leaving
if (data.from == null) { // Message has no from so system group message (join/leave events)
$('#message-logger .mCSB_container').append('<span style="color: #E7E700">' + htmlEscape(data.message).replace(/%20/g, ' ') + '</span><br />');
} else { // Message contains a from property so display message as if user sent it
$('#message-logger .mCSB_container').append('<span style="color: #E7E700"><span style="cursor: pointer;" class="playeroptions" pname="' + encodeURIComponent(data.from) + '">' + data.from + '</span><span style="font-weight: normal">:</span> ' + Autolinker.link(htmlEscape(data.message).replace(/%20/g, ' '), {
stripPrefix: false
}) + '</span><br />');
}
// New message appended to chat container so update/inform custom scrollbar plugin of new lengths and auto scroll to bottom of chat container to show new message
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
if (autoscroll) $("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
}
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
});
}
// Load external script from my site to load auto linker library
getScript('http://reanimated.net/tagpro/Autolinker.min.js', function() {});
getScript('http://reanimated.net/tagpro/jquery.a-tools-1.4.1.js', function() {
getScript('http://reanimated.net/tagpro/jquery.asuggest.js', function() {
function waitUntilPlayerListPopulated() {
// Check if game is loaded and players are inside
if (typeof tagpro.players !== "undefined" &&
objectLength(tagpro.players) > 0) {
playerlist = [];
playersingame = [];
playersteam = [];
for (var pID in tagpro.players) {
if (playerlist.indexOf(tagpro.players[pID].name) == -1) {
playerlist.push(tagpro.players[pID].name);
}
playersingame[pID] = tagpro.players[pID].name;
playersteam[pID] = tagpro.players[pID].team;
}
$("#chat").asuggest(playerlist, {
'stopSuggestionKeys': [$.asuggestKeys.RETURN],
'minChunkSize': 1,
'delimiters': ' \n',
'cycleOnTab': true,
'autoComplete': autocomplete,
'ignoreCase': true
});
tagpro.socket.on('playerLeft', function(pID) {
$('#message-logger .data').fadeIn();
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
}
$('#message-logger .mCSB_container').append(playersingame[pID] + ' has left the ' + (playersteam[pID] == '1' ? '<span style="color: #FFB5BD;">red team</span>' : '<span style="color: #CFCFFF;">blue team</span>') + '.<br />');
playerlist.splice(playerlist.indexOf(playersingame[pID]), 1);
delete playersingame[pID];
delete playersteam[pID];
// New message appended to chat container so update/inform custom scrollbar plugin of new lengths and auto scroll to bottom of chat container to show new message
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
if (autoscroll) $("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
}
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
});
tagpro.socket.on('p', function(data) {
data = data.u || data;
for (var i = 0; i < data.length; i++) {
if (typeof data[i].id !== "undefined" && typeof data[i].name !== "undefined") {
if (playerlist.indexOf(data[i].name) == -1) playerlist.push(data[i].name);
playersingame[data[i].id] = data[i].name;
playersteam[data[i].id] = data[i].team;
}
}
});
} else {
setTimeout(function() {
waitUntilPlayerListPopulated();
}, 1000);
}
}
waitUntilPlayerListPopulated();
});
});
// Load external script from my site to load jquery script for custom scrollbars then execute the rest of the script on this page
getScript('http://reanimated.net/tagpro/jquery.mCustomScrollbar.concat.min.js', function() {
// Create a new style element to load up external css for jquery custom scrollbar script
var style = document.createElement('link');
style.setAttribute("rel", "stylesheet");
style.setAttribute("type", "text/css");
style.href = 'http://reanimated.net/tagpro/jquery.mCustomScrollbar.css';
document.body.appendChild(style);
// jQuery draggable plugin
(function($) {
$.fn.drag = function(options) {
options = $.extend({
handle: null,
cursor: 'move',
draggingClass: 'dragging'
}, options);
var $handle = this,
$drag = this;
if (options.handle) {
$handle = $(options.handle);
}
$handle.css('cursor', options.cursor).on("mousedown", function(e) {
var x = $drag.offset().left - e.pageX,
y = $drag.offset().top - e.pageY,
z = $drag.css('z-index');
$drag.css('z-index', 100000);
$(document.documentElement).on('mousemove.drag', function(e) {
$drag.offset({
left: x + e.pageX,
top: y + e.pageY
});
}).one('mouseup', function() {
if (relativeViewPort) {
setCookie('ce-top', $drag.offset().top - $('#viewport').offset().top, 365);
setCookie('ce-left', $drag.offset().left - $('#viewport').offset().left, 365);
} else {
setCookie('ce-top', $drag.offset().top, 365);
setCookie('ce-left', $drag.offset().left, 365);
}
$(this).off('mousemove.drag');
$drag.css('z-index', z);
});
// disable selection
e.preventDefault();
});
};
})(jQuery);
function onScrollCallback() {
if (mcs.topPct == 100) autoscroll = true;
else autoscroll = false;
}
// Initiate jQuery custome scrollbars on chat containers data element, feel free to change theme and/or google additional settings for this plugin if you wish
$("#message-logger .data").mCustomScrollbar({
set_width: '100%',
set_height: '100%',
horizontalScroll: false,
mouseWheelPixels: 50,
theme: "light",
/*"light", "dark", "light-2", "dark-2", "light-thick", "dark-thick", "light-thin", "dark-thin"*/
callbacks: {
onScroll: function() {
onScrollCallback();
}
}
}).find('.mCSB_container').css('padding', '5px 5px 5px 15px').css('marginRight', '10px');
$("#message-logger .data").mCustomScrollbar("update");
$("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
$("#message-logger .mCSB_draggerContainer").css('visibility', 'hidden');
$('#message-logger .savechat').on('click', function() {
if (startSaveOnGameEnd) {
$(window).on('beforeunload', function() {
exportChat();
});
$('#message-logger .data').fadeIn();
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") $("#message-logger .data").mCustomScrollbar("update");
$('#message-logger .mCSB_container').append('<span style="color: #cccccc;">Chat log will be downloaded at the end of the match.</span><br />');
// New message appended to chat container so update/inform custom scrollbar plugin of new lengths and auto scroll to bottom of chat container to show new message
if (typeof $("#message-logger .data").mCustomScrollbar !== "undefined") {
$("#message-logger .data").mCustomScrollbar("update");
if (autoscroll) $("#message-logger .data").mCustomScrollbar('scrollTo', 'bottom');
}
if (fadeDelay && $('#chat').is(':hidden')) {
clearTimeout(chatFade);
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
} else {
exportChat();
}
});
// On mouse over of chat container, show boundary box along with move/resize handles
$('#message-logger').on('mouseover', function() {
$('#message-logger .data').fadeIn();
clearTimeout(chatFade);
$(this).css('boxShadow', '0px 0px 0px 3px rgba(255,255,255,0.3)');
$(this).find('.draghandle').show();
$(this).find('.savechat').show();
$(this).find('.resizehandle').show();
$("#message-logger .mCSB_draggerContainer").css('visibility', 'visible');
}).on('mouseout', function() { // On mouse out hide boundary box and move/resize handles
if (fadeDelay && $('#chat').is(':hidden')) {
chatFade = setTimeout(function() {
$('#message-logger .data').fadeOut('slow');
}, fadeDelay);
}
$(this).css('boxShadow', 'none');
$(this).find('.draghandle').hide();
$(this).find('.savechat').hide();
$(this).find('.resizehandle').hide();
$("#message-logger .mCSB_draggerContainer").css('visibility', 'hidden');
}).drag({
handle: '.draghandle'
});
// Code to handle resizing of chat container
$('#message-logger .resizehandle').on('mousedown', function(e) {
$(document).on('mousemove.drag', function(e) {
var x = e.pageX - $('#message-logger').offset().left + 5 + parseInt($('#message-logger .resizehandle').css('marginRight')),
y = e.pageY - $('#message-logger').offset().top + 5 + parseInt($('#message-logger .resizehandle').css('marginRight'));
x = (x < 200) ? x = 200 : x = x;
y = (Math.round(y / resizeHeightSnap) < 3) ? 3 * resizeHeightSnap : Math.round(y / resizeHeightSnap) * resizeHeightSnap;
$('#message-logger').css('boxShadow', '0px 0px 0px 3px rgba(255,255,255,0.3)');
$('#message-logger').find('.draghandle').show();
$('#message-logger').find('.savechat').show();
$('#message-logger').find('.resizehandle').show();
$("#message-logger .mCSB_draggerContainer").css('visibility', 'visible')
$('#message-logger').css('width', x);
$('#message-logger').css('height', y);
$("#message-logger .data").mCustomScrollbar("update");
}).one('mouseup', function() {
$('#message-logger').css('boxShadow', 'none');
$('#message-logger').find('.draghandle').hide();
$('#message-logger').find('.savechat').hide();
$('#message-logger').find('.resizehandle').hide();
$("#message-logger .mCSB_draggerContainer").css('visibility', 'hidden');
setCookie('ce-width', parseInt($('#message-logger').css('width')), 365);
setCookie('ce-height', parseInt($('#message-logger').css('height')), 365);
$(this).off('mousemove.drag');
});
e.preventDefault();
});
});
// Variables to store player id and name of ingame player when clicking persons name in chat
var clickedid = null,
clickedname = null;
// Monitor click events on player name in chat
$('body').on('click', '.playeroptions', function(e) {
// Check if player options on click is on the page
if ($('#ec-options').length) {
// Show options where mouse was clicked
$('#ec-options').css('left', e.pageX - 60).css('top', e.pageY - 30).show();
} else {
// Show options where mouse was clicked
$('body').append('<div id="ec-options" style="position: absolute; left: ' + (e.pageX - 60) + 'px; top: ' + (e.pageY - 30) + 'px; z-index: 999; color: ##00FF00; text-shadow: 0px 0px 10px #000, 0px 0px 10px #000, 0px 0px 10px #000, -1px -1px 1px #000, 1px 1px 1px #000, -1px 1px 1px #000, 1px -1px 1px #000, 0px 0px 3px #000; white-space: nowrap; height: 70px; border: 1px solid black; font-size: 11px;"><div style="position: absolute; left: 0; top: 0; width: 100%; height: 100%; background-color: #fff; border: 1px solid #fff; opacity: .6; z-index: -1;"></div><div class="list" style="position: static; left: 0; top: 0; width: 100%; height: 100%;"> <a style="display: block; white-space: nowrap; font-weight: bold; text-align: center; padding: 4px; cursor: pointer;" class="ec-report">Report Player <span class="ec-name"></span></a> <hr style="opacity: .1; margin: 0;"> <a style="display: block; white-space: nowrap; font-weight: bold; text-align: center; padding: 4px; cursor: pointer;" class="ec-profile">Goto <span class="ec-name"></span> Profile</a> <hr style="opacity: .1; margin: 0;"> <a style="display: block; white-space: nowrap; font-weight: bold; text-align: center; padding: 4px; cursor: pointer;" class="ec-stats">Lookup <span class="ec-name"></span> TP-Stats</a></div></div>');
}
if ($(this).attr('pid') == undefined) {
$('.ec-report').css('cursor', 'default');
$('.ec-report').css('color', '#ccc');
} else {
$('.ec-report').css('cursor', 'pointer');
$('.ec-report').css('color', '#00FF00');
}
// Store player id and player name of clicked player incase user selects an option
clickedid = $(this).attr('pid');
clickedname = $(this).attr('pname');
//<span class="class="ec-name"></span>
$('#ec-options .ec-name').html(decodeURIComponent(clickedname));
});
// Hide options on mouseout of div
$('body').on('mouseleave', '#ec-options', function() {
$('#ec-options .ec-name').html('');
$(this).hide();
});
// Show report player prompt if "Report Player" option clicked
$('body').on('click', '#ec-options .ec-report', function() {
// Since we also make group chat player names clickable, only show the report prompt if clickedid contains an id which means its a user ingame
if (typeof clickedid !== "undefined") {
tagpro.kick.player(tagpro.players[clickedid]);
}
$('#ec-options .ec-name').html('');
$('#ec-options').hide();
});
// Lookup player on my site using tagpro-stats db to get tagpro profile id
$('body').on('click', '#ec-options .ec-profile', function() {
$.get('http://tagpro-stats.com/chat_lookup.php?name=' + clickedname, function(results) {
//results = $.parseJSON(data);
// If player found and has a tagpro profile id then open a popup to the users profile in another window
if (results['profile'] != null) {
window.open('http://tagpro-pi.koalabeast.com/profile/' + results['profile'], 'tagproprofile', 'height=747,width=857,menubar=no,location=no,status=no,toolbar=no');
} else {
// Player not found in DB so display message in chat saying so
$('#message-logger .mCSB_container').append('<span style="color: #999">Could not find the profile id for "' + decodeURIComponent(clickedname) + '".</span><br />');
}
});
$('#ec-options .ec-name').html('');
$('#ec-options').hide();
});
// Lookup player on my site using tagpro-stats db to get tagpro-stats id
$('body').on('click', '#ec-options .ec-stats', function() {
$.get('http://tagpro-stats.com/chat_lookup.php?name=' + clickedname, function(results) {
//results = $.parseJSON(data);
// If player found and has a tagpro-stats id then open a popup to the users tagpro-stats.com in another window
if (results['id'] != null) {
window.open('http://tagpro-stats.com/profile.php?userid=' + results['id'], 'tagprostats', 'height=747,width=857,menubar=no,location=no,status=no,toolbar=no');
} else {
// Player not found in DB so display message in chat saying so
$('#message-logger .mCSB_container').append('<span style="color: #999">Could not find the tagpro-stats id for "' + decodeURIComponent(clickedname) + '".</span><br />');
}
});
$('#ec-options .ec-name').html('');
$('#ec-options').hide();
});
}
// Execute fn when game has loaded, otherwise try again after a
// short delay.
function waitUntilGameLoaded(fn) {
// Check if game is loaded and connected to the server.
if (typeof tagpro !== 'undefined' &&
typeof tagpro.socket !== 'undefined' &&
tagpro.socket != null) {
fn();
} else {
setTimeout(function() {
waitUntilGameLoaded(fn);
}, 100);
}
}
// Call the function to check if game is loaded, players are inside, and execute our custom code
waitUntilGameLoaded(gameScript);
}
function ceGroupScript() {
// Helper function to load/inject external javascript onto page
function getScript(url, success) {
var script = document.createElement('script'),
head = document.getElementsByTagName('head')[0],
done = false;
script.src = url;
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
$('body').append('<style>#chat div a { color: #0000FF; }</style>');
// The main group script.
function groupScript() {
getScript('http://reanimated.net/tagpro/Autolinker.min.js', function() {
$('#chat').html(Autolinker.link($('#chat').html(), {
stripPrefix: false
}));
tagpro.group.socket.on('chat', function(data) {
$('#chat').html(Autolinker.link($('#chat').html(), {
stripPrefix: false
}));
});
$('#playerOptions a:eq(1)').after('<a href="cancel" id="lookupstats">Lookup TP-Stats</a>');
$('#playerOptions a:eq(1)').after('<a href="cancel" id="gotoprofile">Goto Profile</a>');
function leaderCheck() {
if (!$('li.leader.you').length) {
$('#playerOptions a:eq(1)').hide();
$('#playerOptions a:eq(0)').hide();
} else {
$('#playerOptions a:eq(1)').show();
$('#playerOptions a:eq(0)').show();
}
setTimeout(function() {
leaderCheck();
}, 1000);
}
leaderCheck();
var clickedname = null;
$('body').on('click', 'li', function(e) {
clickedname = $(this).contents().get(0).nodeValue;
$("#playerOptions").data("model", $(this).data("model")).css({
left: e.pageX - 10,
top: e.pageY - 10,
visibility: "visible"
});
$("#playerOptions").one('mouseleave', function() {
setTimeout(function() {
$("#playerOptions").css({
visibility: "hidden"
})
}, 500);
});
});
$('#gotoprofile').on('click', function() {
$.get('http://tagpro-stats.com/chat_lookup.php?name=' + clickedname, function(results) {
//results = $.parseJSON(data);
// If player found and has a tagpro profile id then open a popup to the users profile in another window
if (results['profile'] != null) {
window.open('http://tagpro-pi.koalabeast.com/profile/' + results['profile'], 'tagproprofile', 'height=747,width=857,menubar=no,location=no,status=no,toolbar=no');
} else {
// Player not found in DB so display message in chat saying so
$('#chat').append('<div style="color: #999">Could not find the profile id for "' + decodeURIComponent(clickedname) + '".</div>');
}
});
});
$('#lookupstats').on('click', function() {
$.get('http://tagpro-stats.com/chat_lookup.php?name=' + clickedname, function(results) {
//results = $.parseJSON(data);
// If player found and has a tagpro-stats id then open a popup to the users tagpro-stats.com in another window
if (results['id'] != null) {
window.open('http://tagpro-stats.com/profile.php?userid=' + results['id'], 'tagprostats', 'height=747,width=857,menubar=no,location=no,status=no,toolbar=no');
} else {
// Player not found in DB so display message in chat saying so
$('#chat').append('<div style="color: #999">Could not find the tagpro-stats id for "' + decodeURIComponent(clickedname) + '".</div>');
}
});
});
});
}
// Execute fn when group has loaded, otherwise try again after a
// short delay.
function waitUntilGroupLoaded(fn) {
if (typeof tagpro !== 'undefined' &&
typeof tagpro.group !== 'undefined' &&
typeof tagpro.group.socket !== 'undefined' &&
tagpro.group.socket != null && $('li').length) {
fn();
} else {
setTimeout(function() {
waitUntilGroupLoaded(fn);
}, 100);
}
}
waitUntilGroupLoaded(groupScript);
}
// Returns "group", "game", or false, depending on our location.
function getLocation() {
var url = w.location.href;
if (/http:\/\/tagpro-.+?\.koalabeast\.com:\d+?/.test(url) ||
/http:\/\/tangent\.jukejuice\.com:\d+?/.test(url) ||
/http:\/\/maptest2?\.newcompte\.fr:\d+?/.test(url)) {
return "game";
} else if (/http:\/\/tagpro-.+?\.koalabeast\.com\/groups\/.+?/.test(url) ||
/http:\/\/tangent\.jukejuice\.com\/groups\/.+?/.test(url) ||
/http:\/\/maptest2?\.newcompte\.fr\/groups\/.+?/.test(url)) {
return "group";
} else {
return false;
}
}
var siteLocation = getLocation();
if (siteLocation == "game") {
contentEval(ceGameScript);
} else if (siteLocation == "group") {
contentEval(ceGroupScript);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment