MediaWiki:Gadget-LazyQuickTools.js

From [N8]
Revision as of 14:25, 14 August 2020 by Banri (talk | contribs) (Created page with "// LazyQuickTools: easy buttons for admins (function ($, mw) { if (mw.config.get("wgCanonicalSpecialPageName") != "Contributions") return; var username = mw.config.get("w...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
// LazyQuickTools: easy buttons for admins
(function ($, mw) {
  if (mw.config.get("wgCanonicalSpecialPageName") != "Contributions") return;

  var username = mw.config.get("wgPageName").split("/")[1],
    token = mw.user.tokens.get("editToken"),

    // Block reason
    blockReason = "vandalism",

    // Page deletion reason
    deleteReason = "deleting spam/vandalism",
    lazyQuickTools = {};

  // Link maker
  lazyQuickTools.init = function () {
    $('.mw-contributions-form').before('<a id="LQT-Delete" style="cursor:pointer">Delete</a> | ' +
      '<a id="LQT-Rollback" style="cursor:pointer">Rollback</a> | ' +
      '<input type="text" id="LQT-BlockTime" value="2 weeks"> <span id="LQT-Block" class="button">Block</span> | ' +
      'for: <input type="text" id="LQT-DeleteReason" value="' + deleteReason + '"> | ' +
      '<a id="LQT-All" style="cursor:pointer">All</a>');

    // On click of any of the buttons, call the functions
    $("#LQT-Rollback").on("click", function () {
      lazyQuickTools.rollback();
    });

    $("#LQT-Block").on("click", function () {
      lazyQuickTools.block();
    });

    $("#LQT-Delete").on("click", function () {
      lazyQuickTools.dlt();
    });

    $("#LQT-All").on("click", function () {
      // Poor way of doing all
      lazyQuickTools.rollback();
      lazyQuickTools.block();
      lazyQuickTools.dlt();
    });
  }

  // Rollback function: get all rollback links in browser, then strike the edit
  lazyQuickTools.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
  lazyQuickTools.block = function () {
    new mw.Api().post({
      format: 'json',
      action: 'block',
      user: username,
      expiry: $("#LQT-BlockTime").val(),
      nocreate: 0,
      autoblock: 0,
      reason: blockReason,
      bot: 1,
      token: token
    }).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
  lazyQuickTools.dlt = function () {
    function apiDelete(page, reason) {
      new mw.Api().post({
        format: 'json',
        action: 'delete',
        title: page,
        reason: reason,
        bot: 1,
        token: token
      }).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, $("#LQT-DeleteReason").val());
      $(this).parent().css({
        'color': 'grey',
        'text-decoration': 'line-through'
      }).children().removeAttr('href').css({
        'color': 'grey',
        'text-decoration': 'line-through'
      });
    });
  }

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