User:Tappity/chat.js

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

/* set a variable to determine Focus or not. */ window.hasFocus = true; $(window).bind('focus', function { window.hasFocus = true; }); $(window).bind('blur', function { window.hasFocus = false; });

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 isMentioned = PINGS.some(function(p) {       return attr.text.toLowerCase.indexOf(p) !== -1;    }); if (isMentioned) { user = '[MENTION] ' + user; mainRoom.viewDiscussion.scrollToBottom; }   return {title: attr.name, 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 alertPing = function(chat) { if (chat.attributes.name === wgUserName || window.hasFocus) { return; }   var msg = getMessage(chat.attributes); new Notification(msg.title, { body: msg.body }); };

var alertInit = function { Notification.requestPermission; mainRoom.model.chats.bind('afteradd', $.proxy(alertPing, mainRoom.viewDiscussion)); };

$(document).ready(alertInit);