User:Pbeaudette/ChatTags/code.js

/** * ChatTags - A BBCode parser for Wikia Special:Chat * * Version v2.4.2 * * Copyright (c) 2013 - 2015 Maria Williams (Shining-Armor) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */

var chatags = typeof chatags !== 'undefined' ? chatags : {}; chatags.images = typeof chatags.images !== 'undefined' ? chatags.images : false; chatags.videos = typeof chatags.videos !== 'undefined' ? chatags.videos : false; chatags.css = '.chatags-image{max-width:300px;max-height:300px;}'; chatags.tags = { 'b':    function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/b]', ' '); } else { s = s.replace('[b]', ' '); }                return s;             }, 'bg':   function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/bg]', ' '); } else { try { t = t.split('="');                        t[1] = t[1].replace('"', ''); s = s.replace('[bg="' + t[1] + '"]', ''); } catch(e) { console.log(e) } }                return s;             }, 'big':  function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/big]', ' '); } else { s = s.replace('[big]', ' '); }                return s;             }, 'c':    function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/c]', ' '); } else { try { t = t.split('="');                        t[1] = t[1].replace('"', ''); s = s.replace('[c="' + t[1] + '"]', ''); } catch(e) { console.log(e) } }                return s;             }, 'code': function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/code]', ' '); } else { s = s.replace('[code]', ' '); }                return s;             }, 'font': function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/font]', ' '); } else { try { t = t.split('="');                        t[1] = t[1].replace('"', ''); s = s.replace('[font="' + t[1] + '"]', ''); } catch(e) { console.log(e) } }                return s;             }, 'i':    function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/i]', ' '); } else { s = s.replace('[i]', ' '); }                return s;             }, 'img':  function (s,t) { if (chatags.images !== true) return s;                if (t.charAt(0) !== '/') { try { t = t.split('="');                        t[1] = t[1].replace('"', ''); s = s.replace('[img="' + t[1] + '"]', ''); } catch(e) { console.log(e) } }                return s;             }, 'small': function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/small]', ' '); } else { s = s.replace('[small]', ' '); }               return s;            }, 's':   function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/s]', ' '); } else { s = s.replace('[s]', ' '); }               return s;            }, 'sub': function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/sub]', ' '); } else { s = s.replace('[sub]', ' '); }               return s;            }, 'sup': function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/sup]', ' '); } else { s = s.replace('[sup]', ' '); }               return s;            }, 'u':   function (s,t) { if (t.charAt(0) === '/') { s = s.replace('[/u]', ' '); } else { s = s.replace('[u]', ' '); }               return s;            }, 'yt':  function (s,t) { if (chatags.videos !== true) return s;                if (t.charAt(0) !== '/') { try { t = t.split('="');                        t[1] = t[1].replace('"', ''); s = s.replace('[yt="' + t[1] + '"]', ' '); } catch(e) { console.log(e) } }                return s;             } }; chatags.parser = function (s) { var t = s.match(/\[([^\[\];]*)\]/g); var tg = ''; if (!t) return s;   for (var i = 0; i < t.length; i++) { tg = t[i].match(/\[\/?([^\[\]]*)\]/)[1]; try { tg = tg.split('="')[0];       } catch(e) { console.log(e) }        if (typeof chatags.tags[tg] !== 'undefined') {            s = chatags.tags[tg](s, t[i].replace('[', ).replace(']', ));        }    }    return s; }; chatags.init = function {    if (typeof window.mainRoom !== 'undefined') {        $('head').append(' ' + chatags.css + ' ');        window.mainRoom.model.chats.bind("afteradd", function(c) {            if (typeof window.mainRoom.roomId === "undefined")                return;            var string = $("#Chat_" + window.mainRoom.roomId + " .message:last").html;            string = chatags.parser(string);            $("#Chat_" + window.mainRoom.roomId + " .message:last").html(string);        });    } }; $(document).ready(function {    chatags.init;    $('.Chat ul').append('An important notice regarding ChatTags can be found here'); });