User:Splarka/cmlhider.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.

/* Category Member Link Hider script, version [0.0.1]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/cmlhider.js
 
On Prefixindex, will prompt for a category name, and hide all links found to pages in that category.
 
Notes:
* VERY slow, could be optimized probably.
* Limited to categories with 500 members, a query-continue would be needed for longer ones.
* Test version.
*/
 
if(wgCanonicalSpecialPageName == 'Prefixindex') addOnloadHook(hideCatMemLinksButton)
function hideCatMemLinksButton() {
  addPortletLink('p-tb','javascript:hideCML()','Hide links','t-cml','Hide links to articles in a specified category');
}
 
function hideCML() {
  var cat = prompt('Enter a category name','Category:NA-Class articles');
  if(cat == '' || !cat) return;
  var cml = document.getElementById('t-cml');
  if(cml) {
    var prog = document.createElement('img');
    prog.setAttribute('id','cml-prog');
    prog.setAttribute('src',stylepath + '/common/images/spinner.gif');
    cml.appendChild(prog);
  }
  var url = wgServer + wgScriptPath + '/api.php?action=query&format=json&callback=hideCMLCB&list=categorymembers&cmnamespace=14&cmlimit=500&cmtitle=' + cat;
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',url);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
 
function hideCMLCB(obj) {
  var links = (document.getElementById('content')) ? document.getElementById('content').getElementsByTagName('a') : document.getElementById('mw_content').getElementsByTagName('a')
  if(!obj['query'] || !obj['query']['categorymembers']) return
  for(var i in obj['query']['categorymembers']) {
    var pg = obj['query']['categorymembers'][i]['title'];
    pg = wgArticlePath.replace(/\$1/,'') + pg.replace(/ /ig,'_');
    for(var j=0;j<links.length;j++) {
      if(links[j].href.replace(wgServer,'') == pg) {
        links[j].style.visibility = 'hidden';
      }
    }
  }
  var cml = document.getElementById('t-cml');
  if(cml) document.getElementById('cml-prog').style.display = 'none'
}