Jump to content

User:Nardog/QuickThank.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.config.get('wgNamespaceNumber') === -1 &&
mw.loader.using(['mediawiki.api', 'mediawiki.util'], function quickThank() {
	var user = mw.config.get('wgUserName');
	var isContribs = mw.config.get('wgCanonicalSpecialPageName') === 'Contributions';
	if (isContribs) {
		var relUser = mw.config.get('wgRelevantUserName');
		if (relUser === user || mw.util.isIPAddress(relUser, true)) return;
	}
	mw.loader.addStyleTag('.quickthank::before{content:" ["} .quickthank::after{content:"]"}');
	function mutationCallback(records, observer) {
		var span = records[0].target;
		var $child = $(span).children();
		if (!$child.length || $child.hasClass('mw-thanks-thank-confirmation')) {
			observer.disconnect();
			span.classList.add('quickthank-thanked');
		}
	}
	mw.hook('wikipage.content').add(function ($content) {
		var data = [];
		var query = isContribs
			? '.mw-contributions-list > li'
			: '.mw-changeslist-edit.mw-changeslist-user-registered.mw-changeslist-others.mw-changeslist-human .mw-changeslist-line-inner, .mw-changeslist-edit.mw-changeslist-user-registered.mw-changeslist-others.mw-changeslist-human .mw-enhanced-rc-nested';
		var $lis = $content.find(query);
		$lis = $lis.filter(function (i) {
			var $parent = $lis.eq(i).closest('[data-mw-revid]');
			var id = $parent.data('mwRevid');
			if (!id) return;
			var recipient = isContribs
				? mw.config.get('wgRelevantUserName')
				: $parent.find('.mw-userlink').first().text();
			if (recipient === user) return;
			data.push([id, recipient]);
			return true;
		});
		if (!$lis.length) return;
		new mw.Api().loadMessagesIfMissing([
			'thanks-thank', 'thanks-thank-tooltip'
		]).then(function () {
			$lis.append(function (i) {
				var span = $('<span>').addClass('quickthank').append(
					$('<a>').attr({
						class: 'mw-thanks-thank-link',
						href: mw.util.getUrl('Special:Thanks/' + data[i][0]),
						title: mw.msg('thanks-thank-tooltip', user, data[i][1]),
						'data-revision-id': data[i][0],
						'data-recipient-gender': 'unknown'
					}).text(mw.msg('thanks-thank', user, data[i][1]))
				)[0];
				new MutationObserver(mutationCallback)
					.observe(span, { childList: true });
				return span;
			});
			mw.config.set('thanks-confirmation-required', true);
			if (mw.loader.getState('ext.thanks.corethank') === 'ready') {
				mw.loader.moduleRegistry['ext.thanks.corethank'].script($);
			} else {
				mw.loader.load('ext.thanks.corethank');
			}
		});
	});
});