User:Stirfriedcalamari/personalsidebar.js

/*

// uppercase first char, leave rest as is function ucfirst(t) { return t.charAt(0).toUpperCase + t.substring(1); }

// ajax stuff in a closure function do_ajax(list, title, i, j) { var req = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP'); debugPrint('send [' + i + '][' + j + ']: ' + title + '\n'); req.open('GET', wgScriptPath + '/index.php?title=' + title + '&action=raw', true); req.onreadystatechange = function { if (req.readyState == 4) { // MediaWiki: message exists // TODO: wiki software returns 200 instead of 404 for pages in the MediaWiki: namespace // TODO: whether they exist or not var req_status = req.status; if (req_status == 200 && req.responseText == '') { debugPrint('req.status == 200 and req.responseText == \'\'\n'); req_status == 404; }

if (req_status == 200) { debugPrint('recv [' + i + '][' + j + ']: ' + req.responseText + '\n'); list.childNodes[i-1].style.display = 'list-item'; // link if (j == 1) { debugPrint(list.childNodes[i-1].firstChild.href + ' -> '); list.childNodes[i-1].firstChild.href = '/wiki/' + req.responseText; list.childNodes[i-1].firstChild.className = ''; if (req.responseText != '') list.parentNode.parentNode.style.display = 'block'; debugPrint(list.childNodes[i-1].firstChild.href + '\n'); // text } else if (j == 2) { debugPrint(list.childNodes[i-1].firstChild.innerHTML + ' -> '); list.childNodes[i-1].firstChild.innerHTML = req.responseText; debugPrint(list.childNodes[i-1].firstChild.innerHTML + '\n'); }     // MediaWiki: message doesn't exist } else if (req_status == 404) { debugPrint('bad: ' + title + '\n'); } else { debugPrint('bad rsc status ' + req_status + '\n'); }   }  };  req.send(null); }

// use sidebar in User:xxx/MediaWiki/Sidebar function personalizeSidebar { var reqa = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');

reqa.onreadystatechange = function { if (reqa.readyState == 4) { // page exists. parse it. if (reqa.status == 200) { // process reqa.responseText var line = new Array; line = reqa.responseText.split('\n');

var portlet = null; var heading = ''; var ul = null;

for (var i = 0; i < line.length; i++) { if (line[i].indexOf('*') != 0) { continue; }         if (line[i].indexOf('**') != 0) { heading = line[i].substring(2);

portlet = document.createElement('div'); portlet.style.display = 'none'; portlet.className = 'portlet'; portlet.id = 'p-' + heading;

var h5 = document.createElement('h5'); h5.appendChild(document.createTextNode(heading)); portlet.appendChild(h5);

var pbody = document.createElement('div'); pbody.className = 'pBody'; portlet.appendChild(pbody); ul = document.createElement('ul'); pbody.appendChild(ul);

var pnav = document.getElementById('p-navigation');

// if it's name has been changed let's look for the 4th portlet if (pnav == null) { var portlets = document.getElementById('column-one').getElementsByTagName('div'); var k = 0; for (var l = 0; l < portlets.length; l++) { if (portlets[i].className.match(/\bportlet\b/)) { if (k == 3) { pnav = portlets[i]; break; }                 k++; }             }            }

pnav.parentNode.insertBefore(portlet, pnav); } else { if (line[i].indexOf('|') != -1) { var link_text = line[i].substring(3).match(/([^|]*)\|(.*)/);

var newli = document.createElement('li'); newli.style.display = 'none'; newli.appendChild(document.createElement('a')); newli.firstChild.className = 'dunno'; newli.firstChild.appendChild(document.createTextNode(link_text[2])); ul.appendChild(newli);

for (var j = 1; j < 3; j++) { // emulate wfMsgForContent using ajax var title = 'MediaWiki:' + ucfirst(link_text[j]);

var list = portlet.getElementsByTagName('ul')[0]; do_ajax(list, title, i, j); }           } else continue; }       }      // page doesn't exist. just use the global sidebar } else if (reqa.status == 404) { debugPrint('personal sidebar doesn\'t exist\n'); // something unexpected } else { debugPrint('bad rsc status ' + reqa.status + '\n'); }   }  };  reqa.open('GET', wgScriptPath + '/index.php?title=' + 'User:' + wgUserName + '/MediaWiki:Sidebar' + '&action=raw', true); reqa.setRequestHeader('Cache-Control', 'no-cache'); reqa.send(null); }

/*