MediaWiki:Gadget-ajaxundo.js

/** * Ajax Undo links * * Adds an Ajax undo link next to the normal undo link on page histories * * @author Grunny * @version 0.1 * */

$(document).ready( function {	if( !$( '.mw-history-undo > a' ).length || wgAction !== 'history' ) {		return;	}	$( '.mw-history-undo > a' ).each( function  { var	undoUrl = $( this ).attr( 'href' ), $ajaxUnduLink = $( '' ).text( 'AJAX Undo' ).attr( 'href', '#' ).click( function {				var	$ajaxUnduLinkob = $( this ),					undoIdRegex = /&undo=([^&]*)/,					undoId = undoIdRegex.exec( undoUrl )[1],					editToken,					etUrl = wgServer + wgScriptPath + '/api.php?action=query&prop=info|revisions&intoken=edit&titles=Main%20Page&format=json';				$ajaxUnduLinkob.html( ' ' );				$.getJSON( etUrl, function (data) { for ( var p in data.query.pages ) { break; }					editToken = data.query.pages[p].edittoken; $.ajax( {						url: wgScriptPath + 'api.php?',						data: 'action=edit&format=json&title=' + encodeURIComponent( wgPageName ) + '&undo=' + encodeURIComponent( undoId ) + '&bot=1&token=' + encodeURIComponent( editToken ),						dataType: 'json',						type: 'POST',						success: function ( data ) {							if ( data.edit && data.edit.result === 'Success' ) {								$ajaxUnduLinkob.text( '(undone)' );							} else if ( data.error && data.error.code === 'undofailure' ) {								$ajaxUnduLinkob.text( '(error)' );								alert( data.error.info );							} else {								$ajaxUnduLinkob.text( '(error)' );								alert( 'Error: Unknown result from API.' );							}						},						error: function {							$ajaxUnduLinkob.text( '(error)' );						}					} ); } );			} );		$( this ).parent.after( ' | ', $ajaxUnduLink ); } ); } );