User:Nardog/QuickRollback.js

(mw.config.exists('wgDiffNewId') || mw.config.get('wgNamespaceNumber') === -1 || mw.config.get('wgAction') === 'history') && mw.loader.using([	'mediawiki.api', 'mediawiki.util', 'user.options' ], function quickRollback {	[		['rollback', 'rollback'],		['rollback-tooltip', 'Restore penultimate editor\'s revision'],		['rollingback', 'rolling back'],		['rolledback', 'rolled back'],		['rolledback-tooltip', 'You have performed rollback on this page'],		['prompt', 'You\'re about to roll back edits by $1 to $2. Enter the summary (optional):'],		['custom-summary', 'Reverted edits by $1 (talk): $2'],		['custom-summary-anon', 'Reverted edits by $1: $2'],		['notification', 'Rolled back edits by $1 ($2).'],		['talk', 'talk'],		['diff-link', 'Open diff in a new tab'],		['error-rollback', 'Couldn\'t roll back.'],		['error-get-user', 'Couldn\'t retrieve the last editor.'],		['error-find-title', 'Can\'t find the page name.'],	].forEach(function (entry) { var key = 'quickrollback-' + entry[0]; if (!mw.messages.exists(key)) mw.messages.set(key, entry[1]); });	mw.loader.addStyleTag('.quickrollback-done + .quickaccept{display:none}');	var isPc = mw.config.get('wgCanonicalSpecialPageName') === 'PendingChanges';	function handler(e) {		e.preventDefault;		var $link = $(this);		var $wrapper = $link.parent;		var title;		if (isPc) {			$wrapper.parent.prevAll('a').each(function { if (mw.util.getParamValue('action', this.search) === 'history') { title = mw.util.getParamValue('title', this.search); return false; }			});		} else {			title = mw.util.getParamValue('title', this.search);		}		if (!title) {			showError('find-title');			return;		}		title = title.replace(/_/g, ' ');		$link.detach;		$wrapper.addClass('quickrollback-doing')			.text(mw.msg('quickrollback-rollingback'));		var user = !isPc && mw.util.getParamValue('from', this.search);		if (user) {			rollback(title, user, $link, $wrapper);		} else {			new mw.Api.get({ action: 'query', titles: title, prop: 'revisions', rvprop: 'user', rvlimit: 1, formatversion: 2 }).always(function (response) { user = ((((((response[0] || {}).query || {}).pages || [])[0] || {}) .revisions || [])[0] || {}).user; if (user) { rollback(title, user, $link, $wrapper); } else { showError('get-user'); $wrapper.removeClass('quickrollback-doing').html($link); }			});		}		mw.requestIdleCallback(function { var notif = $('.mw-notification-tag-quickrollback').data('mw-notification'); if (notif) notif.close; });	}	function rollback(title, user, $link, $wrapper) {		mw.loader.using('oojs-ui-windows').then(function { return OO.ui.prompt(mw.msg('quickrollback-prompt', user, title), {				textInput: { inputId: 'wpSummary' }			}); }).then(function (summary) { if (typeof summary !== 'string') { $wrapper.removeClass('quickrollback-doing').html($link); return null; }			summary = summary.trim; if (summary) { var key = 'quickrollback-custom-summary'; if (mw.util.isIPAddress(user)) key += '-anon'; summary = mw.msg(key, user, summary); }			return new mw.Api.rollback(title, user, {				summary: summary,				tags: mw.messages.get('quickrollback-tag', undefined),				errorformat: 'html',				formatversion: 2			}); }).always(function (response, error) { if (response === null) return; if (typeof response === 'object') { $wrapper.addClass('quickrollback-done') .text(mw.msg('quickrollback-rolledback')) .attr('title', mw.msg('quickrollback-rolledback-tooltip')); mw.notify($( $.parseHTML(mw.msg( 'quickrollback-notification', $('').attr({							href: mw.util.getUrl('Special:Contributions/' + user),							target: '_blank'						}).text(user)[0].outerHTML, $('').attr({							href: mw.util.getUrl('User talk:' + user),							target: '_blank'						}).text(mw.msg('quickrollback-talk'))[0].outerHTML )).concat(						$(' ').append( $('').attr({								href: mw.util.getUrl(response.title || title, { diff: response.revid || 'cur', diffonly: 1 }),								target: '_blank'							}).text(mw.msg('quickrollback-diff-link')) )[0]					)				), { tag: 'quickrollback' }); } else { var msg = (((error || {}).errors || [])[0] || {}).html; showError('rollback', msg && $.parseHTML(msg)); $wrapper.html($link); }			$wrapper.removeClass('quickrollback-doing'); });	}	function showError(key, msg) {		mw.notify(msg || mw.msg('quickrollback-error-' + key), { tag: 'quickrollback', type: 'error' });	}	if (isPc) {		mw.hook('wikipage.content').add(function ($content) { var $button = $(' ').addClass('quickrollback').append(				$('').attr({ href: '#', role: 'button', title: mw.msg('quickrollback-rollback-tooltip') }).text(mw.msg('quickrollback-rollback')).on('click', handler)			); var $acceptButtons = $content.find('.quickaccept'); if ($acceptButtons.length) { $acceptButtons.before($button); } else { $content.find('form[name="pendingchanges"] ~ ul > li').append(					' ',					$(' ').addClass('mw-changeslist-links mw-pager-tools').append($button)				); }		});	} else if (Number(mw.user.options.get('showrollbackconfirmation')) !== 1) {		$(document.body).on('click', '.mw-rollback-link > a', handler)			.addClass('quickrollback-ready');	} });