User:Nardog/QuickAccept.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/QuickAccept. |
(mw.config.get('wgNamespaceNumber') === -1 ||
mw.config.get('wgAction') === 'history') &&
mw.loader.using(['mediawiki.api', 'mediawiki.util'], function quickAccept() {
[
['accept', 'accept'],
['unaccept', 'unaccept'],
['accepting', 'accepting'],
['unaccepting', 'unaccepting'],
['accept-tooltip', 'Accept this pending revision'],
['unaccept-tooltip', 'Make this revision pending again'],
['error-accept', 'Couldn\'t accept the revision.'],
['error-unaccept', 'Couldn\'t unaccept the revision.'],
['error-get-id', 'Couldn\'t retrieve the revision ID.'],
['error-find-id', 'Can\'t find the revision ID.'],
['error-find-title', 'Can\'t find the page name.'],
].forEach(function (entry) {
var key = 'quickaccept-' + entry[0];
if (!mw.messages.exists(key)) mw.messages.set(key, entry[1]);
});
var isPc = ['PendingChanges', 'UnreviewedPages'].includes(mw.config.get('wgCanonicalSpecialPageName'));
function handler(e) {
e.preventDefault();
var $link = $(this);
var $wrapper = $link.parent();
var isUndo = $wrapper.hasClass('quickaccept-undo');
var id = $wrapper.data('quickacceptId') ||
!isPc && $wrapper.closest('[data-mw-revid]').data('mwRevid');
if (id) {
$link.detach();
$wrapper.addClass('quickaccept-doing')
.text(mw.msg('quickaccept-' + (isUndo ? 'unaccepting' : 'accepting')));
accept(id, isUndo, $link, $wrapper);
} else if (isPc) {
var title;
$wrapper.parent().prevAll('a').each(function () {
if (mw.util.getParamValue('action', this.search) === 'history') {
title = mw.util.getParamValue('title', this.search);
return false;
}
});
if (!title) {
showError('find-title');
return;
}
$link.detach();
$wrapper.addClass('quickaccept-doing')
.text(mw.msg('quickaccept-' + (isUndo ? 'unaccepting' : 'accepting')));
new mw.Api().get({
action: 'query',
titles: title,
prop: 'info',
formatversion: 2
}).always(function (response) {
id = ((((response || {}).query || {}).pages || [])[0] || {}).lastrevid;
if (id) {
accept(id, isUndo, $link, $wrapper);
} else {
showError('get-id');
$wrapper.removeClass('quickaccept-doing').html($link);
}
});
} else {
showError('find-id');
return;
}
mw.requestIdleCallback(function () {
var notif = $('.mw-notification-tag-quickaccept').data('mw-notification');
if (notif) notif.close();
});
}
function accept(id, isUndo, $link, $wrapper) {
new mw.Api().postWithEditToken({
action: 'review',
revid: id,
unapprove: isUndo ? 1 : undefined,
errorformat: 'html',
formatversion: 2
}).always(function (response, error) {
if (((response || {}).review || {}).result === 'Success') {
$link.text(mw.msg('quickaccept-' + (isUndo ? 'accept' : 'unaccept')))
.attr('title', mw.msg(
'quickaccept-' + (isUndo ? 'accept' : 'unaccept') +
'-tooltip'
));
$wrapper.toggleClass('quickaccept-undo', !isUndo)
.attr('data-quickaccept-id', id);
} else {
var msg = (((error || {}).errors || [])[0] || {}).html;
showError(isUndo ? 'unaccept' : 'accept', msg && $.parseHTML(msg));
}
$wrapper.removeClass('quickaccept-doing').html($link);
});
}
function showError(key, msg) {
mw.notify(msg || mw.msg('quickaccept-error-' + key), {
tag: 'quickaccept',
type: 'error'
});
}
mw.hook('wikipage.content').add(function ($content) {
var $lis;
if (isPc) {
$lis = $content.find('form[name="pendingchanges"] ~ ul > li, form[name="unreviewedpages"] ~ ul > li');
} else if (mw.config.get('wgAction') === 'history') {
$lis = $content.find('.flaggedrevs-pending');
} else {
$lis = $content.find('.mw-contributions-list > .flaggedrevs-pending, .mw-contributions-list > .flaggedrevs-unreviewed');
if (!$lis.length) {
$lis = $content.find('.mw-changeslist-edit.mw-changeslist-last .mw-changeslist-line-inner[data-target-page]')
.has('.mw-fr-reviewlink');
$lis = $lis.add(
$content.find('table.mw-changeslist-edit').has('.mw-fr-reviewlink')
.find('.mw-changeslist-last > .mw-enhanced-rc-nested[data-target-page]')
);
}
}
if (!$lis.length) return;
var $tools = $lis.children('.mw-changeslist-links.mw-pager-tools');
$tools = $tools.add(
$lis.not($tools.parent())
.append(' ', $('<span>').addClass('mw-changeslist-links mw-pager-tools'))
.children('.mw-changeslist-links.mw-pager-tools')
);
$('<span>').addClass('quickaccept').append(
$('<a>').attr({
href: '#',
role: 'button',
title: mw.msg('quickaccept-accept-tooltip')
}).text(mw.msg('quickaccept-accept')).on('click', handler)
).appendTo($tools);
});
});