MediaWiki:ChatBlockButton/code.3.js

/* * ChatBlockButton, version 3 * Adds a block option to each dropdown of every user in chat when their name is clicked if they do not have moderator rights. * @author Ozank Cx * Cleanup and +i18n by Count of Howard using code by KockaAdmiralac */ (function {   var config = mw.config.get([ 'wgCanonicalSpecialPageName', 'wgUserLanguage', 'wgUserGroups', 'wgSiteName' ]);

if (       config.wgCanonicalSpecialPageName !== "Chat" &&        config.wgUserGroups.indexOf('sysop') === -1    ) { return; }

var i18n = { 'en': { //English menuLabel: 'Block', expires: 'Expires', reason: 'Reason', accountCreation: 'Prevent account creation', talkPage: 'Prevent this user from editing their own talk page while blocked', cBlockReason: 'Abusing multiple accounts', cBlockExpiry: 'infinite', blockAction: 'Block $1 from', buttonCancel: 'Cancel', buttonBlock: 'Block this user', blockSuccess: 'has been blocked!', blockFailure: 'Failed to block user:', blockUnknownError: 'unknown error' }   },    lang = i18n[config.wgUserLanguage] || i18n[config.wgUserLanguage.split('-')[0]] || i18n.en, blockReason = window.chatBlockReason || lang.cBlockReason, blockExpiry = window.chatBlockExpiry || lang.cBlockExpiry;

var formHTML = '' + ' ' +               ' ' + lang.expires + ' ' +                   '' + ' ' +               ' ' +                ' ' + lang.reason + ' ' +                   '' + ' ' +               ' ' +                '' + '' + lang.accountCreation + ' ' + ' ' +               '' + '' + lang.talkPage + ' ' + ' ' +       ' ',    alias = $.fn.show;

$.fn.show = function { alias.apply(this, arguments); $('.kick') .clone .addClass('block-custom') .removeClass('kick') .appendTo('.admin-actions'); $('.block-custom span').attr({           id: 'block-custom',            class: 'label'        }); $('.block-custom .label').text(lang.menuLabel); $('.block-custom').click(function {           var name = $(this).parents.eq(2).children('.info').children('ul').children('.username').text;            $(this).remove;            modal(name);        }); };

function inlineAlert(msg) { mainRoom.model.chats.add(new models.InlineAlert({text:msg})); $('[name="message"]').val('').removeAttr('disabled').focus; }

function modal(username) { mw.util.addCSS(           '#block-expiry,' +            '#block-reason {' +                'width: 98%;' +            '}'        ); $.showCustomModal(lang.blockAction.replace('$1', username) + ' ' + config.wgSiteName, formHTML, {           id: 'chat-admin-block',            width: 400,            buttons: [{                message: lang.buttonCancel,                handler: function {                    $('#chat-admin-block').closeModal;                }            }, {                message: lang.buttonBlock,                defaultButton: true,                handler: function {                    handler(username);                }            }]        }); }

function handler(username) { var block = { action: 'block', user: username, expiry: $('#block-expiry')[0].value || $('#block-expiry').attr('placeholder'), reason: $('#block-reason')[0].value || $('#block-reason').attr('placeholder'), nocreate: '', allowusertalk: '', autoblock: 0, format: 'json', token: mw.user.tokens.values.editToken };       if (!$('#block-nocreate')[0].checked) { delete block.nocreate; }       if ($('#block-restrtp')[0].checked) { delete block.allowusertalk; }

blockUser(block, username); }

function blockUser(param, username) { $.ajax({           url: mw.util.wikiScript('api'),            type: "POST",            dataType: "JSON",            data: param,            success: function(d) {                if (!d.error) {                    inlineAlert(username + ' ' + lang.blockSuccess);                    mainRoom.kick({ name: username });               } else {                    inlineAlert(lang.blockFailure + ' ' + d.error.code);                }            },            error: function {                inlineAlert(lang.blockUnknownError);            }        }); $('#chat-admin-block').closeModal; } });