User:Connel MacKenzie/custom.js/citetab.js
Note – after saving, you may have to bypass your browser’s cache to see the changes.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
/*
[[User:Hippietrail]] wrote this.
<pre>
*/
// look for a wiki link with a specific title attribute and return the link's class name
// possible returns: null, '', 'new', 'new selected', 'selected new'
function lookforlink(tabli, title) {
var foundclass = 'dunno';
var allas = document.getElementById('bodyContent').getElementsByTagName('a');
if (allas != -1) {
for (var i = 0; i < allas.length; i++) {
if (allas[i].title == title) {
foundclass = allas[i].className;
break;
}
}
}
// no link in this document so use ajax
if (foundclass == 'dunno') {
var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
req.open('GET', wgScriptPath + '/index.php?title=' + title + '&action=raw', true);
req.onreadystatechange = function(event) {
var li = tabli;
if (req.readyState == 4) {
// page exists. make tab link blue
if (req.status == 200) {
//li.removeAttribute('className');
li.className = null;
// page doesn't exist. make tab link red and change href to action edit
} else if (req.status == 404) {
li.className = 'new';
li.firstChild.href = li.firstChild.href.replace(/\/wiki\/(.*)/, '/w/index.php?title=$1&action=edit');
} else {
debugPrint('bad rsc status ' + req.status + '\n');
}
}
};
req.send(null);
}
return foundclass;
}
// set or change the class and href of a tab
function fixtab(li, classes, title)
{
if (classes && classes.match(/\bnew\b/))
li.firstChild.href = wgScriptPath + '/index.php?title=' + title + '&action=edit';
else
li.firstChild.href = '/wiki/' + title;
if (classes != null)
li.className = classes;
else
//li.removeAttribute('className');
li.className = null;
}
// add citations tab
function addCiteTab() {
var currentpagetype = 'dunno';
var maintitle;
var talktitle;
var citetitle;
// make the correct article names for each tab
switch (document.getElementsByTagName('body')[0].className.match(/\bns-(-?\d+)\b/)[1])
{
case '0':
if (wgTitle.search(/^Citations:/) != -1) {
currentpagetype = 'cite';
maintitle = wgTitle.replace(/^Citations:(.*)$/, '$1');
talktitle = 'Citations talk:' + maintitle;
citetitle = wgTitle;
} else {
currentpagetype = 'article';
maintitle = wgTitle;
talktitle = 'Talk:' + maintitle;
citetitle = 'Citations:' + wgTitle;
}
break
case '1':
currentpagetype = 'talk';
maintitle = wgTitle;
talktitle = 'Talk:' + maintitle;
citetitle = 'Citations:' + wgTitle;
break
default:
return;
}
var maintabli = document.getElementById('ca-nstab-main');
var talktabli = document.getElementById('ca-talk');
var citetabli = document.createElement('li');
citetabli.id = 'ca-cite';
citetabli.appendChild(document.createElement('a'));
citetabli.firstChild.appendChild(document.createTextNode('citations'));
if (currentpagetype == 'article' || currentpagetype == 'talk') {
fixtab(citetabli, lookforlink(citetabli, citetitle), citetitle);
} else if (currentpagetype == 'cite') {
// move article tab href and classes to cite tab
fixtab(citetabli, maintabli.className, citetitle);
fixtab(maintabli, lookforlink(maintabli, maintitle), maintitle);
fixtab(talktabli, lookforlink(talktabli, talktitle), talktitle);
}
// add the cite tab
talktabli.parentNode.insertBefore(citetabli, talktabli.nextSibling);
}
addOnloadHook( addCiteTab );
/*
</pre>
*/