Jump to content

User:Sander Säde/dabcheck.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(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;
}