User:Nardog/QuickRollback.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Nardog/QuickRollback. |
(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 [[Special:Contributions/$1|$1]] ([[User talk:$1|talk]]): $2'],
['custom-summary-anon', 'Reverted edits by [[Special:Contributions/$1|$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',
$('<a>').attr({
href: mw.util.getUrl('Special:Contributions/' + user),
target: '_blank'
}).text(user)[0].outerHTML,
$('<a>').attr({
href: mw.util.getUrl('User talk:' + user),
target: '_blank'
}).text(mw.msg('quickrollback-talk'))[0].outerHTML
)).concat(
$('<p>').append(
$('<a>').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 = $('<span>').addClass('quickrollback').append(
$('<a>').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(
' ',
$('<span>').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');
}
});