User:TUSF/chat.js

var PINGS = ['tusf', 'loli', 'pedo']; var missedMsgCount = 0; var oldWindowTitle = document.title; var isInit = false; var buildInMessage = function(attr) { var avatarSmall = "scale-to-width-down/28" var avatarFull = attr.avatarSrc.substr(0,attr.avatarSrc.length-avatarSmall.length) return {title: '[IN] ' + attr.name, icon: avatarFull}; }; var buildOutMessage = function(attr) { return {title: '[OUT] ' + getFirstWord(attr.text)}; }; var buildChatMessage = function(attr) { //console.log(JSON.stringify(attr)); var userTitle = attr.name; var isMentioned = PINGS.some(function(p) {       return attr.text.toLowerCase.indexOf(p) !== -1;    }); if (isMentioned) { userTitle = '[MENTION] ' + userTitle; mainRoom.viewDiscussion.scrollToBottom; }   if (!document.hasFocus) { missedMsgCount += 1; }   var avatarSmall = "scale-to-width-down/28" var avatarFull = attr.avatarSrc.substr(0,attr.avatarSrc.length-avatarSmall.length) return {title: userTitle, body: attr.text, icon: avatarFull}; }; var getFirstWord = function(text) { return text.substr(0, text.indexOf(' ')); }; var getMessage = function(attr) { if (attr.name) { return buildChatMessage(attr); } else if (attr.text.endsWith('is now trapped with us!')) { return buildInMessage(attr); } else { return buildOutMessage(attr); } }; var updateWindowTitle = function { var addOn = missedMsgCount ? '[' + missedMsgCount + '] ' : ''; document.title = addOn + oldWindowTitle; }; var alertPing = function(chat) { console.log('start - alertPing'); console.log(JSON.stringify(chat)); //console.log(chat, document.hasFocus); if (chat.attributes.name == wgUserName || document.hasFocus) { return; }   var msg = getMessage(chat.attributes); var newNotification = ( 'body' in msg || 'icon' in msg ) ? new Notification(msg.title, msg) : new Notification(msg.title); updateWindowTitle; console.log('end - alertPing'); }; $(document).ready(function {   // skip first few messages if any    setTimeout(function { // avoid double loading if (!isInit) { console.log('binded - alertPing'); mainRoom.model.chats.bind('afteradd', $.proxy(alertPing, mainRoom.viewDiscussion)); isInit = true; }   }, Math.floor(Math.random * (3000 - 2000 + 1)) + 2000); }); Notification.requestPermission; $(window).on('focus', function { missedMsgCount = 0; updateWindowTitle; });