User:Gary/contribs alt link.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:Gary/contribs alt link. |
/*
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();
});
}