User:Abelmoschus Esculentus/MCMH.js

//Written by Abelmoschus Esculentus //Date: 15 January 2019 // 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($(' ').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('editToken'); if (radio === 'insert') { var wikitext = ""; for (i = 0; i < cats.length; i++) { wikitext += "\n';   }    var sum = '+';    if (cats.length > 5) {      sum += 'multiple categories';    }    else {      for (i = 0; i < cats.length - 1; i++) {        sum += ", +'; }     sum += "';    }    for (i = 0; i < pages.length; i++) {      $("#MCMH-interface-content").append(' Editing ' + pages[i] + '...');      editpagei(pages[i],wikitext,sum + ex + ' (MCMH)',edittoken);      $("#MCMH-interface-content").append('Done!');    }    $("#MCMH-interface-content").append(' Finished!');  }  else {    alert('Remove');   var pagetext;    for (i = 0; i < pages.length; i++) {       $("#MCMH-interface-content").append(' Editing ' + pages[i] + '...');      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 += ", -';        }        summ += "';      }      editpager(pages[i], pagetext, summ, edittoken);      $("#MCMH-interface-content").append('Done!');    }    $("#MCMH-interface-content").append(' Finished!');  } } 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(' '+	    ' '+	      ' '+	      ' '+	      ' '+	      ' '+	      ' '+	    ' '+	  ' ');	  $("#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(	  $(' ').css('margin-bottom','0.5em').append(	    $(' ').attr({'for':'MCMH-userinput-label', 'id':'MCMH-userinput-label'}).text('Action: '),	    $(' ').attr({'type':'radio', 'name':'MCMH-radio','id':'MCMH-radio1','value':'insert','checked':'true'}),	    $(' ').text('Insert '),	    $(' ').attr({'type':'radio', 'name':'MCMH-radio','id':'MCMH-radio2','value':'remove', 'disabled':'disabled'}),	    $(' ').text('Remove '),	    $(' ').text('(under development)')	  ),	  $(' ').css('margin-bottom','0.5em').append(	    $(' ').text('Cateogories to be inserted/removed (one per line, no need to add "Category:"): '),	    $(' ').css({'resize':'none'}).attr({'name':'MCMH-cat','id':'MCMH-cat','rows':'6','cols':'35'})	  ),	  $(' ').css('margin-bottom','0.5em').append(	    $(' ').text('Pages to be edited (one per line, maximum 100): '),	    $(' ').css({'resize':'none'}).attr({'name':'MCMH-pages','id':'MCMH-pages','rows':'10','cols':'35'})	  ),	  $(' ').css('margin-bottom','0.5em').append(	    $(' ').text('Explanation (optional): '),	    $(' ').attr({'type':'text','name':'MCMH-e','id':'MCMH-e'})	  ),	  $(' ').css('margin-bottom','0.5em').append(	    $(' ').attr({'src':'https://upload.wikimedia.org/wikipedia/commons/e/e5/OOjs_UI_icon_alert_destructive_black-darkred.svg','height':'23','width':'23'}),	    $(' ').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(	  $(' ').attr({'name':'MCMH-done', 'id':'MCMH-done'}).text('Edit'),	  $(' ').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; }); //