User:Splarka/fetchredirects.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:Splarka/fetchredirects. |
// ==================================================
// 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');
}
}