Jump to content

User:Iceblock/wlh.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.
// This script creates a list of pages that redirects to the current page. The list is put at the top of every visited page. The maximum is 50 redirects. If there 50 or more redirects, a link to Special:WhatLinksHere is provided.
// Made by [[User:Iceblock]]

function wlh()
{ 
   if (window.XMLHttpRequest) {
       var req = new XMLHttpRequest();
   }
   else if (window.ActiveXObject) {
      var req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req) {
    req.open("GET", wgServer + wgScriptPath+"/api.php?action=query&list=backlinks&bltitle="+encodeURIComponent(mw.config.get('wgPageName'))+"&bllimit=50&blfilterredir=redirects&format=xml", false);
    req.send("");
    var response = req.responseXML;
    var wlhdiv = document.createElement("div");
    var tableNode = document.createElement("table");
    tableNode.style.borderColor="#888";
    tableNode.style.borderWidth="1px";
    tableNode.style.borderStyle="solid";
    tableNode.style.width="100%";
    document.getElementById("bodyContent").insertBefore(wlhdiv, document.getElementById("jump-to-nav"));
    for (x = 0; x < response.getElementsByTagName("bl").length; x++) {
      if (x % 8 == 0) var trNode = document.createElement("tr");
      var tdNode = document.createElement("td");
      var textNode = document.createTextNode(response.getElementsByTagName("bl")[x].getAttribute("title"));
      var aNode = document.createElement("a");
      aNode.href=mw.config.get('wgServer')+mw.config.get('wgScript')+"?title="+encodeURIComponent(response.getElementsByTagName("bl")[x].getAttribute("title"))+"&redirect=no"; 
      aNode.appendChild(textNode);
      tdNode.appendChild(aNode);
      trNode.appendChild(tdNode);
      tableNode.appendChild(trNode);
    }
    if (response.getElementsByTagName("bl").length == 0) {
      var trNode = document.createElement("tr");
      var tdNode = document.createElement("td");
      var textNode = document.createTextNode("No redirects found.");    
      tdNode.appendChild(textNode);
      trNode.appendChild(tdNode);
      tableNode.appendChild(trNode);
    }
    wlhdiv.appendChild(document.createTextNode("These pages redirect to the current page: "));
    if (response.getElementsByTagName("bl").length >= 50) {
      var aNode = document.createElement("a");
      aNode.href=wgServer + wgScript+"?title=Special%3AWhatLinksHere&hidetrans=1&hidelinks=1&target="+encodeURIComponent(mw.config.get('wgPageName'));
      aNode.appendChild(document.createTextNode("List all"));
      wlhdiv.appendChild(aNode);
   }  
    wlhdiv.appendChild(tableNode);
  }
 }

$ ( wlh );