User:ShahFahad191/Undo.js

$(document).ready(function{	console.time('mobileUndo');	if ( mw.config.get('wgCanonicalSpecialPageName') === 'MobileDiff') {		mw.util.addCSS('#mw-mf-userinfo .mobileUndo-ui .mw-ui-icon.mw-ui-icon-before:before{display:inline-block;}');		if (mw.config.get('wgVisualEditor').pageLanguageDir === 'ltr') {			$('#mw-mf-userinfo .post-content').append(' ');		}		else {			$('#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')) {		   if (mw.config.get('wgVisualEditor').pageLanguageDir === 'ltr') {				$('.mobileUndo-ui').append('' + '' + ' Undo ');			}			else {				$('.mobileUndo-ui').append('' + '' + ' Undo ');			}			console.timeEnd('mobileUndo');		}	} }); if ( mw.config.get('wgCanonicalSpecialPageName') === 'MobileDiff') { $.when(mw.loader.using(['mediawiki.util', 'mediawiki.api', 'oojs-ui-windows'])).then(function {	   return new mw.Api.getMessages(['editundo', 'Undo-nochange']);	}).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-mf-diffarea > div.mw-mf-diff-info > h3 > a').attr('href')).split('&oldid=')[1];	    var revId = wgRevId;		console.log(revId);	    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', {	           textInput: {	                placeholder: 'Reason?'	            }	        }).done(function(customEditSummary) {				$.when(mw.loader.using('mediawiki.api')).then(function { return new mw.Api.getMessages( ['undo-summary'], { amlang: mw.config.get('wgContentLanguage') } ); }).then(function(message) { var editSummaryMessage = message['undo-summary']; var editSummary = editSummaryMessage.replace(/\$1/g,revId); editSummary = editSummary.replace(/\$2/g,username); if (customEditSummary !== null) { document.getElementById("mobileUndo").disabled = true; editSummary = editSummary + ' ' + customEditSummary; var api = new mw.Api({		                   ajax: {		                        headers: {		                            'Api-User-Agent': 'mobileUndo/2.0.0(https://en.wikipedia.org/wiki/User:ShahFahad191/Undo.js)'		                        }		                    }		                }); $('#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'] + ' Error Occured :( '		                        ); location.href = location.href + '#mobileUndo-info'; return; }		                   $('#mobileUndo').text('Successful'); mw.notify('Revert successful....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) +		                        ' Error Occured :(  ');		                    location.href = location.href + '#mobileUndo-info';		                }); }				});	       });	    });	}); }