Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//Written by Abelmoschus Esculentus
//Date: 15 January 2019
//Copied from [[User:Abelmoschus Esculentus/MCMH.js]
//<nowiki>
function editpagei(page, wikitext, summary, edittoken) {
  var API = new mw.Api();
  API.post({
    'action':'edit',
    'title': page,
    'appendtext': wikitext,
    'summary': summary,
    'token': edittoken
  })
  .done()
  .fail( function(c,r) {
    alert('An error has occured.');
    screen000();
  });
}
function editpager(page, wikitext, summary, edittoken) {
  var API = new mw.Api();
  API.post({
    'action':'edit',
    'title': page,
    'text': wikitext,
    'summary': summary,
    'token': edittoken
  })
  .done()
  .fail( function(c,r) {
    alert('An error has occured.');
    screen000();
  });
}
function screen100(cats, pages, radio, ex) {
  $('#MCMH-done').remove();
  $('#MCMH-interface-footer').prepend($('<button>').attr({'name':'MCMH-back', 'id':'MCMH-back'}).text('Back'));
  $('#MCMH-interface-content').empty();
  $("#MCMH-interface-content").text('Retrieving information...Done!');
  $('#MCMH-back').click(function() {
    screen000();
  });
  var i;
  var edittoken = mw.user.tokens.get('csrfToken');
  if (radio === 'insert') {
    var wikitext = "";
    for (i = 0; i < cats.length; i++) {
      wikitext += "\n[[Category:" + cats[i] + ']]';
    }
    var sum = '+';
    if (cats.length > 5) {
      sum += 'multiple categories';
    }
    else {
      for (i = 0; i < cats.length - 1; i++) {
        sum += "[[Category:" + cats[i] + ']], +';
      }
      sum += "[[Category:" + cats[cats.length-1] + ']]';
    }
    for (i = 0; i < pages.length; i++) {
      $("#MCMH-interface-content").append('<br>Editing <a class="plainlinks" href="https://en.wikipedia.org/wiki/' + pages[i] + '">' + pages[i] + '</a>...');
      editpagei(pages[i],wikitext,sum + ex + ' ([[User:DannyS712/MCMH|MCMH]])',edittoken);
      $("#MCMH-interface-content").append('<b>Done!</b>');
    }
    $("#MCMH-interface-content").append('<br><b>Finished!</b>');
  }
  else {
    alert('Remove');
   var pagetext;
    for (i = 0; i < pages.length; i++) { 
      $("#MCMH-interface-content").append('<br>Editing <a class="plainlinks" href="https://en.wikipedia.org/wiki/' + pages[i] + '">' + pages[i] + '</a>...');
      alert(pages[i]);
      API.get( {
				'action': 'query',
				'titles': pages[i],
				'prop': 'revisions',
				'rvprop': 'content'
      })
      .done(function(result) {
        pagetext = result.query.pages[pages[i]].revisions[0]['*'];
      })
      .fail();
      alert(pagetext);
      var temp1;
      for (var j = 0; j < cats.length; j++) {
        if (pagetext.includes('[[Category:'+cats[i])) {
          temp1 = pagetext.indexOf('[[Category:'+cats[i]);
          for (var k = temp1; k < pagetext.length; k++) {
            if (pagetext.substring(k, 2) === ']]') {
              pagetext.replace(pagetext.substring(temp1, k + 1 - temp1), '');
              break;
            }
          }
        }
      }
      var summ = '-';
      if (cats.length > 5) {
        summ += 'multiple categories';
      }
      else {
        for (i = 0; i < cats.length - 1; i++) {
          summ += "[[Category:" + cats[i] + ']], -';
        }
        summ += "[[Category:" + cats[cats.length-1] + ']]';
      }
      editpager(pages[i], pagetext, summ, edittoken);
      $("#MCMH-interface-content").append('<b>Done!</b>');
    }
    $("#MCMH-interface-content").append('<br><b>Finished!</b>');
  }
}
function mcmh_init() {
	mw.util.addPortletLink('p-tb', 'javascript:void(0)', 'Mass Cat Mod Helper', 'aca-mcmh', null, null);
	$('#aca-mcmh').on('click', function() {
	  $('body').prepend('<div id="MCMH-modal">'+
	    '<div id="MCMH-interface">'+
	      '<h4 id="MCMH-interface-header"></h4>'+
	      '<hr>'+
	      '<div id="MCMH-interface-content"></div>'+
	      '<hr>'+
	      '<div id="MCMH-interface-footer"></div>'+
	    '</div>'+
	  '</div>');
	  $("#MCMH-modal").css({
	    "position": "fixed",
	    "z-index": "1",
	    "left": "0",
	    "top": "0",
	    "width": "100%",
	    "height": "100%",
	    "overflow": "hidden",
	    "background-color": "rgba(0,0,0,0.4)"
	  });
	  $("#MCMH-interface").css({
	    "background-color": "#fffcd3",
	    "margin": "15% auto",
	    "padding": "2px 20px",
	    "border": "1px solid #888",
	    "width": "80%",
	    "max-width": "60em",
	    "font-size": "90%"
	  });
	  $("#MCMH-interface-content").css({
	    "min-height": "7em",
	    "width" : "875px",
	    "height" : "400px",
	    "overflow-y": "scroll"
	  });
	  $("#MCMH-interface-footor").css("min-height", "3em");
	  screen000();
	});
}
var screen000 = function() {
	$("#MCMH-interface-header, #MCMH-interface-content, #MCMH-interface-footer").empty();
	$("#MCMH-interface-header").text("Mass Category Modifying Helper");
	$("#MCMH-interface-content").append(
	  $('<div>').css('margin-bottom','0.5em').append(
	    $('<label>').attr({'for':'MCMH-userinput-label', 'id':'MCMH-userinput-label'}).text('Action: '),
	    $('<input>').attr({'type':'radio', 'name':'MCMH-radio','id':'MCMH-radio1','value':'insert','checked':'true'}),
	    $('<label>').text('Insert '),
	    $('<input>').attr({'type':'radio', 'name':'MCMH-radio','id':'MCMH-radio2','value':'remove', 'disabled':'disabled'}),
	    $('<label>').text('Remove '),
	    $('<small>').text('(under development)')
	  ),
	  $('<div>').css('margin-bottom','0.5em').append(
	    $('<label>').text('Cateogories to be inserted/removed (one per line, no need to add "Category:"): '),
	    $('<textarea>').css({'resize':'none'}).attr({'name':'MCMH-cat','id':'MCMH-cat','rows':'6','cols':'35'})
	  ),
	  $('<div>').css('margin-bottom','0.5em').append(
	    $('<label>').text('Pages to be edited (one per line, maximum 100): '),
	    $('<textarea>').css({'resize':'none'}).attr({'name':'MCMH-pages','id':'MCMH-pages','rows':'10','cols':'35'})
	  ),
	  $('<div>').css('margin-bottom','0.5em').append(
	    $('<label>').text('Explanation (optional): '),
	    $('<input>').attr({'type':'text','name':'MCMH-e','id':'MCMH-e'})
	  ),
	  $('<div>').css('margin-bottom','0.5em').append(
	    $('<img>').attr({'src':'https://upload.wikimedia.org/wikipedia/commons/e/e5/OOjs_UI_icon_alert_destructive_black-darkred.svg','height':'23','width':'23'}),
	    $('<label>').attr('style','font-size:94%').text('You take full responsibility for any action you perform using this script. Please make sure you entered the data correctly before editing.')
	  )
	);
	$("#MCMH-interface-footer").append(
	  $('<button>').attr({'name':'MCMH-done', 'id':'MCMH-done'}).text('Edit'),
	  $('<button>').attr('id', 'MCMH-cancel').text('Close')
	);
	$('#MCMH-cancel').click(function() {
	  $('#MCMH-modal').remove();
	});
	$('#MCMH-done').click(function() {
    var cats = $('#MCMH-cat').val().split('\n');
    var pages = $('#MCMH-pages').val().split('\n');
    var radio = $("input[name=MCMH-radio]:checked").val();
    var ex = '';
    if ($("#MCMH-e").val().length > 0) {
      ex = ': ' + $("#MCMH-e").val();
    }
    if (pages.length > 100) {
      alert('The maximum no. of pages allowed is 100.');
    }
    else {
      screen100(cats, pages, radio, ex);
    }
	});
};
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.Title', 'mediawiki.RegExp'], function() {
	mcmh_init();
});
//</nowiki>