Jump to content

User:MarkTraceur/autopatrol.js

From Wikipedia, the free encyclopedia
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.
function autopatrolclick() {
  var api = new mw.Api();
  debug = mw.config.get( 'debug' );
  var username = $('#mw-np-username').val();
  var bits = $('link[rel="canonical"]')[0].href.split('&');
  var ns = 0;
  var lim = 50;
  var start = false;
  var hidepat = false;
  var hidered = true;
  for (var i=0; i<bits.length; i++) {
    b = bits[i].split('=');
    if (b.length == 1) {
      continue;
    }
    if (b[0] == 'limit') {
      lim = parseInt(b[1]);
    }
    if (b[0] == 'namespace') {
      ns = b[1];
    }
    if (b[0] == 'hidepatrolled' && b[1] == '1') {
      hidepat = true;
    }
    if (b[0] == 'hideredirs' && b[1] != '1') {
      hidered = false;
    }
    if (b[0] == 'offset') {
      start = b[1].replace(/(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/, "$1-$2-$3T$4:$5:$6Z")
    }
  }
  dir = 'older';
  if (document.URL.indexOf('dir=prev') != -1) {
    dir = 'newer';
  }
  if (lim>500 && $.inArray('sysop', wgUserGroups) == -1) {
    lim = 500;
  }
  var params = {'action':'query',
    'list':'recentchanges',
    'rcuser':username,
    'rctype':'new',
    'rcnamespace':ns,
    'rclimit':lim,
    'rctoken':'patrol',
    'rcdir':dir,
    'rcprop':'ids|patrolled',
    'format':'json'
  }
  if (start) {
    params['rcstart'] = start;
  }
  if (hidepat) {
    params['rcshow'] = '!patrolled';
    if (hidered) {
      params['rcshow'] += '|!redirect';
    }
  } else if(hidered) {
    params['rcshow'] = '!redirect';
  }
  $('#autopatrolbutton').after('<b id="autopatrol-done"></b>');
  $('#autopatrol-done').ajaxStop(function() {
    $(this).html('&nbsp;&nbsp;Finished!');
  });
	  if (debug) {
  	for (var p in params) {
  		console.log(p);
  		console.log(params[p]);
  	}
  }
  api.post( params ).then( function ( data ) {
    rclen = data['query']['recentchanges'].length
    for (var i=0; i<rclen; i++) {
      if (typeof(data['query']['recentchanges'][i].patrolled) != "undefined" ) {
        continue;
      }
      if (!debug) {
    	 autopatrol(data['query']['recentchanges'][i]['rcid'], data['query']['recentchanges'][i]['patroltoken']);
      }
    }
  } );
}

function autopatrol(rcid, token, last) {
  var api = new mw.Api();
  
  api.post( {
  	'action':'patrol',
    'token':token,
    'rcid':rcid
  } );
}
 
function autopatrollink() {
  if (document.getElementById('mw-np-username').value) {
    button = document.createElement('input');
    button.type = 'button';
    button.id = 'autopatrolbutton';
    button.value = 'Autopatrol';
    button.onclick = autopatrolclick;
    button.title = 'Autopatrol all edits by this user';
    document.getElementById('mw-np-username').parentNode.appendChild(button);
  }
}

if (mw.config.get('wgPageName') == 'Special:NewPages') {
  $(document).ready(autopatrollink);
}