User:WillThaSerpent/chat.js

// Load the block list. var BLOCK_LIST = []; if (localStorage.getItem("Block-List") !== null) { BLOCK_LIST = JSON.parse(localStorage.getItem("Block-List")); for (i = 0; i < BLOCK_LIST.length; i++) { $("head").append("li[data-user='" + BLOCK_LIST[i] + "'] { display:none; } "); } } // Ping list. var PING_LIST = [ "\\ba+(?:k+r+|k+v+|r+k+i+)" ]; // Load up the ping sound and color. var PING_SOUND = 'http://soundbible.com/mp3/PINGAS-Richard-89282878.mp3'; var PING_COLOR = "cyan"; // Ping check method. function pingCheck(chat) { var text = chat.attributes.text; var name = chat.attributes.name; var isInline = chat.attributes.isInlineAlert || false; var context = mainRoom.viewDiscussion.chatUL.children.last; var chatMain = mainRoom.viewDiscussion.chatDiv[0]; // Check if it is worth checking. if (chatMain.scrollHeight - chatMain.scrollTop - chatMain.clientHeight < 500 && document.hasFocus) return; if (BLOCK_LIST.indexOf(name) > -1 || isInline) return; for (var i = 0; i < PING_LIST.length; i++) { if (new RegExp(PING_LIST[i], "mi").test(text) === true) { // Display desktop notification. if (Notification.permission === 'granted') { var notification = new Notification(name + " mentioned you!", {                   body: text.replace(/(<(img src[^>]*|(?:\/|)a[^>]*|(?:\/|)span[^>]*)>)/g, ""),                    icon: context.children('.avatar').attr('src').slice(0, -2) + '150'                }); notification.onclick = function { window.focus; mainRoom.viewDiscussion.scrollToBottom; notification.close; };           }            // Play the sound. $(' ', {               id: 'ping',                src: PING_SOUND,                autoplay: true            }).appendTo('body'); // Highlight the text. $("#" + context.attr('id') + " .message").html("" + text + "<\/span>"); break; }   }    // Remove the ping after a second. setTimeout(function {       if ($('#ping').length) $('#ping').remove;    }, 1000); } // Blocks a specific username by using CSS. function block(username) { BLOCK_LIST.push(username); $("head").append("li[data-user='" + username + "'] { display:none; } "); localStorage.setItem("Block-List", JSON.stringify(BLOCK_LIST)); } // Unblocks a specific username by using CSS. function unblock(username) { for (i = 0; i < BLOCK_LIST.length; i++) { if (BLOCK_LIST[i] === username) { $("head").append("li[data-user='" + BLOCK_LIST[i] + "'] { display:block; } "); BLOCK_LIST[i] = undefined; }		localStorage.setItem("Block-List", JSON.stringify(BLOCK_LIST)); } } // Fix "mainRoom is not defined" var interval = setInterval(function {   if (window.mainRoom) {        Notification.requestPermission;        mainRoom.model.chats.bind('afteradd', pingCheck);        clearInterval(interval);    } }, 250);