Jump to content

User:Cryptic/dblredir.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.
//<pre><nowiki>
function makebutton(lbl, action)
{
  var button = document.createElement('input');
  button.type = 'button';
  button.value = lbl;
  button.setAttribute('onClick', action);
  return button;
}

function descendent_ul(node, ancestor)
{
  return (node.parentNode.tagName == 'LI'
          && (node.parentNode.parentNode == ancestor
              || (node.parentNode.parentNode.tagName == 'UL' && descendent_ul(node.parentNode.parentNode, ancestor))));
}

function fix_double_redirects()
{
  var a, i, li, uls = document.getElementsByTagName('UL'), title = escape(document.getElementsByTagName('H1')[0].firstChild.data);
  for (i = 1; i < uls.length; ++i)
    if (descendent_ul(uls[i], uls[0]))
      for (li = uls[i].firstChild; li; li = li.nextSibling)
        if (li.tagName == 'LI'
            && (a = li.firstChild).tagName == 'A'
            && a.href.indexOf('&redirect=no') >= 0)
          window.open(a.href + '&action=edit&fakeaction=fix_dbl_redir&faketarget=' + title, '_blank');
}

addOnloadHook(function ()
{
  if (new String(document.location).indexOf('Special:Whatlinkshere') >= 0)
    {
      var h = document.getElementsByTagName('H1')[0];
      h.parentNode.insertBefore(makebutton('Fix double redirects', 'javascript:fix_double_redirects()'), h.nextSibling);
    }
  else if (location.search)
    {
      var action, target, l = location.search.substring(1).split('&');
      for (var i = 0; i < l.length; ++i)
        {
          var eq = l[i].indexOf('=');
          var name = l[i].substring(0, eq);
          if (name == 'fakeaction')
            action = l[i].substring(eq + 1);
          else if (name == 'faketarget')
            target = unescape(l[i].substring(eq + 1)).replace(/_/g, ' ');
        }
      else if (action == 'fix_dbl_redir')
        {
          document.editform.wpTextbox1.value = document.editform.wpTextbox1.value.replace(/\[\[[^\]]+\]\]/, '[[' + target + ']]');
          document.editform.wpSummary.value = 'dbl redir';
          document.editform.wpMinoredit.checked = true;
          document.editform.wpDiff.click(); // insurance
        }
    }
});
//</nowiki></pre>