MediaWiki:AjaxUndo.js

/** * Ajax Undo links * * Adds an Ajax undo link next to the normal undo link on page histories * and on diff pages * * @author Grunny * @author Cqm * * @version 0.5 * * Used files: */

'use strict';
 * (function ($, mw) {

if (window.AjaxUndoLoaded) return;

window.AjaxUndoLoaded = true;

var conf = mw.config.get([       'wgArticlePath',        'wgAction',        'wgVersion'    ]);

const strings = { undoing: 'Undoing...', undone: 'undone', error: 'error', unknownerror: 'Error: Unknown result from API.', buttontext: 'AJAX Undo', undotitle: 'Instantly revert this edit without leaving the page', summaryprompt: 'Enter the undo summary to leave when undoing this edit:' };

var api;

function undoEdit { var $this = $(this), url = $this.data.url, page = $this.data.page, undoId = /&undo=([^&]*)/.exec(url)[1], summaryPromise, defaultSummary = window.AjaxUndoSummary || '';

if (window.AjaxUndoPrompt) { summaryPromise = OO.ui.prompt(strings.summaryprompt, {               textInput: {                    value: defaultSummary                }            }); } else { summaryPromise = $.Deferred; summaryPromise.resolve(defaultSummary); }

summaryPromise.then(function(summary) {           if (summary === null) {                return;            }			// @todo: update this image with something that actually loads.            $this.html( $(' ')                   .attr({                        src: 'https://cdn.wikimg.net/en/hkwiki/images/c/cd/Pulse_loading.webp',                        alt: strings.undoing,                        border: '0'                    }) .css('vertical-align', 'baseline') );           return api.post({ action: 'edit', title: page, undo: undoId, bot: '1', minor: window.AjaxUndoMinor ? undefined : '1', summary: summary === '' ? undefined : summary, token: mw.user.tokens.get('csrfToken') || mw.user.tokens.get('editToken') });       }).then(function (data) {            if (!data) {                return;            }            if (data.edit && data.edit.result === 'Success') {                $this.text( '(' + strings.undone + ')' );            } else {                $this.text('(' + strings.error + ')');

alert(data.error && data.error.code === 'undofailure' ?                   data.error.info :                    strings.unknownerror                ); }       });    }

function createUndoLink(url) { var uri = new mw.Uri(url); return $('', {           href: '#ajaxundo', // For integration            'data-url': url,            'data-page': decodeURIComponent( uri.query && uri.query.title || uri.path.substring(                       conf.wgArticlePath                            .replace('$1', '')                            .length                    ) ),           text: strings.buttontext,            click: undoEdit,            title: strings.undotitle,        }); }

function init { api = new mw.Api; if (conf.wgAction === 'history' && $('.mw-history-undo > a').length) { $('.mw-history-undo > a').each(function {                var $this = $(this),                    url = $(this).attr( 'href' ),                    $link = createUndoLink(url);

$this.parent.after(' | ', $link); });       } else if ($('table.diff').length && mw.util.getParamValue('diff') !== undefined) {            var $undoLink = $('table.diff').find('.diff-ntitle .mw-diff-undo a:first'),                url = $undoLink.attr('href'),                $link = createUndoLink(url);

$undoLink.parent.append('(', $link, ')'); }       mw.hook('quickdiff.ready').add(function {            var $undoLink = $('#quickdiff-modal table.diff').find('.diff-ntitle .mw-diff-undo a:first'),                url = $undoLink.attr('href'),                $link = createUndoLink(url);

$undoLink.parent.append('(', $link, ')'); });   }

mw.loader.using([			'mediawiki.api',			'mediawiki.user',			'mediawiki.util',			'mediawiki.Uri',			'oojs-ui-windows'	]).done(function {		init;	}); }(jQuery, mediaWiki));