User:Lil' Miss Rarity/ChatBlock.js

var ButtonStyle = 'position:absolute; right:54px; bottom:2px;'; var BlockButton = document.createElement('a'); BlockButton.setAttribute('class', 'wikia-button'); BlockButton.setAttribute('id', 'chat-block-use'); BlockButton.setAttribute('style', ButtonStyle); BlockButton.textContent = 'Block!';

var ChatBar = document.getElementById('Write'); ChatBar.appendChild(BlockButton);

var FormHTML = '\  \ \       Username to block: \  \ \       \        Length of block: \  \ \       \        Reason of block: \  \ \       \        Disable account creation? \       Disable talk page editing? \   \ ';

document.getElementById('chat-block-use').addEventListener('click', function {    $.showCustomModal('Block User', FormHTML, { id: 'chat-block', width: 400, buttons: [{ message: 'Block', defaultButton: true, handler: function { var block = { action: 'block', user: document.getElementById('block-name').value, expiry: document.getElementById('block-expiry').value, reason: document.getElementById('block-reason').value, nocreate: '', allowusertalk: '', autoblock: 0, format: 'json', token: mw.user.tokens.values.editToken };

if (!document.getElementById('block-nocreate').checked) { delete block.nocreate; }

if (document.getElementById('block-restrtp').checked) { delete block.allowusertalk; }

$.ajax({                   url: "/api.php",                    type: "POST",                    dataType: "JSON",                    data: block,                    success: function(data) {                        if (!data.error) {                            $('#chat-block').closeModal;

$.showCustomModal('Success!', block.user + ' has been blocked for ' + block.expiry, {                               id: 'chat-block-okay',                                width: 400,                                buttons: [{                                    message: 'Okay',                                    defaultButton: true,                                    handler: function {                                        $('#chat-block-okay').closeModal;                                    }                                }]                            });

mainRoom.kick({                               name: block.user                            }); } else { $('#chat-block').closeModal;

$.showCustomModal('Failure!', data.error.code, {                               id: 'chat-block-fail',                                width: 200,                                buttons: [{                                    message: 'Okay',                                    defaultButton: true,                                    handler: function {                                        $('#chat-block-fail').closeModal;                                    }                                }]                            }); }                   },                    error: function { $('#chat-block').closeModal;

$.showCustomModal('Failure!', 'An unknown error has occured!', {                           id: 'chat-block-fail',                            width: 200,                            buttons: [{                                message: 'Okay',                                defaultButton: true,                                handler: function {                                    $('#chat-block-fail').closeModal;                                }                            }]                        }); }               });            }        }, {            message: 'Cancel',            handler: function {                $('#chat-block').closeModal;            }        }]    }); });