User:SD0001/Gadget-mobile-undo.js

/** * mobileUndo, a script allowing reversion of edits from * mobile diff pages, (Special:MobileDiff) * * 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. * * Based on the original by User:FR30799386 at meta:User:FR30799386/undo.js * */

// $.when(mw.loader.using(['mediawiki.util', 'mediawiki.api', 'oojs-ui-windows', 'oojs-ui-core', 'oojs-ui.styles.icons-editing-core']), $.ready).then(function {

return new mw.Api.getMessages(['editundo', 'Undo-nochange']);

}).then(function(globalmessages) {

if (mw.config.get('wgCanonicalSpecialPageName') !== 'MobileDiff') { return; }

if ($('.warningbox').length || !mw.config.get('wgRelevantPageIsProbablyEditable')) { return; }

var msg = { "mu-success": "Successful", "mu-fail": "Failed", "mu-loading": "Loading...", "editsummary-ad": " (mu)", "mu-undo-successful": "Revert successful, reloading page", "mu-empty-reponse-from-server": "An empty response was received from the server", "mu-edit-summary-prompt": "Add a reason to your edit summary!", "mu-edit-summary-prompt-placeholder": "Reason...", "mu-thank-success": "Thanked", "mu-thank-confirm": "Send public thanks?", "mu-thank-loading": "Thanking...", "mu-thank-successful": "You thanked $1.", "mu-thank-failed": "Failed to thank $1.", "mu-http-error": "HTTP error: ", "mu-error-code": "error code: ", "mu-fail-begin-text": "Could not undo: " };

var api = new mw.Api({		ajax: { headers: { 'Api-User-Agent': 'w:en:User:SD0001/Gadget-mobile-undo.js' } }	});

mw.util.addCSS(		'#mw-mf-userinfo .mobileUndo-ui .mw-ui-icon.mw-ui-icon-before:before { display: none; }' +		'#mobileUndo > a > span.oo-ui-iconElement-icon.oo-ui-icon-editUndo.oo-ui-image-invert { display: inline-block; position:initial; }' +		'#mobileUndo > a { padding: 0.57142857em 0.9375em; }'+		'#mw-mf-userinfo > div > div.mobileUndo-ui > a > div{ line-height: normal; }'	);

$('#mw-mf-userinfo .post-content').append(		$(' ').attr('class', 'mobileUndo-ui').css({ 'display': 'inline-block', 'float': mw.config.get('wgVisualEditor').pageLanguageDir === 'ltr' ? 'right' : 'left' })	);

$('.mobileUndo-ui').append(		$('.mw-mf-action-button').remove.css({ 'clear': 'none' })	);

var mobileUndo = new OO.ui.ButtonWidget({		label: globalmessages.editundo[0].toUpperCase + globalmessages.editundo.slice(1),		icon: 'editUndo',		title: 'mobileUndo',		flags: ['destructive', 'primary'],		id: 'mobileUndo'	}); $('.mobileUndo-ui').append(mobileUndo.$element); if (mw.config.get('wgVisualEditor').pageLanguageDir === 'ltr') { $('#mobileUndo').css('margin-right', '0.3em'); } else { $('#mobileUndo').css('margin-left', '0.3em'); }

var username = $('.mw-mf-user-link').text || $('#mw-mf-userinfo .post-content div:nth-child(2)').text; var revId = $('#mw-mf-diffarea > div.mw-mf-diff-info > h3 > a').attr('href').split('&oldid=')[1];

$('#mw-mf-userinfo .post-content .mobileUndo-ui .mw-mf-action-button').click(function(e) {		e.preventDefault;		OO.ui.confirm(msg['mu-thank-confirm']).done(function(confirmed) { if (confirmed) { $('.mw-mf-action-button') .css('pointer-events', 'none') .text(msg['mu-thank-loading']);

api.postWithToken('csrf', {					action: "thank",					rev: revId,				}).done(function {					mw.notify(msg['mu-thank-successful'].replace(/\$1/g, username));					$('.mw-mf-action-button').text(msg['mu-thank-success']);				}).fail(function {					mw.notify(msg['mu-thank-failed'].replace(/\$1/g, username));					$('.mw-mf-action-button').text(msg['mu-failed']);				}); }		});	});

mobileUndo.on('click', function {		OO.ui.prompt(msg['mu-edit-summary-prompt'], { textInput: { placeholder: msg['mu-edit-summary-prompt-placeholder'] }		}).done(function(customEditSummary) {

api.getMessages(['undo-summary'], {				amlang: mw.config.get('wgServerName').split('.wik')[0]			}).then(function(message) {				var editSummary = message['undo-summary'].replace(/\$1/g, revId).replace(/\$2/g, username);				if (customEditSummary === null) {					return;				}				mobileUndo.setDisabled(true);				editSummary = editSummary + ' ' + customEditSummary + (msg['editsummary-ad']);

// Feedback UI				mobileUndo.setLabel(msg['mu-loading']); return api.postWithToken('csrf', {					action: "edit",					title: mw.config.get('wgRelevantPageName'),					summary: editSummary,					undo: revId,				});

}).done(function(result) { if (typeof result.edit.nochange !== 'undefined') { mobileUndo.setLabel(msg['mu-fail']); OO.ui.alert($(' ').html(globalmessages['Undo-nochange'])).done(function {}); return; }				mobileUndo.setLabel(msg['mu-success']); mw.notify(msg['mu-undo-successful']); setTimeout(function {					window.location.href = mw.util.getUrl('Special:MobileDiff/' + result.edit.newrevid);				}, 1500);

}).fail(function(code, jqxhr) { mobileUndo.setLabel(msg['mu-fail']); var details; if (code === 'http') { if (jqxhr.textStatus === 'error') { details = msg['mu-http-error'] + jqxhr.xhr.status; } else { details = msg['mu-http-error'] + jqxhr.textStatus; }				} else if (code === 'ok-but-empty') { details = msg['mu-empty response-from-server']; } else { details = msg['mu-error-code'] + code + ' ' + jqxhr.error.info; }				OO.ui.alert($(' ').html(msg['mu-fail-begin-text'] + details)).done(function {}); });		});	});

}); //