Jump to content

User:Gary/contribs alt link.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.
/*
	CONTRIBUTIONS ALTERNATIVE LINKS
	Description: On a user's contributions page, an extra link is added in "(diff | hist)".
		It appears as either "(diff | main | hist)" or "(diff | talk | hist)", and either links
		to the page's Main page or Talk page.
*/

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

function addLinkToTalkPage()
{
	$('#bodyContent > ul li').each(function()
	{
		var pageLink = $('a:eq(3)', $(this));
		if (!pageLink.length) pageLink = $('a:eq(2)', $(this));
		var link = pageLink.href;
		var name = pageLink.text();
		var namespace = name.substring(0, name.indexOf(':')).toLowerCase().replace(/ /g, '_');
		var title;
		
		// Is non-article namespace
		if (typeof(mw.config.get(namespace)) != 'undefined')
		{
			title = name.substring(name.indexOf(':') + 1);
		}
		// Is article namespace
		else
		{
			namespace = '';
			title = name;
		}
		
		// This is already a talk page.
		var altNamespace, altType;
		var namespaceId = mw.config.get('wgNamespaceIds')[namespace];
		
		// Is a main page, so output a talk page
		if (namespaceId % 2 == 0)
		{
			altNamespace = mw.config.get('wgFormattedNamespaces')[namespaceId + 1];
			altType = 1;
		}
		// Is a talk page, so output a main page
		else
		{
			altNamespace = mw.config.get('wgFormattedNamespaces')[namespaceId - 1];
			altType = 0;
		}
		
		var altPage = (altNamespace ? (altNamespace + ':' + title) : title).replace(/ /g, '_');
		var altLink = ' | <a class="contribs-alt-page" href="' + mw.config.get('wgScript') + '?title=' + altPage + '">' + (altType ? 'talk' : 'main') + '</a>';
		$('a:eq(1)', $(this)).after(altLink)
	});
}

if (mw.config.get('wgCanonicalSpecialPageName') == 'Contributions')
{
	$(document).ready(function()
	{
		addLinkToTalkPage();
	});
}