User:Recludam/monobook.js

mw.loader.using(['mediawiki.api','mediawiki.util'], function {   var ug = mw.config.get("wgUserGroups").join(' ');    if ( $('#t-bd').length || ug.indexOf('sysop') + ug.indexOf('vstf') + ug.indexOf('staff') + ug.indexOf('helper') + ug.indexOf('content-moderator') === -5 ) {       return;    }    var token = mw.user.tokens.get('editToken'),        delay = window.batchDeleteDelay || 1000,        api = new mw.Api, i18ndata = {            en: {                toolsTitle: 'Batch Delete',                modalTitle: 'Ajax Batch Delete',                close: 'Close',                inputReason: 'Reason for deleting',                inputProtect: 'Protect for admin only?',                inputPages: 'Put the name of each page you want to delete on a separate line',                errorsForm: 'Any errors encountered will appear below',                addCategoryContents: 'Add category contents',                initiate: 'Initiate',                stateReason: 'Please state a reason!',                endTitle: 'Finished!',                endMsg: 'Nothing left to do, or next line is blank.',                enterCategory: 'Please enter the category name (no category prefix)', ajaxError: 'AJAX error', errorGetContents: 'Failed to get contents of $1: $2', errorDelete: 'Failed to delete $1: $2', errorProtect: 'Failed to protect $1: $2', deleteSuccess: 'Deletion of $1 successful!', protectSuccess: 'Protection of $1 successful!' }       },        lang = mw.config.get('wgUserLanguage'), i18n = $.extend(i18ndata.en, i18ndata[lang.split('-')[0]], i18ndata[lang]), formHtml = ('' +               ' ' +                    ' :' +                        '' +                        ' ' +                        '  ' +                    ' ' +                    ' . ' +                    '' +                    ' : ' +                    ' ' +                ' ' +            ' ').replace(//g, function(_, a) { return i18n[a]; }); // Support for Monobook mw.util.addPortletLink('p-tb', '#', 'Batch Delete TESTY TEST', 't-bd'); $('#t-bd').click(function {       $.showCustomModal(i18n.modalTitle, formHtml, { id: 'form-mass-delete', width: 500, buttons: [{ message: i18n.close, handler: function { $('#form-mass-delete').closeModal; }           }, {                message: i18n.addCategoryContents, defaultButton: true, handler: addCategoryContents }, {               id: 'startButton', message: i18n.initiate, defaultButton: true, handler: init }]       });    });    function init { var txt = document.getElementById('text-mass-delete'), deleteReason = document.getElementById('delete-reason').value, pages = txt.value.split('\n'), currentPage = pages[0]; if (!deleteReason) { alert(i18n.stateReason); return; }       document.getElementById('startButton').setAttribute('disabled', 'disabled'); if (!currentPage) { document.getElementById('startButton').removeAttribute('disabled'); $.showCustomModal(i18n.endTitle, i18n.endMsg, {               id: 'mass-delete-complete',                width: 200,                buttons: [{                    message: i18n.close,                    defaultButton: true,                    handler: function {                        $('#mass-delete-complete').closeModal;                    }                }]            }); } else { process(currentPage, deleteReason); }       pages = pages.slice(1,pages.length); txt.value = pages.join('\n'); }   function addCategoryContents { var category = prompt(i18n.enterCategory + ':'); api.get({           action: 'query',            list: 'categorymembers',            cmtitle: 'Category:' + category,            cmlimit: 5000        }).done(function(d) {            if (!d.error) {                var data = d.query;	            for (var i in data.categorymembers) {                    $('#text-mass-delete').append(data.categorymembers[i].title + '\n');                }            } else {                outputError('GetContents', category, d.error.code);            }        }).fail(function {            outputError('GetContents', category, i18n.ajaxError);        }); }   function outputError(error, param1, param2) { $('#text-error-output').append(formatI18n('error' + error, param1, param2) + ' '); }   function formatI18n(message, param1, param2) { return i18n[message].replace('$1', param1).replace('$2', param2); }   function process(page,reason) { api.post({           action: 'delete',            watchlist: 'nochange',            title: page,            reason: reason,            token: token,            bot: true        }).done(function(d) {            if (!d.error) {                console.log(formatI18n('deleteSuccess', page));                if (document.getElementById('protect-check').checked) {                    api.post({ action: 'protect', expiry: 'infinite', protections: 'create=sysop', watchlist: 'nochange', title: page, reason: reason, token: token }).done(function(d) { if (!d.error) { console.log(formatI18n('protectSuccess', page)); } else { console.log(formatI18n('errorProtect', page, d.error.code)); outputError('Protect', page, d.error.code); }                   }).fail(function { console.log(formatI18n('errorProtect', page, i18n.ajaxError)); outputError('Protect', page, i18n.ajaxError); });               }            } else {                console.log(formatI18n('errorDelete', page, d.error.code));                outputError('Delete', page, d.error.code);            }        }).fail(function {            console.log(formatI18n('errorDelete', page, i18n.ajaxError));            outputError('Delete', page, i18n.ajaxError);        }); setTimeout(init, delay); } })