Jump to content

User:Amorymeltzer/logSwap.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.
// Creates a button on [[Special:Log]] that swaps the user log and logs on that user
// Original inspiration from [[User:PleaseStand/common.js]]

$(function() {
	if (mw.config.get('wgCanonicalSpecialPageName') === 'Log') {
		mw.loader.using(['oojs-ui-core', 'oojs-ui-widgets'], function () {
			var rsbutton = new OO.ui.ButtonInputWidget({label: 'Swap roles', useInputTag: true});
			rsbutton.$element.children('input').click(function(event) {
				var $user = $(document.getElementById('mw-input-user').firstChild), $page = $(document.getElementById('mw-input-page').firstChild),
					oldUser = $.trim($user.val()), oldPage = /^user:(.+)$/i.exec($.trim($page.val()));
				$page.val(oldUser ? 'User:' + oldUser : '');
				$user.val(oldPage ? oldPage[1] : '');
				event.preventDefault();
			});
			$('.mw-htmlform-submit-buttons').append(rsbutton.$element);
		});
	}
});