User:Monchoman45/ChatHacks.js

//Too bad this doesn't write over the functions properly. Going to have to figure out why. $(function {	if(wgPageName == 'Special:Chat') {		$('body').prepend('.chat-mod, .mod, .mod:after {color:#da0;} .chat-mod.away {color:#eda;} .ChatHeader .username, .me {color:#00c;} .UserStatsMenu {color:#000;} ');

//Add a 'go afk' button $('#Write').append('AFK'); //Unbind all of the listeners that set your status to back $(window).unbind('mousemove').unbind('focus').unbind('keypress'); //Set status to back when you press enter $('input[name=message]').bind('keypress', function {if(event.keyCode == 13) {NodeChatHelper.resetActivityTimer;}}); //Add a 'clear chat' button $('#Write').append('Clear'); window.mods = []; window.list = document.getElementById('Users').getElementsByTagName('ul')[0].getElementsByTagName('li'); for(i = 0; i < list.length; i++) { if(list[i].className.split(' ')[1] == 'chat-mod') { mods.push(list[i].id.substring(5, list[i].id.length)); }		}

//Add/remove mods when people join/leave NodeChatController.view.model.users._callbacks.add[0] = function(user) { if(user.attributes.isModerator == true) { var add = true; for(i in mods) {if(mods[i] == user.attributes.name) {add = false;}} if(add == true) {mods.push(user.attributes.name)} }			var view = new UserView({model: user}); $('#Users > ul').append(view.render.el); }		NodeChatController.view.model.users._callbacks.remove[0] = function(user) { NodeChatHelper.log("Trying to remove " + user.get('name') + " from the list."); NodeChatHelper.log("Matches found: " + $('#' + NodeChatHelper.liIdByUsername( user.get('name') )).length); for(i in mods) {if(mods[i] == user.attributes.name) {mods[i] = undefined;}} $('#' + NodeChatHelper.liIdByUsername( user.get('name') )).remove; }		//Color mod posts when they're added NodeChatController.view.model.chats._callbacks.add[0] = function (chat) { // Determine if chat view is presently scrolled to the bottom var isAtBottom = false; if (($("#Chat").scrollTop + 1) >= ($("#Chat ul").outerHeight - $("#Chat").height)) { isAtBottom = true; }			// Add message to chat var view = new ChatView({model: chat}); for(i in mods) {if(mods[i] == view.model.attributes.name) {view.model.attributes.name = ' ' + view.model.attributes.name + ' '}} if(wgUserName == view.model.attributes.name) {view.model.attributes.name = ' ' + view.model.attributes.name + ' '} $('#Chat ul').append(view.render.el);

// Scroll chat to bottom if (chat.attributes.name == wgUserName || isAtBottom) { NodeChatHelper.scrollToBottom; }		}

//Add timestamp to messages - doesn't work yet, have to find this object ref $('#Write').unbind('submit'); //unbind the default, because it uses 6 nested function calls that make it impossible to know what the object reference is		NodeChatController.view.sendMessage = function{ var inputField = $('input[name=message]'); var nameField = $('input[name=user_name]'); if (inputField.val) { //Preparse message - will return true or false, based on whether the message should still be sent or not var run = Preparse(inputField.val); if(run == true) { var date = new Date; if(date.getUTCHours >= 10) {var hours = date.getUTCHours;} else {var hours = '0' + date.getUTCHours;} if(date.getUTCMinutes >= 10) {var minutes = date.getUTCMinutes;} else {var minutes = '0' + date.getUTCMinutes;} chatEntry = new models.ChatEntry({name: nameField.val, text: '[' + hours + ':' + minutes + '] ' + inputField.val}); this.socket.send(chatEntry.xport); }				inputField.val(''); }			NodeChatHelper.focusTextInput; }		$('#Write').bind('submit', function{NodeChatController.view.sendMessage.call(NodeChatController.view);}); //rebind our new function, even though it'll throw an XHR POST error every time we send } });

function Preparse(input) { var text = input.split(' '); switch(text[0]) { case '/away': case '/afk': ToggleAway; return false; break; case '/clear': $('.Chat ul').html(''); return false; break; case '/kickban': //NodeChatController.view.kickBan(build(text, 1)); return false; break; case '/me': if(text[1] != undefined) {$('input[name="message"]').val('* ' + wgUserName + ' ' + build(text, 1));} else {$('input[name="message"]').val('* ' + wgUserName);} return true; break; default: return true; break; } }

//Parser helping function - takes an array of text created with .split(' '), //and an index number for where to start, then rebuilds the string. //Can also accept an index value for where to stop. function build(text, index, stop) { var newtext = ''; if(stop == undefined || stop == 0) {stop = text.length} else if(stop < 0) {stop += text.length} if(index == stop || index == text.length - 1) {return text[index];} for(var i = index; i < stop; i++) { newtext += text[i] + ' '; }	return newtext; }

function ToggleAway { if($('#ChatHeader .User').hasClass('away') == true) { NodeChatController.view.setBack; }	else { NodeChatController.view.setAway; } }