MediaWiki:Gadget-LazyAdminTools.js

From Zeah RSPS - Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// LazyAdminTools: easy delete, rollback, and block buttons for admins
// @author Jr Mime
// @author Iiii_I_I_I

(function ($, mw) {
    if (mw.config.get("wgCanonicalSpecialPageName") != "Contributions") return;

    var username = mw.config.get("wgPageName").split("/")[1],
        lazyAdminTools = {};

    // Fieldset maker
    lazyAdminTools.init = function () {
        var blockSection = new OO.ui.ActionFieldLayout(
            new OO.ui.ComboBoxInputWidget({
                options: [
                    { data: '1 day' },
                    { data: '3 days' },
                    { data: '1 week' },
                    { data: '2 weeks' },
                    { data: '1 month' },
                    { data: '3 months' },
                    { data: 'infinite' }
                ],
                value: '3 days',
                placeholder: 'Block duration'
            }),
            new OO.ui.ButtonWidget({
                classes: ['LAT-button-block'],
                label: 'Block'
            }),
            {
                align: 'top',
                classes: ['LAT-section-block'],
                label: 'Block with reason "Spam/vandalism" for:',
            }
        );
        var deleteSection = new OO.ui.ActionFieldLayout(
            new OO.ui.TextInputWidget({
                value: 'Removing spam/vandalism',
                placeholder: 'Reason for deletion'
            }),
            new OO.ui.ButtonWidget({
                classes: ['LAT-button-delete'],
                label: 'Delete'
            }),
            {
                align: 'top',
                classes: ['LAT-section-delete'],
                label: 'Delete page creations with reason:'
            }
        );
        var rollbackSection = new OO.ui.FieldLayout(
            new OO.ui.ButtonWidget({
                classes: ['LAT-button-rollback'],
                label: 'Rollback'
            }),
            {
                align: 'top',
                classes: ['LAT-section-rollback'],
                label: 'Revert current revisions:'
            }
        );
        var everythingSection = new OO.ui.FieldLayout(
            new OO.ui.ButtonWidget({
                classes: ['LAT-button-everything'],
                label: 'You\'re a big guy',
                title: 'For you',
                flags: [ 'primary', 'destructive' ]
            }),
            {
                align: 'top',
                classes: ['LAT-section-everything'],
                label: 'Perform all three actions:'
            }
        );

        $('<fieldset/>', { class: 'gadget-LAT' }).append(
            $('<legend/>', { text: 'LazyAdminTools' }),
            $('<div/>', { class: 'LAT-container' }).append(
                blockSection.$element,
                deleteSection.$element,
                rollbackSection.$element,
                everythingSection.$element
            )
        ).prependTo('#mw-content-text');

        // On click of any of the buttons, call the functions
        $(".LAT-button-rollback").on("click", function () {
            lazyAdminTools.rollback();
        });

        $(".LAT-button-block").on("click", function () {
            lazyAdminTools.block();
        });

        $(".LAT-button-delete").on("click", function () {
            lazyAdminTools.del();
        });

        $(".LAT-button-everything").on("click", function () {
            // Poor way of doing all
            lazyAdminTools.rollback();
            lazyAdminTools.block();
            lazyAdminTools.del();
        });
    };

    // Rollback function: get all rollback links in browser, then strike the edit
    lazyAdminTools.rollback = function () {
        $(".mw-rollback-link a").each(function (i) {
            var obj = $(this),
                href = obj.attr("href");

            setTimeout(function () {
                $.get(href);
                obj.text("gone!")
                    .css({
                        color: "grey",
                        "text-decoration": "line-through",
                    })
                    .removeAttr("href")
                    .parents()
                    .eq(1)
                    .css({
                        color: "grey",
                        "text-decoration": "line-through",
                    })
                    .children()
                    .removeAttr("href")
                    .css({
                        color: "grey",
                        "text-decoration": "line-through",
                    });
            }, i * 500);
        });
    };

    // Block function, simple API call
    lazyAdminTools.block = function () {
        new mw.Api()
            .postWithEditToken({
                format: "json",
                action: "block",
                user: username,
                expiry: $('.LAT-section-block .oo-ui-inputWidget-input').val(),
                nocreate: 0,
                autoblock: 0,
                reason: "Spam/vandalism",
                bot: 1
            })
            .done(function (d) {
                if (!d.error) {
                    alert("User has been blocked!");
                } else {
                    alert("Failed to block " + username + ": " + d.error.code);
                }
            })
            .fail(function () {
                alert("Failed to block " + username + "!");
            });
    };

    // Delete function, get all titles if the line has a "newpage" attribute, delete
    lazyAdminTools.del = function () {
        function apiDelete(page, reason) {
            new mw.Api()
                .postWithEditToken({
                    format: "json",
                    action: "delete",
                    title: page,
                    reason: reason,
                    bot: 1
                })
                .done(function (d) {
                    if (!d.error) {
                        // console.log('Deletion of ' + page + ' successful!');
                    } else {
                        console.log("Failed to delete " + page + ": " + d.error.code);
                    }
                })
                .fail(function () {
                    console.log("Failed to delete " + page + "!");
                });
        }

        $("li .newpage ~ a").each(function () {
            var title = $(this).attr("title");

            apiDelete(title, $('.LAT-section-delete .oo-ui-inputWidget-input').val());
            $(this)
                .parent()
                .css({
                    color: "grey",
                    "text-decoration": "line-through",
                })
                .children()
                .removeAttr("href")
                .css({
                    color: "grey",
                    "text-decoration": "line-through",
                });
        });
    };

    // Launch the damn thing
    lazyAdminTools.init();
})(jQuery, mediaWiki);