User:Gary/contribs alt link.js

/*

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 = ' | ' + (altType ? 'talk' : 'main') + '';

$('a:eq(1)', $(this)).after(altLink)

});

}

if (mw.config.get('wgCanonicalSpecialPageName') == 'Contributions')

{

$(document).ready(function()

{

addLinkToTalkPage();

});

}