MediaWiki:EscapeEmoticons/code.js

/* EscapeEmoticons (w:c:dev:EscapeEmoticons) * * Provides a checkbox for Special:Chat to automatically escape all emoticons on your messages, so they don't display as images when sent on chat. * Note: This script adds 4 characters to your message per emoticon, so if you're writing a message that's ~1000 characters and you're using a lot of emotes, this probably won't work. * Bonus: raw JS only. * For personal use only * @author: Dorumin */

(function {   if (wgCanonicalSpecialPageName != 'Chat') return;    // Function for escaping regular expressions    RegExp.escape = RegExp.escape || function(s) {        return s.replace(/[-\/\\^$*+?.|[\]{}]/g, '\\$&');    };    // Adds the checkbox to toggle escaping    var railWordmark = document.querySelector('#Rail h1.wordmark');    railWordmark.outerHTML = '  Escape emotes ' + railWordmark.outerHTML;    // Get the list of escaped emoticons    var emoteLines = wgChatEmoticons.split(/\n/g);    var escapedEmotesList = [];    for (var i in emoteLines) {       if (emoteLines[i].charAt(0) != '*') {/* do nothing :P */}       else if (emoteLines[i].charAt(1) == '*') escapedEmotesList.push(RegExp.escape(emoteLines[i].slice(2)));    }    // Escape the emotes before they're sent (if enabled)    var msgTextarea = document.querySelector('#Write .message textarea'); msgTextarea.addEventListener('keypress', function(e) {       if (document.getElementById('escapeEmoticonsCheckbox') === null) return; // Raw JavaScript hates exceptions.        var isChecked = document.getElementById('escapeEmoticonsCheckbox').checked;        var key = e.which || e.keyCode || 0;        if (key != 13 || !isChecked) return;        var msgVal = this.value;        var escapedEmotes = msgVal.replace(new RegExp(escapedEmotesList.join('|'), 'gi'), '$&');        this.value = escapedEmotes;    }); });