Advertisement
Maulle

Chat Tags

Sep 21st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * '''ChatTags'''
  3.  *   By [[User:AnimatedCartoons]]
  4.  */
  5. // <syntaxhighlight lang="javascript">
  6. (function ($) {
  7.     'use strict';
  8.  
  9.     var disable = $.isArray(window.chatTagsDisable) ? window.chatTagsDisable : [];
  10.  
  11.     // Translations
  12.     var lng = {
  13.         // English
  14.         en: {
  15.             hide: 'Hide spoiler',
  16.             show: 'Show spoiler'
  17.         },
  18.         // Polski
  19.         pl: {
  20.             hide: 'Ukryj',
  21.             show: 'Pokaż'
  22.         },
  23.         // Português
  24.         'pt-br': {
  25.             hide: 'Esconder',
  26.             show: 'Mostrar'
  27.         },
  28.         // Español
  29.         es: {
  30.             hide: 'Ocultar spoiler',
  31.             show: 'Mostrar spoiler'
  32.         },
  33.         // Català
  34.         ca: {
  35.             hide: 'Amaga spoiler',
  36.             show: 'Mostra spoiler'
  37.         },
  38.         // Italiano
  39.         it: {
  40.             hide: 'Nascondere spoiler',
  41.             show: 'Mostrare spoiler'
  42.         },
  43.         // Français
  44.         fr: {
  45.             hide: 'Cacher spoiler',
  46.             show: 'Montrer spoiler'
  47.         },
  48.         // 日本の (Japanase)
  49.         ja: {
  50.             hide: '隠すスポイラー',
  51.             show: '表示スポイラー'
  52.         },
  53.         // русский  (Russian)
  54.         ru: {
  55.             hide: 'Скрыть спойлер',
  56.             show: 'Показать спойлер'
  57.         },
  58.         // Deutsch
  59.         de: {
  60.             hide: 'Spoiler ausblenden',
  61.             show: 'Spoiler anzeigen'
  62.         }
  63.     };
  64.  
  65.     lng = $.extend(lng.en, lng[mw.config.get('wgContentLanguage')]);
  66.  
  67.     mainRoom.model.chats.bind('afteradd', function (chat) {
  68.         var t = chat.attributes.text,
  69.             $m = $('#Chat_' + roomId + ' .message:last').html();
  70.  
  71.         if ($.inArray('*bg', disable) === -1) {
  72.             // Message line background
  73.             if (t.match(/(?=.*\[\*bg .*\])/gi)) {
  74.                 $m = $m.replace(/<a.*?\>/gi, '').replace(/<\/a\>/gi, '');
  75.  
  76.                 var bckgrnd = (/\[\*bg (.*?)\]/gi).exec($m);
  77.  
  78.                 if (bckgrnd[1].match(/^https?:\/\//gi)) {
  79.                     $('#Chat_' + roomId + ' li:last').css('background-image', 'url(' + bckgrnd[1] + ')');
  80.                 } else {
  81.                     $('#Chat_' + roomId + ' li:last').css('background-color', bckgrnd[1]);
  82.                 }
  83.  
  84.                 $m = $m.replace(/\[\*bg (.*?)\]/gi, '');
  85.             }
  86.         }
  87.  
  88.         if ($.inArray('b', disable) === -1) {
  89.             // Boldface
  90.             if (t.match(/(?=.*\[b\])(?=.*\[\/b\])/gi)) {
  91.                 $m = $m.replace(/\[b\]/gi, '<span style="font-weight: bold">').replace(/\[\/b\]/gi, '</span>');
  92.             }
  93.         }
  94.  
  95.         if ($.inArray('bg', disable) === -1) {
  96.             // Text background
  97.             if (t.match(/(?=.*\[bg .*\])(?=.*\[\/bg\])/i)) {
  98.                 $m = $m.replace(/\[bg (.*?)\]/gi, '<span style="background-color: $1">').replace(/\[\/bg\]/gi, '</span>');
  99.             }
  100.         }
  101.  
  102.         if ($.inArray('big', disable) === -1) {
  103.             // Big
  104.             if (t.match(/(?=.*\[big\])(?=.*\[\/big\])/gi)) {
  105.                 $m = $m.replace(/\[big\]/gi, '<span style="font-size: larger">').replace(/\[\/big\]/gi, '</span>');
  106.             }
  107.         }
  108.  
  109.         if ($.inArray('c', disable) === -1) {
  110.             // Color
  111.             if (t.match(/(?=.*\[c .*\])(?=.*\[\/c\])/i)) {
  112.                 $m = $m.replace(/\[c (.*?)\]/gi, '<span style="color: $1">').replace(/\[\/c\]/gi, '</span>');
  113.             }
  114.         }
  115.  
  116.         if ($.inArray('f', disable) === -1) {
  117.             // Font
  118.             if (t.match(/(?=.*\[f .*\])(?=.*\[\/f\])/i)) {
  119.                 $m = $m.replace(/\[f (.*?)\]/gi, '<span style="font-family: $1">').replace(/\[\/f\]/gi, '</span>');
  120.             }
  121.         }
  122.  
  123.         if ($.inArray('i', disable) === -1) {
  124.             // Italicize
  125.             if (t.match(/(?=.*\[i\])(?=.*\[\/i\])/i)) {
  126.                 $m = $m.replace(/\[i\]/gi, '<span style="font-style: italic">').replace(/\[\/i\]/gi, '</span>');
  127.             }
  128.         }
  129.  
  130.         if ($.inArray('img', disable) === -1) {
  131.             // Image
  132.             if (t.match(/(?=.*\[img .*\])/i)) {
  133.                 $m = $m.replace(/<a.*?\>/gi, '').replace(/<\/a\>/gi, '').replace(/\[img (.*?)\]/gi, '<a href="$1"><img src="$1" style="max-height: 200px; max-width: 200px" /></a>');
  134.             }
  135.         }
  136.  
  137.         if ($.inArray('p', disable) === -1) {
  138.             // Preformatted
  139.             if (t.match(/(?=.*\[p\])(?=.*\[\/p\])/i)) {
  140.                 $m = $m.replace(/\[p\]/gi, '<pre>').replace(/\[\/p\]/gi, '</pre>');
  141.             }
  142.         }
  143.  
  144.         if ($.inArray('s', disable) === -1) {
  145.             // Strikethrough
  146.             if (t.match(/(?=.*\[s\])(?=.*\[\/s\])/i)) {
  147.                 $m = $m.replace(/\[s\]/gi, '<span style="text-decoration: line-through">').replace(/\[\/s\]/gi, '</span>');
  148.             }
  149.         }
  150.  
  151.         if ($.inArray('sc', disable) === -1) {
  152.             // SoundCloud
  153.             if (t.match(/(?=.*\[sc .*\])/i)) {
  154.                 $m = $m.replace(/\[sc (.*?)\]/gi, '<iframe width="50%" height="120" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/$1&amp;color=ff5500&amp;auto_play=false&amp;hide_related=false&amp;show_artwork=true"></iframe>');
  155.             }
  156.         }
  157.  
  158.         if ($.inArray('small', disable) === -1) {
  159.             // Small
  160.             if (t.match(/(?=.*\[small\])(?=.*\[\/small\])/gi)) {
  161.                 $m = $m.replace(/\[small\]/gi, '<small>').replace(/\[\/small\]/gi, '</small>');
  162.             }
  163.         }
  164.  
  165.         if ($.inArray('sp', disable) === -1) {
  166.             // Spoiler
  167.             if (t.match(/(?=.*\[sp\])(?=.*\[\/sp\])/gi)) {
  168.                 $m = $m.replace(/\[sp\]/gi, '<button id="spoil">Spoiler</button>&nbsp;<span id="spoil2" style="display: none">').replace(/\[\/sp\]/gi, '</span>');
  169.             }
  170.         }
  171.  
  172.         if ($.inArray('sub', disable) === -1) {
  173.             // Subscript
  174.             if (t.match(/(?=.*\[sub\])(?=.*\[\/sub\])/i)) {
  175.                 $m = $m.replace(/\[sub\]/gi, '<sub>').replace(/\[\/sub\]/gi, '</sub>');
  176.             }
  177.         }
  178.  
  179.         if ($.inArray('sup', disable) === -1) {
  180.             // Superscript
  181.             if (t.match(/(?=.*\[sup\])(?=.*\[\/sup\])/i)) {
  182.                 $m = $m.replace(/\[sup\]/gi, '<sup style="vertical-align: top">').replace(/\[\/sup\]/gi, '</sup>');
  183.             }
  184.         }
  185.  
  186.         if ($.inArray('u', disable) === -1) {
  187.             // Underline
  188.             if (t.match(/(?=.*\[u\])(?=.*\[\/u\])/i)) {
  189.                 $m = $m.replace(/\[u\]/gi, '<span style="text-decoration: underline">').replace(/\[\/u\]/gi, '</span>');
  190.             }
  191.         }
  192.  
  193.         if ($.inArray('yt', disable) === -1) {
  194.             // YouTube
  195.             if (t.match(/(?=.*\[yt .*\])/i)) {
  196.                 $m = $m.replace(/\[yt (.*?)\]/gi, '<iframe width="400" height="215" src="http://youtube.com/embed/$1?autohide=1&rel=0" frameborder="0" allowfullscreen />');
  197.             }
  198.         }
  199.  
  200.         $('#Chat_' + roomId + ' .message:last').html($m);
  201.     });
  202.  
  203.     $('body').on('click', '#spoil', function () {
  204.         var $s = $(this).siblings('#spoil2');
  205.  
  206.         if ($($s).is(':hidden')) {
  207.             $(this).text(lng.hide);
  208.             $($s).show();
  209.         } else {
  210.             $(this).text(lng.show);
  211.             $($s).hide();
  212.         }
  213.     });
  214. }(this.jQuery));
  215. // </syntaxhighlight>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement