User:Fille Magique Numéro 1/chat.js

/*  * ChatHacks.js, by Monchoman45. * Version 7.0 *  * Features: *   Ability to clear your chat window *   A host of slash commands *   Ability to create multi user private chats *   Inline alerts every time your away status changes *   Icons next to messages by staff and chat mods in the room *   Pings when someone mentions your name or any other specified phrases */

/*  * ChatHack_Redux.js, by Captain Barracuda * Features: *     Is a light version of the original ChatHacks.js by Monchoman45. *     Supports regex for pings. */

if(wgCanonicalSpecialPageName == 'Chat') { // Checks that the current page is the chat page window.hasFocus = true; window.dinged = false; window.ding = 0; window.titleorig = document.title; function Unding { //for fixing the title after you've been dinged document.getElementsByTagName('title')[0].innerHTML = window.titleorig; clearInterval(window.ding); window.dinged = false; }    $(window).bind('focus', Unding); $(window).bind('focus', function {window.hasFocus = true;}); $(window).bind('blur', function {window.hasFocus = false;}); console.log("ChatHack_Redux has been loaded");

//Function for adding messages to the window NodeChatDiscussion.prototype.iconPing = function (chat) { var icon = ''; for(var i in this.model.users.models) { if(this.model.users.models[i].attributes.name == chat.attributes.name) { if(this.model.users.models[i].attributes.isStaff) { icon = ' '; }                else if(this.model.users.models[i].attributes.isModerator) { icon = ' '; }                break; }        }        if(icon) {this.chatUL.children.last.children('.username').html(this.chatUL.children.last.children('.username').html + icon);}

if(mainRoom.isInitialized && chat.attributes.name != wgUserName && !chat.attributes.isInlineAlert) { window.dinged = true; //resolve HTML var text = chat.attributes.text; var pings = document.getElementById('pings').value.removeTrailing('\n').split('\n'); for(var i = 0; i < pings.length; i++) { // PING //if(text(pings[i].toLowerCase.indexOf.toLowerCase) != -1) { if (mustPing(text, pings[i])) { if(!window.hasFocus) { //Only annoy people if the window isn't focused document.getElementById('sound').innerHTML = ' '; if(!window.dinged) { window.ding = setInterval('FlashTitle', 500); }                    }                    this.scrollToBottom; if(this == mainRoom.viewDiscussion) { var ref = text.toLowerCase.indexOf(pings[i].toLowerCase); var phrase = text.slice(ref, ref + pings[i].length); this.chatUL.children.last.children('.message').addClass('ping'); }                    break; }            }        }

//Kill emoticons this.chatUL.children.last.children('.message img').each(function {this.outerHTML = '' + this.title + ' ';}); }    mainRoom.model.chats.bind('afteradd', $.proxy(mainRoom.viewDiscussion.iconPing, mainRoom.viewDiscussion));

//Away status functions NodeChatController.prototype.setAway = function (msg){ if(!msg) {var msg = '';} $.log("Attempting to go away with message: " + msg); var setStatusCommand = new models.SetStatusCommand({            statusState: STATUS_STATE_AWAY,            statusMessage: msg        }); this.inlineAlert(i18n['away']); this.socket.send(setStatusCommand.xport); }    NodeChatController.prototype.setBack = function { if( !this.comingBackFromAway ) { // if we have sent this command (but just haven't finished coming back yet), don't keep spamming the server w/this command $.log("Telling the server that I'm back."); this.comingBackFromAway = true; var setStatusCommand = new models.SetStatusCommand({                statusState: STATUS_STATE_PRESENT,                 statusMessage: ''             }); this.inlineAlert(i18n['back']); this.socket.send(setStatusCommand.xport); }    }

//Send message function NodeChatController.prototype.sendMessage = function (event) { if(!this.active) { return true; }

if (event.which == 13 && !event.shiftKey) { event.preventDefault; mainRoom.resetActivityTimer; var inputField = this.viewDiscussion.getTextInput; var send = Preparse.call(this, inputField.val);

if (inputField.val && send) { var chatEntry = new models.ChatEntry({roomId: this.roomId, name: wgUserName, text: inputField.val}); if( this.isPrivate == true ) { //is prive if( this.afterInitQueue.length < 1 || this.model.users.length < 2 ){ this.mainController.socket.send( this.model.privateRoom.xport ); }                    if( !this.isInitialized  ) { this.afterInitQueue.push(chatEntry.xport); //temp chat entry in case of slow connection time chatEntry.set({temp : true, avatarSrc: wgAvatarUrl }); this.model.chats.add(chatEntry); } else { this.socket.send(chatEntry.xport); }                } else { this.socket.send(chatEntry.xport); }

event.preventDefault; }             inputField.val(''); $.log('submitting form'); inputField.focus; }    }    mainRoom.viewDiscussion.unbind('sendMessage'); mainRoom.viewDiscussion.bind('sendMessage', $.proxy(mainRoom.sendMessage, mainRoom));

//Functions that have to be changed for interwiki chat NodeChatController.prototype.openPrivateRoom = function(users) { users.push( wgUserName ); $.ajax({            type: 'POST',            url: wgScript + '?action=ajax&rs=ChatAjax&method=getPrivateRoomID',            data: {                users : users.join(',')            },            success: $.proxy(function(data) { $.log("Attempting create private room with users " + users.join(',')); var data = new models.OpenPrivateRoom({roomId: data.id, users: users}); this.baseOpenPrivateRoom(data, true); this.showRoom(data.get('roomId') ); this.chats.privates[ data.get('roomId') ].isPrivate = true; this.chats.privates[ data.get('roomId') ].init; //this.socket.send(data.xport); }, this)        }); this.viewUsers.hideMenu; }

NodeRoomController.prototype.onJoin = function(message) { var joinedUser = new models.User; joinedUser.mport(message.joinData);

var connectedUser = this.model.users.findByName(joinedUser.get('name'));

if(typeof connectedUser == "undefined"){ this.model.users.add(joinedUser); this.fire('afterJoin', joinedUser);

//TODO: move it to other class if( !this.isPrivate ) { // Create the inline-alert (on client side so that we only display it if the user actually IS new to the room and not just disconnecting/reconnecting). var newChatEntry = new models.InlineAlert({text: $.msg('chat-user-joined', [joinedUser.get('name')] ) }); this.model.chats.add(newChatEntry); }

this.disableRoom(joinedUser, false); } else { // The user is already in the room... just update them (in case they have changed). this.model.users.remove(connectedUser); this.model.users.add(joinedUser); }            }    NodeRoomController.prototype.onPart = function(message) { var partedUser = new models.User; partedUser.mport(message.data);

var connectedUser = this.model.users.findByName(partedUser.get('name'));

if(typeof connectedUser != "undefined"){

//TODO: move it to other class if( !this.isPrivate ) { var newChatEntry = new models.InlineAlert({text: $.msg('chat-user-parted', [connectedUser.get('name')] ) }); this.model.chats.add(newChatEntry); }

this.model.users.remove(connectedUser); this.disableRoom(connectedUser, true); }            }    NodeRoomController.prototype.disableRoom = function(user, flag) { if( this.isPrivate == false ) { //TODO: fix it for multiuser priv chat var privateUser =  this.model.privateUsers.findByName(user.get('name'));

if(typeof privateUser != "undefined"){ var roomId = privateUser.get('roomId'); if( typeof( this.chats.privates[roomId] ) != "undefined" ){ this.chats.privates[roomId].model.room.set({                         'blockedMessageInput': flag                    }); }                //try to reconnect if(flag === false && this.chats.privates[roomId].model.chats.length > 0) { this.socket.send( this.chats.privates[roomId].model.privateRoom.xport ); }            }        }    }

NodeChatController.prototype.showRoom = function(roomId) { $.log(roomId); if( this.activeRoom == roomId ) { return false; }

this.activeRoom = roomId; if(roomId == 'main') { this.chats.main.setActive(true); } else { this.chats.main.setActive(false); }

for(var i in this.chats.privates) { if(i == roomId) { this.chats.privates[i].setActive(true); } else { this.chats.privates[i].setActive(false); }        }

for(var i in this.chats.opens) { if(i == roomId) { this.chats.opens[i].setActive(true); } else { this.chats.opens[i].setActive(false); }        }        return true; }

NodeChatController.prototype.openPublicRoom = function(roomId) { this.chats.opens[roomId] = new NodeRoomController(roomId); this.chats.opens[roomId].mainController = this; //set main controller for this chat room this.showRoom(roomId); this.chats.opens[roomId].init; }

NodeChatController.prototype.inlineAlert = function(text) { for(var i in text.split('\n')) { this.viewDiscussion.chatUL.append(' ' + text.split('\n')[i] + ' '); }        this.viewDiscussion.scrollToBottom; }

function FlashTitle { if(document.getElementsByTagName('title')[0].innerHTML == window.titleorig) { document.getElementsByTagName('title')[0].innerHTML = i18n['activity'].replace(/\$1/g, wgSiteName); }        else { document.getElementsByTagName('title')[0].innerHTML = window.titleorig; }    }

//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(msg) { if(!msg) {var msg = '';} if($('#ChatHeader .User').hasClass('away') == true) { mainRoom.setBack; }        else { mainRoom.setAway(msg); }    }    toggleAway.back = function { //Force back status if($('#ChatHeader .User').hasClass('away') == true) {mainRoom.setBack;} }    toggleAway.away = function(msg) { //Force away status if(!msg) {var msg = '';} if($('#ChatHeader .User').hasClass('away') == false) {mainRoom.setAway(msg);} }

NodeChatController.prototype.clearWindow = function { this.viewDiscussion.chatUL.html(''); this.inlineAlert(i18n['cleared']); }

function active { //Returns the NodeChatController for the active window if(mainRoom.activeRoom && mainRoom.activeRoom != 'main') {return mainRoom.chats.privates[mainRoom.activeRoom];} else {return mainRoom;} }

String.prototype.removeTrailing = function(char) { //Remove extraneous characters var str = this; while(str.charAt(0) == char) {str = str.substring(1, str.length);} while(str.charAt(str.length - 1) == char) {str = str.substring(0, str.length - 1);} return str; }

function createCookie(name,value,days) { if (days) { var date = new Date; date.setTime(date.getTime+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString; }        else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }

function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); }        return null; }

$(function {        if(!importScriptURI) {function importScriptURI(a){var b=document.createElement("script");b.setAttribute("src",a);b.setAttribute("type","text/javascript");document.getElementsByTagName("head")[0].appendChild(b);return b }}         importScriptURI('http://c.wikia.com/index.php?title=User:Monchoman45/ChatHacks.js/i18n.js&action=raw&ctype=text/javascript');        importScriptURI('http://monchbox.wikia.com/index.php?title=MediaWiki:APIQuery.js&action=raw&ctype=text/javascript'); //APIQuery for /unban

$('head').append('\n.stafficon {\n\tmargin-bottom:-4px;\n\twidth:14px;\n\theight:14px;\n}\n.modicon {\n\tmargin-bottom:-3px;\n\twidth:14px;\n\theight:14px;\n}\n.UserStatsMenu {\n\tcolor:#000;\n}\n.Write [name="message"] {\n\twidth:93%;\n}\n.inline-alert span {\n\tcolor:#006CB0;\n\tcursor:pointer;\n}\n.inline-alert span:hover {\n\ttext-decoration:underline;\n}\n#pingspan {\n\tposition:absolute;\n\tz-index:5;\n\ttop:23px;\n\tmargin-left:15px;\n\tfont-size:15px;\n\tfont-weight:normal;\n\tline-height:15px;\n}\n#pings {\n\tresize:none;\n\tmargin-left:0;\n\tfont-size:12px;\n\theight:100px;\n\tdisplay:block;\n}\n#pingspan div span {\n\tfont-size:55%;\n}\n#ChatHeader {\n\tz-index:9999;\n}\n#ChatHeader {\n\tz-index:9999;\n}\n.message.ping {\n\tcolor:red;\n}\n ');

//Unbind all of the window listeners that set your status to back $(window).unbind('mousemove').unbind('focus').unbind('keypress'); //Add the sound space $('body').append('  '); });

// CONTENT ADDED BY THE REDUX VERSION const REGEX_CHECKER = new RegExp('^/.+/$');

function mustPing(text, ping) { /*         * Two forms of ping text: *     - Regex (starts and end with /) *     - Non-Regex */        if (REGEX_CHECKER.test(ping)) { return ping.test(text); } else if (text.toLowerCase.indexOf(ping.toLowerCase) != -1) { return true; } else { return false; }    } } else { $(function {        var a = document.getElementsByTagName('a');        for(var i = 0; i < a.length; i++) {            if(a[i].href && a[i].href.indexOf('/wiki/Special:Chat') != -1) {                a[i].addEventListener('click', function(event) {event.preventDefault; OpenChatWindow;});                a[i].removeAttribute('data-canonical');            }        }        if(document.body.className.indexOf('skin-oasis') != -1) {window.chatcheck = setInterval('ChatCheck', 200);}    });

function ChatCheck { if($('.chat-join button').length != 0) { $('.chat-join button').replaceWith('' + $('.chat-join button').html + ''); clearInterval(window.chatcheck); }    }

function OpenChatWindow { window.chatwindow = window.open('/wiki/Special:Chat?useskin=wikia', 'chat'); window.chatwindow.onload = function { window.chatwindow.importScriptPage('User:' + wgUserName + '/global.js', 'c'); window.chatwindow.importScript('User:' + wgUserName + '/wikia.js'); }    } }