FANDOM


// Chat Options
importArticles({
    type: 'script',
    articles: [
        'u:dev:MediaWiki:ChatOptions/code.js',
        'w:c:User:Monchoman45/ChatHacks.js'
    ]
});
 
/**
 * Pinglist.
 * Using '\\b' is a barrier: word\b would trigger on forword but not on wordgame, etc. Other regex flags work on this.
 * Don't forget to double-slash, since single slashes in JS break things.
 */
var PING_LIST = [
    '(Username)',
    'Ping(?:2|)',
    'Ping(?:2|)',
    'Ping(?:2|)',
    'Ping(?:2|)',
    'Ping(?:2|)',
    'Ping(?:2|)',
    'Ping(?:2|)',
    'Ping(?:2|)'
];
 
/**
 * Ping sound.
 * Needs to be a valid URL to an .ogg or .mp3 file.
 */
var PING_SOUND = '(.ogg URL)';
 
/**
 * Ping color.
 * Needs to be a valid HTML color code, can be a color string e.g. "red" or a hex color e.g. "#FFF" or "#FFFFFF".
 */
var PING_COLOR = 'red';
 
// Ask our end-user if we want notifications for the script.
Notification.requestPermission();
 
// Function itself. This was watered down from a brilliant coder.
$(function() {
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'Chat') return;
    console.log("Pingchecking activated!");
    $('.Chat').on('DOMNodeInserted', function(e) {
        var msg = $.parseHTML(e.target.innerHTML)[7];
        var mee = $('.User .username').html();
        var mid = e.target.id;
        var usr = e.target.getAttribute('data-user');
 
        if (usr === mee || msg === void 0) return;
 
        for (var i = 0; i < PING_LIST.length; i++) {
            if (new RegExp(PING_LIST[i], 'im').test(msg.innerHTML)) {
                // Play the sound.
                $('<audio>', {
                    id: 'ping',
                    src: PING_SOUND,
                    autoplay: true
                }).appendTo('body');
 
                // Display desktop notifications.
                if (Notification.permission === 'granted') {
                    var notification = new Notification(usr + ' needs something!', {
                        body: msg.innerHTML.replace(/(<(img src[^>]*|(?:\/|)a[^>]*|(?:\/|)span[^>]*)>)/g, ''),
                        icon: $('#' + mid + ' .avatar').attr('src') 
                    });
                }
            }
            // Change the look of the triggered message.
            $('#' + mid + ' .message').html('<span style="color:' + PING_COLOR + ' !important; font-weight: bold !important; text-shadow: 0px 0px 10px ' + PING_COLOR + ' !important;">' + msg.innerHTML + '</span>');
            break;
        }
 
        // Terminate the sound.
        setTimeout(function() {
            if ($('#ping').length) $('#ping').remove();
        }, 1000);
    });
});

Note

You must copy and paste this in source editor.