User:ZeHaffen/chat.js

var PINGS = ['ze', 'haffen', 'zehaffen']; var missedMsgCount = 0; var oldWindowTitle = document.title; var isInit = false; var decodeEntities = (function { // this prevents any overhead from creating the object each time  var element = document.createElement('div');  function decodeHTMLEntities (str) {    if(str && typeof str === 'string') {      // strip script/html tags      str = str.replace(/]*>([\S\s]*?)<\/script>/gmi, );      str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, );      element.innerHTML = str;      str = element.textContent;      element.textContent = '';    }    return str;  }  return decodeHTMLEntities; }); var buildInMessage = function(attr) { var username = decodeEntities(attr.text.match(regIn)[1]); var user = mainRoom.model.users.findByName(username); if ( user != null ) { //This is likely to be empty upon log in. Depending on how wikia works anyways. var avatar = user.attributes.avatarSrc.replace(/scale-to-width-down\/\d*/, ""); return {title: '[IN] ' + username, icon: avatar}; }	return {title: '[IN] ' + username}; }; var buildOutMessage = function(attr) { var username = decodeEntities(attr.text.match(regOut)[1]); var user = mainRoom.model.users.findByName(username); if ( user != null ) { var avatar = user.attributes.avatarSrc.replace(/scale-to-width-down\/\d*/, ""); return {title: '[OUT] ' + username, icon: avatar}; }	return {title: '[OUT] ' + username}; }; 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 avatarFull = attr.avatarSrc.replace(/scale-to-width-down\/\d*/, ""); return {title: userTitle, body: attr.text, icon: avatarFull}; }; var regIn = function { return new RegExp(wgMessages["chat-user-joined"].replace("$1", "(.*)")); }; var regOut = function { return new RegExp(wgMessages["chat-user-parted"].replace("$1", "(.*)")); }; var getMessage = function(attr) { if (attr.name) { return buildChatMessage(attr); } else if ( attr.text.match(regIn) ) { return buildInMessage(attr); } else if ( attr.text.match(regOut) ) { 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; });