User:Alpha Quadrant/patrollinks.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Alpha Quadrant/patrollinks. |
// 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();
}
}
});