User:FR30799386/quick-undo.js

function loadQuickUndo(globalmessages) { var api = new mw.Api({       ajax: {            headers: {                'Api-User-Agent': 'quickUndo/0.0.1 ( https://en.wikipedia.org/wiki/User:FR30799386/quick-undo.js ) '            }        }    });

function setStatus(message) { $('.quickUndoStatus').text(message); }

function doQuickUndo(e, $pointer) { e.preventDefault; var revId = ($pointer.prop('href')).split('&undo=')[1]; api.get({           "action": "query",            "format": "json",            "prop": "revisions",            "revids": revId,            "rvprop": "user"        }).then(function(response) {        var user = response.query.pages[mw.config.get('wgRelevantArticleId')].revisions[0].user;        $pointer.hide.after(' ');        setStatus('Loading...', $pointer);        var editSummary = 'Undid revision ' +            revId +            ' by ' + user + ' (talk) ';        console.log(editSummary);        var customEditSummary = prompt('Add a reason to your edit summary !');        if (customEditSummary !== null) {            editSummary = editSummary + customEditSummary + ' (quickUndo)';        } else {            $pointer.show;            setStatus('', $pointer);            return; }       //call the API api.postWithToken('csrf', {           action: "edit",            title: mw.config.get('wgRelevantPageName'),            summary: editSummary,            undo: revId,        }).done(function(result) {            if (typeof result.edit.nochange !== 'undefined') {                setStatus('Failed', $pointer);                mw.notify(globalmessages['Undo-nochange']);                return;            }            setStatus('Sucessful', $pointer);            mw.notify('Revert sucessful....Reloading in a sec');            setTimeout(function { location.href = mw.util.getUrl('Special:Diff/' +result.edit.newrevid); //Fill this up           }, 1500);            console.log(result);        }).fail(function(code, jqxhr) {            setStatus('Failed', $pointer);            var details;            if (code === 'http' && jqxhr.textStatus === 'error') {                details = 'HTTP error: ' + jqxhr.xhr.status;            } else if (code === 'http') {                details = 'HTTP error: ' + jqxhr.textStatus;            } else if (code === 'ok-but-empty') {                details = 'An empty response was given the server';            } else {                details = jqxhr.error.info;            }            mw.notify(details);        }); });   }    $('.mw-history-undo a').click(function(e) { var $pointer = $(this); doQuickUndo(e, $pointer); });   $('.mw-diff-undo a').click(function(e) { var $pointer = $(this); doQuickUndo(e, $pointer); });

}

$.when(mw.loader.using(['mediawiki.util', 'mediawiki.api']), $.ready).then(function {   return new mw.Api.getMessages(['Undo-nochange'], { amlang: mw.user.options.get('languages') }); }).then(function(globalmessages) {   loadQuickUndo(globalmessages); }); //End of code //