User:Bizzeebeever/scripts/patrol.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes.
- Internet Explorer: hold down the Ctrl key and click the Refresh or Reload button, or press Ctrl+F5.
- Firefox: hold down the Shift key while clicking Reload; alternatively press Ctrl+F5 or Ctrl-Shift-R.
- Opera, Konqueror and Safari users can just click the Reload button.
- Chrome: press Ctrl+F5 or Shift+F5
window.bb = window.bb || {};
bb.patrol = bb.patrol || {};
bb.util = bb.util || {};
$.extend( bb.util, {
GUIDf: '0123456789ABCDEF'.split( '' ),
GUID: function( limit ) {
var h = '';
for ( var i = 0; i < ( limit || 32 ); i ++ )
h += this.GUIDf[ Math.floor( Math.random() * this.GUIDf.length ) ];
return h;
},
getRcid: function( str ) {
return ( str.match( /rcid=(\d+)/ ) || [ '', '' ] )[ 1 ];
},
getCurid: function( str ) {
return ( str.match( /curid=(\d+)/ ) || [ '', '' ] )[ 1 ];
},
getPageName: function( str ) {
return ( str.match( /wiki\/([^\?]+)/ ) || [ '', '' ] )[ 1 ]
}
});
$.extend( bb.patrol, {
articles: {},
_done: '<img src="//images.uncyclomedia.co/uncyclopedia/en/thumb/0/02/Symbol_for_vote.svg/15px-Symbol_for_vote.svg.png"/><span style="color:green;"> patrolled</span>',
script: mw.config.get( 'wgScriptPath' ) + '/api.php',
init: function() {
if ( ! wgPageName.match( /recentchanges/i ) ) return;
$( 'h4' ).next().each( function() {
var dateGUID = bb.util.GUID( 10 );
$( this )
.find( 'a[href*=rcid], a:contains(" changes")' )
.each( bb.patrol.parseLinks, [ dateGUID ] );
});
//find each H4 heading's next-sibling, which is a <div> containing links
//The GUID part is necessary to differentiate changes for one page done on different dates
},
parseLinks: function( guid ) {
var $t = $( this ),
href = $t.attr( 'href' ),
rcid = bb.util.getRcid( href ),
id = bb.util.getCurid( href ) + guid,
text = $t.text(),
article = bb.patrol.articles[ id ] || ( bb.patrol.articles[ id ] = { 'group': [] } );
//note the assignment taking place after the logical OR
if ( text.match( /\d+ changes$/i ) ) bb.patrol.addGroupedPatrolLink( $t, article )
else bb.patrol.addPatrolLink( $t, article, href, rcid, text );
},
addPatrolLink: function( obj, article, href, rcid, text ) {
if ( !rcid ) return;
article.total = text.match( /\d+:\d+/ ) ?
article.group.push( this.createNewPageLink( obj, article, rcid ) ) : //'this' is a new page diff link
article.group.push( this.createDiffLink( obj, article, rcid ) ); //'this' is simply a diff link
if ( article.changeLink && !article.parent )
article.parent = this.createGroupLink( article.changeLink, article );
},
addGroupedPatrolLink: function( obj, article ) {
if ( article.total )
article.parent = this.createGroupLink( obj, article )
else
article.changeLink = obj;
},
createDiffLink: function( obj, article, rcid ){
return $( '<a href="#" title="patrol this change only">patrol</a>' )
.data( 'article', article )
.data( 'rcid', rcid )
.click( bb.patrol.doPatrol )
.insertAfter( $( '<span> | </span>' ).insertAfter( obj ) );
},
createNewPageLink: function( obj, article, rcid ) {
return $( '<a href="#" title="patrol this new page and all changes grouped here">(patrol this page)</a>' )
.data( 'article', article )
.data( 'rcid', rcid )
.click( bb.patrol.doGroupPatrol )
.insertAfter( $( '<span> </span>' ).insertAfter( obj ) );
},
createGroupLink: function( obj, article, rcid ) {
return $( '<a href="#" title="patrol all grouped changes shown below">patrol these changes</a>' )
.data( 'article', article )
.data( 'rcid', rcid )
.click( bb.patrol.doGroupPatrol )
.insertAfter( $( '<span> | </span>' ).insertAfter( obj ) );
},
doGroupPatrol: function( e ) {
var i, article = $( this ).data( 'article' );
if ( e ) e.preventDefault();
for ( i in article.group )
bb.patrol.doPatrol.call( article.group[ i ] );
//cannot use article.group[ i ].click() because it will create an infinite loop when patrolling NewPages
},
doPatrol: function( e ) {
$t = $( this );
if ( e ) e.preventDefault();
return $.post( bb.patrol.script, {
action: 'patrol',
token: mw.user.tokens.get( 'patrolToken' ),
rcid: $t.data( 'rcid' ),
format: 'xml'
})
.done( $.proxy( bb.patrol.onPatrolled, $t ) )
},
onPatrolled: function( xml ) {
if ( !$( xml ).find( 'error' ).length ) {
var article = this.data( 'article' );
if ( article && article.parent && -- article.total == 0 ) article.parent.replaceWith( bb.patrol._done );
this.replaceWith( bb.patrol._done );
}
}
});
//$( document ).ready();
$( bb.patrol.init );