User:CBM/patrollinks.js

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 = $(' ').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 = $(' ').attr('src', 'http://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]; alert('http://toolserver.org/~alexz/util/rcid.php?rcid='+rcid+'&title='+encodeURIComponent(title)); $.getScript('http://toolserver.org/~alexz/util/rcid.php?rcid='+rcid+'&title='+encodeURIComponent(title)); }

patrollinks.enable = function { if ($('#p-cactions')) { $('#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").attr('href', '#').text("Patrol"); var 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-cactions')) { $('#ca-plinks-hide').hide; $('#ca-plinks-show').show; } $('.patrollink').hide }

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

} });