User:Tappity/chat.js

var PINGS = ['tappity', 'tap', 'slayze', 'taptap'];

var isInit = false; var missedMsgCount = 0; var oldWindowTitle = document.title;

var buildInMessage = function(attr) { return {title: '[IN] ' + getFirstWord(attr.text)}; };

var buildOutMessage = function(attr) { return {title: '[OUT] ' + getFirstWord(attr.text)}; };

var buildChatMessage = function(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; }   console.log(attr.text, missedMsgCount); return {title: userTitle, body: attr.text}; };

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 { //console.log(missedMsgCount); var addOn = missedMsgCount ? '[' + missedMsgCount + '] ' : ''; document.title = addOn + oldWindowTitle; };

var alertPing = function(chat) { console.log(chat, document.hasFocus); if (!isInit || chat.attributes.name == wgUserName || document.hasFocus) { if (chat.attributes.text == $.msg('chat-welcome-message', wgSiteName)) { isInit = true; }       return; }   var msg = getMessage(chat.attributes); // console.log(msg); var newNotification = 'body' in msg ? new Notification(msg.title, { body: msg.body }) : new Notification(msg.title); updateWindowTitle; };

$(document).ready(function {   // console.log('alertInit');    Notification.requestPermission;    mainRoom.model.chats.bind('afteradd', $.proxy(alertPing, mainRoom.viewDiscussion));    $(window).on('focus', function { missedMsgCount = 0; updateWindowTitle; }); });