Jump to content

User:Alpha Quadrant/patrollinks.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.
// This is a modified version of [[User:Mr.Z-man/patrollinks.js]]
// This script places the show/hide button in the toolbox instead of the default tabs
var patrollinks = {
  showbydefault:true
};

$(document).ready(function() {
if (mw.config.get('wgPageName') == "Special:NewPages" ) {

patrollinks.patrol = function(rcid, timestamp, title) {
  var params = {'action':'query',
    'list':'recentchanges',
    'rcstart':timestamp,
    'rcend':timestamp,
    'rctype':'new',
    'rcprop':'title|patrolled',
    'rctoken':'patrol',
    'format':'json'
  };
  $.post(mw.config.get('wgServer')+mw.config.get('wgScriptPath')+'/api.php',
    params,
    function(data) {
      for(var i=0; i<data['query']['recentchanges'].length; i++) {
        if (data['query']['recentchanges'][i]['title'] == title) {
          if (data['query']['recentchanges'][i]['patrolled']) {
            patrollinks.fail(rcid, 'Already patrolled', 'mark');
          } else {
            patrollinks.finish(rcid, data['query']['recentchanges'][i]['patroltoken']);
          }
          break;
        }
      }
    },
    'json'
  );
}

patrollinks.finish = function(rcid, token) {
  var params = {'action':'patrol',
    'rcid':rcid,
    'token':token,
    'format':'json'
  };
  $.post(mw.config.get('wgServer')+mw.config.get('wgScriptPath')+'/api.php',
    params,
    function(data) {
      if (data['error']) {
        patrollinks.fail(data['error']['info'], 'nothing')
        $('#plink-'+rcid+' > img').remove();
      } else {
        $('#plink-'+rcid).parent().removeClass('not-patrolled');
        $('#plink-'+rcid).remove();
      }
    },
    'json'
  );
}

patrollinks.fail = function(rcid, reason, action) {
  if ($('#plink-error-'+rcid).length == 0) {
    var err = $('<span />').attr('id', 'plink-error-'+rcid);
    $('#plink-'+rcid).after(err);
  }
  $('#plink-error-'+rcid).attr('style', 'font-weight:bold; color:red;').text(' Error: '+reason);
  switch(action) {
  case 'del':
    $('#plink-'+rcid).parent().removeClass('not-patrolled');
    $('#plink-'+rcid).parent().children('a.mw-newpages-pagename').addClass('new');
    $('#plink-'+rcid).remove();
    break;
  case 'mark':
    $('#plink-'+rcid).parent().removeClass('not-patrolled');
    $('#plink-'+rcid).remove();
    break;
  case 'nothing':
    $('#plink-'+rcid+' > img').remove();
    break;
  }
}

patrollinks.clickhandler = function(elem) {
  var loader = $('<img />').attr('src', '//upload.wikimedia.org/wikipedia/commons/2/27/Throbber_allbackgrounds_eightbar.gif').attr('alt', 'patrolling...').attr('width', '17').attr('height', '17');
  $(elem).append(loader);
  var title = $(elem).attr('rel');
  var rcid = $(elem).attr('id').split('-')[1];
  $.getScript('http://toolserver.org/~alexz/util/rcid.php?rcid='+rcid+'&title='+encodeURIComponent(title));
}

patrollinks.enable = function() {
  if ($('#p-tb')) {
    $('#ca-plinks-show').hide();
    $('#ca-plinks-hide').show();
  }
  if ($('.patrollink').length != 0) {
    $('.patrollink').show()
    return;
  }
  var unpatrolled = $('li.not-patrolled');
  for(var i=0; i<unpatrolled.length; i++) {
    var title = $(unpatrolled[i]).children('a.mw-newpages-pagename').attr('title');
    var rcid = $(unpatrolled[i]).children('a.mw-newpages-pagename').attr('href').replace(/.*rcid=(\d*)/, "$1");
    var link = $("<a />").attr('href', '#').text("patrol");
    var span = $("<span />").append(' [').append(link).append(']').addClass('patrollink').attr('id', 'plink-'+rcid).attr('rel', title);
    $(unpatrolled[i]).append(span);
  }
  $('.patrollink').click(function() {
    patrollinks.clickhandler(this);
    return false;
  });
}

patrollinks.disable = function() {
  if ($('#p-tb')) {
    $('#ca-plinks-hide').hide();
    $('#ca-plinks-show').show();
  }
  $('.patrollink').hide()
}

if ($('#p-tb')) {
  mw.util.addPortletLink('p-tb', '#', "Hide patrol links", "ca-plinks-hide", "Hide patrol links");
  $('#ca-plinks-hide').click(patrollinks.disable);
  $('#ca-plinks-hide').hide();
  mw.util.addPortletLink('p-tb', '#', "Show patrol links", "ca-plinks-show", "Show patrol links");
  $('#ca-plinks-show').click(patrollinks.enable);
}
if (!$('#p-tb') || patrollinks.showbydefault) {
  patrollinks.enable();
}
if (patrollinks.showbydefault) {
  $('#ca-plinks-show').hide();
  $('#ca-plinks-hide').show();
}

}
});