User:MZMcBride/stripredlinks.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 code adds a link to the "Toolbox" section ("p-tb") of the sidebar.
 */
addOnloadHook(function() {
  mw.util.addPortletLink('p-tb',
                 'javascript:stripredlinks()',
                 'Remove red links',
                 't-unred',
                 'Remove all red <li> links on this page');
})

/**
 * This code looks at the <a> HTML element inside list items in a page's body content.
 * Red links have the attribute class="new" (so that [[CSS]] can make them red).
 * This script just looks for that particular class and then removes those elements.
 */
function stripredlinks() {
  var lis = document.getElementById('bodyContent').getElementsByTagName('li');
  for(var i=lis.length-1;i>-1;i--) {
    var as = lis[i].getElementsByTagName('a');
    for(var j=as.length-1;j>-1;j--) {
      if(as[j].className.indexOf('new')!=-1) lis[i].parentNode.removeChild(lis[i])
    }
  }
}