User:Gary/namespace redirect.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.
/*
	NAMESPACE REDIRECT
	Description: Redirects pages that begin with certain prefixes to appropriate namespaces.
	Example: [[C:Test]] -> [[:Category:Test]], [[F:Test]] -> [[File:Test]],
                 [[T:Test]] -> [[Template:Test]],  [[UT:TEST]] -> [[User Talk:Test]]

		To edit a page, [[E:Test]]
        For spam fighting: [[L:test.com]] -> [[Special:Linksearch/*.test.com]]
*/

if (typeof(unsafeWindow) != 'undefined')
{
	console = unsafeWindow.console;
	mw = unsafeWindow.mw;
}

function namespaceRedirect()
{
	if (mw.config.get('wgPageName') != 'Special:Search') return false;

	var searchText = $('#searchText');
	if (!searchText.length) return false;
	var searchTerm = searchText.attr('value');
	var colon = searchTerm.indexOf(':');

	if (colon != -1)
	{
		var firstPart = searchTerm.substring(0, colon);
		var secondPart = searchTerm.substring(colon + 1);
		var pageName = '';
		
		switch (firstPart.toUpperCase())
		{
			case 'C':
				pageName = 'Category:' + secondPart;
				break;
			case 'F':
				pageName = 'File:' + secondPart;
				break;
			case 'T':
				pageName = 'Template:' + secondPart;
				break;
			case 'U':
				pageName = 'User:' + secondPart;
				break;
			/* User:7 - add in User Talk namespace shortcut */
			case 'UT':
				pageName = 'User Talk:' + secondPart;
				break;
			/* User:7 - add in shortcut to do a link search for spam fighters*/
			case 'L':
				pageName = 'Special:LinkSearch/*.' + secondPart;
				break;
		}
		
		if (pageName != '') window.location = mw.config.get('wgServer') + '/wiki/' + pageName;
		else if (firstPart.toUpperCase() == 'E') window.location = mw.config.get('wgScriptPath') + '/index.php?title=' + secondPart + '&action=edit';
	}
}

$(namespaceRedirect);