User:Omegatron/monobook.js/namedredirect.js
/*
*/*/ // Workaround for bugzilla: 218
// Currently this function just directs the window to the desired section. It could be re-written to add a link to that section instead.
function linkDesiredSection(section) {
if (window.location.hash == '') {
window.location.hash = section;
}
}
// Find the section you were supposed to go to
function findDesiredSection(http_request) {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
// Find the redirect link, it's the only link in a span of class "redirectText"
spans = http_request.responseXML.getElementsByTagName('span');
for (i = 0; i < spans.length; ++i) {
if (http_request.responseXML.getElementsByTagName('span')[i].className == "redirectText") {
break;
}
}
// Back in the page you are visiting, create a link that goes to the right section
if (http_request.responseXML.getElementsByTagName('span')[i].firstChild.hash) {
linkDesiredSection(http_request.responseXML.getElementsByTagName('span')[i].firstChild.hash);
}
}
else {
alert('redirector: There was a problem with the request.');
return false;
}
}
}
// Download the redirect page asynchronously
// from http://developer.mozilla.org/en/docs/AJAX:Getting_Started
function makeRequest(url) {
var http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
// See note below about this line
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('redirector: Giving up :( Cannot create an XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function() { findDesiredSection(http_request); };
http_request.open('GET', url, true);
http_request.send(null);
}
// If the page is redirected, find the redirector
addOnloadHook( function () {
// Check in the little div underneath the main heading, where redirect notes appear
links = document.getElementById('contentSub').getElementsByTagName('a');
for (i = 0; i < links.length; ++i) {
// If there's a link that says redirect=no, it's probably the redirect
if (links[i].href.indexOf('redirect=no') > 0) {
makeRequest(links[i].href);
break;
}
// If this isn't working reliably, try a text search for "Redirected from" instead
}
});
/*