User:Ugochimobi/undo.js

// mobileUndo 2.0.0, a script allowing reversion of edits on mobile. // All code is released under the default FAMEPedia content license. // Installation instructions at User:Ugochimobi/undo. //  This script basically connects an API endpoint to //         a mw-ui-destructive button which is added to the //        footer by using jQuery. Additional functionality //        includes changing the position of the thanks button //        and the addition of a confirm dialog to the thank button, //        to prevent mis-clicks due to both buttons being close //        to each other. // // $(document).ready(function{ console.time('mobileUndo'); if (mw.config.get('wgTitle').split('f/')[0] === 'MobileDif') {    //Page is a diff page    //Interface construction    mw.util.addCSS('#mw-mf-userinfo .mobileUndo-ui .mw-ui-icon.mw-ui-icon-before:before{display:none;}');    //Thank module starts here    $('#mw-mf-userinfo .post-content').append(' ');    $('.mobileUndo-ui').append($('.mw-mf-action-button').remove.css({        'clear': 'none',        'margin-top': '0.25em'    }));    if (!$('.warningbox').length && mw.config.get('wgRelevantPageIsProbablyEditable')) {        //The real music, the undo button is created        $('.mobileUndo-ui').append('' + '' + ' Undo ');       console.timeEnd('mobileUndo');    } } }); $.when(mw.loader.using(['mediawiki.util', 'mediawiki.api', 'oojs-ui-windows'])).then(function {   return new mw.Api.getMessages(['editundo', 'Undo-nochange'], { amlang: mw.user.options.get('language') }); }).then(function(globalmessages) {	window.wgRelevantUser = function {   var user;    	if ($('.mw-mf-user-link').text !== '') {        	user = $('.mw-mf-user-link').text;    	} else {        	user = $('#mw-mf-userinfo .post-content div:nth-child(2)').text;    	}    return user;    };    window.wgRevId = mw.config.get('wgTitle').split('f/')[1];    var revId = wgRevId;    var username = wgRelevantUser;    $('#mobileUndo-text').text(globalmessages.editUndo);    $('#mw-mf-userinfo .post-content .mobileUndo-ui .mw-mf-action-button').click(function(e) { e.preventDefault; OO.ui.confirm('Send public thanks ?').done(function(confirmed) {           if (confirmed) {                $('.mw-mf-action-button').prop('disabled', true).text('Thanking...');                var api = new mw.Api;                api.postWithToken('csrf', { action: "thank", rev: revId, }).done(function(result) { mw.notify('You thanked ' + username + '.'); $('.mw-mf-action-button').text('Thanked'); }).fail(function { mw.notify('Failed to thank ' + username + '.'); $('.mw-mf-action-button').text('Failed'); });           }        });    });

$('#mobileUndo').click(function(e) {       e.preventDefault;        OO.ui.prompt('Add a reason to your edit summary !', { textInput: { placeholder: 'Reason...' }       }).done(function(customEditSummary) { var editSummary = 'Undid revision ' + revId + ' by ' + username + ' (talk) '; if (customEditSummary !== null) { document.getElementById("mobileUndo").disabled = true; editSummary = editSummary + customEditSummary + ' (mobileUndo)'; //call the API var api = new mw.Api({                   ajax: {                        headers: {                            'Api-User-Agent': 'mobileUndo/2.0.0(https://en.famepedia.org/wiki/User:Ugochimobi/undo.js)'                        }                    }                }); //Feedback UI               $('#mobileUndo').text('Loading...'); api.postWithToken('csrf', {                   action: "edit",                    title: mw.config.get('wgRelevantPageName'),                    summary: editSummary,                    undo: revId,                }).done(function(result) {                    if (typeof result.edit.nochange !== 'undefined') {                        $('#mobileUndo').text('Failed');                        $('#mw-mf-diff-info').append(' ' + globalmessages['Undo-nochange'] + ' If this error message sounds to tech oriented,' + ' please drop a note at Ugochimobi\'s talk page. '                       );                        location.href = location.href + '#mw-mf-diff-info';                        return;                    }                    $('#mobileUndo').text('Sucessful');                    mw.notify('Revert sucessful....Reloading in a sec');                    setTimeout(function { location.href = mw.util.getUrl('Special:MobileDiff/' + result.edit.newrevid); }, 1500);                   console.log(result);                }).fail(function(code, jqxhr) {                    $('#mobileUndo').text('Failed');                    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-mf-diff-info').append(' ' + details.charAt(0).toLowerCase + details.slice(1) + ' If this error message sounds too tech oriented,' + ' please drop a note at Ugochimobi\'s talk page. ');                   location.href = location.href + '#mw-mf-diff-info';                }); }       });

}); }); //End of code //