User:BenjaminWillJS/AjaxUndo.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:BenjaminWillJS/AjaxUndo. |
/**
* Ajax Undo links
*
* Adds an Ajax undo link next to the normal undo link on page histories
* and on diff pages
*/
jQuery(document).ready( function ( $ ) {
"use strict";
function createUndoLink( diffUndoUrl ) {
var $ajaxUndoLink = $( '<a />' ).text( 'AJAX Undo' ).attr( 'href', '#' ).click( function () {
var $ajaxUndoLinkob = $( this ),
undoIdRegex = /&undo=([^&]*)/,
undoId = undoIdRegex.exec( diffUndoUrl )[1],
editToken,
etUrl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?action=query&prop=info|revisions&intoken=edit&titles=' + encodeURIComponent( mw.config.get('wgPageName') ) + '&format=json';
$ajaxUndoLinkob.html( ' <img src="http://images2.wikia.nocookie.net/dev/images/8/82/Facebook_throbber.gif" style="vertical-align: baseline;" border="0" alt="Undoing..." />' );
$.getJSON( etUrl, function ( data ) {
editToken = data.query.pages[wgArticleId].edittoken;
$.ajax( {
url: mw.config.get('wgScriptPath') + '/api.php?',
data: 'action=edit&format=json&title=' + encodeURIComponent( mw.config.get('wgPageName') ) + '&undo=' + encodeURIComponent( undoId ) + '&bot=1&token=' + encodeURIComponent( editToken ),
dataType: 'json',
type: 'POST',
success: function ( data ) {
if ( data.edit && data.edit.result === 'Success' ) {
$ajaxUndoLinkob.text( '(undone)' );
} else if ( data.error && data.error.code === 'undofailure' ) {
$ajaxUndoLinkob.text( '(error)' );
alert( data.error.info );
} else {
$ajaxUndoLinkob.text( '(error)' );
alert( 'Error: Unknown result from API.' );
}
},
error: function () {
$ajaxUndoLinkob.text( '(error)' );
}
} );
} );
} );
return $ajaxUndoLink;
}
if ( $( '.mw-history-undo > a' ).length && mw.config.get('wgAction') === 'history' ) {
$( '.mw-history-undo > a' ).each( function () {
var diffUndoUrl = $( this ).attr( 'href' ),
$ajaxUndoLink = createUndoLink( diffUndoUrl );
$( this ).parent().after( ' | ', $ajaxUndoLink );
} );
} else if ( $( 'table.diff' ).length && typeof $.getUrlVar( 'diff' ) !== 'undefined' ) {
var $diffUndoLink = $( 'table.diff' ).find( '.diff-ntitle > #mw-diff-ntitle1 a:last' ),
diffUndoUrl = $diffUndoLink.attr( 'href' ),
$ajaxDiffUndoLink = createUndoLink( diffUndoUrl );
$diffUndoLink.parent().append( ' (', $ajaxDiffUndoLink, ')' );
}
} );