User:Sander Säde/dabcheck.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:Sander Säde/dabcheck. |
$(DabButton);
function DabButton() {
// if ((document.URL.indexOf(':', 8) != -1) && (document.URL.indexOf('Talk:') == -1)) return;
//if (document.URL.indexOf('/wiki/') == -1) return;
mw.util.addPortletLink( chooseBox( 'dabcheck', TwinkleConfig.toolboxButtons ), "javascript:checkdabs()", "dabcheck", "", "", "");
}
function checkdabs() {
document.getElementById("contentSub").innerHTML = "";
var tx = document.getElementById("bodyContent");
var anchor;
var url;
var aclean = new Array();
var anchors =
tx.getElementsByTagName("a");
for(var i=0; i < anchors.length; i++)
{
anchor = anchors[i];
if (anchor.getAttribute("class") == "internal") continue; //images
url = anchors[i].getAttribute("href");
if ((url == null) || (url == "")) continue;
if (url.indexOf("http://") != -1) continue;
if (url.indexOf("/wiki/") == -1) continue;
url = anchor.getAttribute("title");
if ((url == null) || (url == "")) continue;
if (url.indexOf("Category:") != -1) continue;
if (url.indexOf("Special:") != -1) continue;
if (url.indexOf("Wikipedia:") != -1) continue;
if (url.indexOf("talk:") != -1) continue;
aclean.push(anchor);
}
aclean.sort();
aclean = removeDuplicates(aclean);
document.getElementById("contentSub").innerHTML = "Total " + aclean.length + " innerwiki links found.<br />Checking them for disambiguation and redirect pages<br />";
for(var i=0; i < aclean.length; i++)
{
anchor = aclean[i];
var dabajax = sajax_init_object();
var addr = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + "/index.php?title=" + anchor.getAttribute("title") + "&action=raw"
dabajax.open("GET", addr, false);
dabajax.onreadystatechange = function()
{
if(dabajax.readyState != 4) return;
dabTextReceived(dabajax.responseText, anchor);
};
dabajax.send();
}
}
function dabTextReceived(sText, oAnchor) {
//redirects
if (sText.indexOf("#REDIRECT") != -1) {
url = document.getElementById("contentSub").innerHTML + oAnchor.getAttribute("title").link(oAnchor.getAttribute("href")) + ": redirect".fontcolor("maroon") + "<br/>";
document.getElementById("contentSub").innerHTML = url;
return;
}
//dab
//check title containing disamb, regular dab templates and as a last measure, various "dis}}" pages
if ((oAnchor.getAttribute("title").indexOf("disamb") != -1)
|| (oAnchor.getAttribute("title").indexOf("Disamb") != -1)
|| (sText.indexOf("{disambig}") != -1)
|| (sText.indexOf("{dab}") != -1)
|| (sText.indexOf("{Disambiguation}") != -1)
|| (sText.indexOf("{disambiguation}") != -1)
|| (sText.indexOf("{disambiguation}") != -1)
|| (sText.indexOf("dis}}") != -1)) {
url = document.getElementById("contentSub").innerHTML + oAnchor.getAttribute("title").link(oAnchor.getAttribute("href")) + ": disambiguation".fontcolor("red") + "<br/>";
document.getElementById("contentSub").innerHTML = url;
return;
}
//OK
url = document.getElementById("contentSub").innerHTML + oAnchor.getAttribute("title").link(oAnchor.getAttribute("href")) + ": OK".fontcolor("green") + "<br/>";
document.getElementById("contentSub").innerHTML = url;
}
function removeDuplicates(arr)
{
var result=new Array();
var lastValue="";
for (var i=0; i<arr.length; i++)
{
var curValue=arr[i].getAttribute("href");
if (curValue != lastValue)
{
result[result.length] = arr[i]
}
lastValue=curValue;
}
return result;
}