User:Alex6122/chat.js

document.addEventListener('DOMContentLoaded', function {   var chat = document.getElementsByClassName( 'WikiaPage' )[0],        rail = document.getElementsByClassName( 'Rail' )[0],        textArea = document.getElementsByTagName( 'textarea' )[0];    //    chat.addEventListener('click', function( event ) { var target = event.target, message = target.closest( 'li' ); if ( message && !message.closest( '.Rail' ) &&           (target.classList.contains( 'username')  || target.tagName === 'IMG') ) { textArea.value += message.dataset.user + ', '; textArea.focus; }   });    rail.addEventListener( 'click', function( event ) { var user = event.target.closest( 'li' ); if ( user && event.which === 2 ) { textArea.value += user.dataset.user + ', '; textArea.focus; }   });    //    var pmAddSound = new Audio( 'http://vignette1.wikia.nocookie.net/siegenax/images/0/02/Jingle-bells-sms.ogg/revision/latest?cb=20151209133611&path-prefix=ru' ),        pmMessageSound = new Audio( 'http://vignette1.wikia.nocookie.net/siegenax/images/0/08/Sounds-937-job-done.ogg/revision/latest?cb=20151209142227&path-prefix=ru' ),        observeNewPm = function( mutationList ) {            var addedNodes,            newChat,            isSoundNotification,            i,            j;            for ( i = 0; i < mutationList.length; i++ ) {                addedNodes = mutationList[i].addedNodes;                for ( j = 0; j < addedNodes.length; j++ ) {                    newChat = addedNodes[j];                    if ( newChat.classList.contains( 'Chat' ) ) {                        pmObserver.observe(newChat.firstElementChild, { childList: true });                       if ( newChat.style.display === 'none' ) {                            newChat.firstMute = true;                            isSoundNotification = true;                        }                    }                }            }            if ( isSoundNotification )                pmAddSound.play;        },        pmTracking = function( mutationList ) {            var pmChat,            message,            isSoundNotification,            noMute,            i,            j;            for ( i = 0; i < mutationList.length; i++ ) {                pmChat = mutationList[i].target.parentElement;                for ( j = 0; j < mutationList[i].addedNodes.length; j++ ) {                    message = mutationList[i].addedNodes[j];                    if ( message.dataset.user ) {                        if ( !pmChat.firstMute && pmChat.style.display === 'none' || document.visibilityState === 'hidden')                           isSoundNotification = true;                        if ( pmChat.firstMute )                            noMute = true;                    }                }            }            if ( isSoundNotification )                pmMessageSound.play;            if ( noMute )                pmChat.firstMute = false;        },        chatObserver = new MutationObserver( observeNewPm ),        pmObserver = new MutationObserver( pmTracking );    chatObserver.observe(chat, { childList: true }); });