Jump to content

User:BenjaminWillJS/AjaxUndo.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * 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, ')' );
	}
} );