User:Nardog/QuickAccept.js

(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(' ', $(' ').addClass('mw-changeslist-links mw-pager-tools'))				.children('.mw-changeslist-links.mw-pager-tools')		); $(' ').addClass('quickaccept').append(			$('').attr({ href: '#', role: 'button', title: mw.msg('quickaccept-accept-tooltip') }).text(mw.msg('quickaccept-accept')).on('click', handler)		).appendTo($tools); }); });