User:Splarka/fetchredirects.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
// ==================================================
//  Fetch and Show redirects in content sub (test)
// ==================================================
// Yes, is ugly. This is just a test.
// Someone else do it better.
 
var rdlist = document.createElement('span');
var getReq;
 
addOnloadHook(getRedirectsButton);
function getRedirectsButton() {
  addPortletLink('p-tb','javascript:getRedirects();','get redirects','t-getredirects')
}
 
function getRedirects() {
  rdlist.appendChild(document.createElement('br'));
  rdlist.appendChild(document.createTextNode('fetching...'));
  document.getElementById('contentSub').appendChild(rdlist);
  var url = wgServer + wgScript + '?title=Special:Whatlinkshere/' + wgPageName + '&limit=1000';
  getXML(url,getRedirectsStateChange);
  document.getElementById('t-getredirects').style.display='none';
}
 
function getRedirectsStateChange() {
  switch (getReq.readyState) {
    case 1: break;
    case 2: break;
    case 3: break;
    case 4:
      if (getReq.status == 200) { // OK response
        clearNode(rdlist);
        var txt = getReq.responseText;
        var rdp = /\<a[^>]*>[^<]*\<\/a\> \(redirect page\)/ig;
        rdlist.appendChild(document.createElement('br'));
        rdlist.appendChild(document.createTextNode('Redirects to this page:'));
        rdlist.appendChild(document.createElement('br'));
        var redir = txt.match(rdp);
        if(redir) {
          for(var i=0;i<redir.length;i++) {
          //just a test, should replace with proper DOM if going to be used
          rdlist.innerHTML += redir[i] + '<br>';
          }
        }
      } else {
        rdlist.appendChild(document.createTextNode('** Problem ** ' + getReq.statusText))
      }
      break;
  }
}
 
function clearNode(obj) {
  while(obj.firstChild) obj.removeChild(obj.firstChild);
}
 
function getText(obj) {
  if (obj.nodeType == 3) return obj.nodeValue;
  var txt = new Array();
  var i=0;
  while(obj.childNodes[i]) {
    txt[txt.length] = getText(obj.childNodes[i]);
    i++;
  }
  return txt.join('');
}
 
function getXML(url,func) {
  if (window.XMLHttpRequest) { // Non-IE browsers
    getReq = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    getReq = new ActiveXObject('Microsoft.XMLHTTP');
  }
  if (getReq) {
    getReq.onreadystatechange = func;
    try {
      getReq.open('GET', url, true);
      getReq.send('');
    } catch (e) {
      alert(e);
    }
  } else {
    alert('XMLHTTPRequest not supported');
  }
}