Jump to content

User:Magnus Manske/category intersection.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.
addOnloadHook ( add_category_intersection ) ;

ci_list = Array() ;

function add_category_intersection () {
  if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
  catlinks = document.getElementById('catlinks') ;
  if ( !catlinks ) return ;
  links = catlinks.getElementsByTagName('a');
  catname = "Category:" ;
  for ( i = 0 ; i < links.length ; i++ ) {
    t = links[i].title ;
    if ( t.substr ( 0 , catname.length ) != catname ) continue ;
    ci_list.push ( t.substr ( catname.length ) ) ;
  }
  if ( ci_list.length < 2 ) return ;
  
  a = document.createElement ( 'a' ) ;
  a.href = '#' ;
  a.onclick = category_intersection_show ;
  a.appendChild ( document.createTextNode ( 'Other articles in these categories' ) ) ;
  catlinks.appendChild ( a ) ;
}

function category_intersection_show () {
  d = document.getElementById('category_intersection_div') ;
  if ( d ) return false ;

  d = document.createElement ( 'div' ) ;
  d.id = 'category_intersection_div' ;
  d.style.border = '2px solid black' ;
  d.style.background = 'white' ;
  d.style.margin = '5px' ;
  d.style.padding = '5px' ;
  d.style.position = 'relative' ;
  d.style.zindex = 99 ;
  d.style.left = '200px' ;

  n = ci_list.length * 9 + 20 ;
  d.style.top = '-' + n + 'px' ;

  h = document.createElement ( 'h2' ) ;
  h.appendChild ( document.createTextNode ( 'Category intersection' ) ) ;
  d.appendChild ( h ) ;

  t = "" ;
  for ( i = 0 ; i < ci_list.length ; i++ ) {
    cb = document.createElement ( 'input' ) ;
    cb.type = 'checkbox' ;
    cb.name = ci_list[i] ;
    cb.value = 1 ;
    cb.checked = 1 ;
    d.appendChild ( cb ) ;
    d.appendChild ( document.createTextNode ( ci_list[i] ) ) ;
    d.appendChild ( document.createElement ( 'br' ) ) ;
  }

  x = document.createElement ( 'input' ) ;
  x.type = 'button' ;
  x.value = 'Show articles in the above category intersection' ;
  x.onclick = category_intersection_action ;
  x.style.margin_right = '50px' ;
  x.style.marginright = '50px' ;
  d.appendChild ( x ) ;

  x = document.createElement ( 'input' ) ;
  x.type = 'button' ;
  x.value = 'Cancel' ;
  x.onclick = category_intersection_cancel ;
  d.appendChild ( x ) ;

  catlinks = document.getElementById('catlinks') ;
  catlinks.appendChild ( d ) ;
  return false ;
}

function category_intersection_action () {
  d = document.getElementById('category_intersection_div') ;
  cbs = d.getElementsByTagName('input') ;
  call = Array() ;
  for ( i = 0 ; i < cbs.length ; i++ ) {
    if ( cbs[i].type != 'checkbox' ) continue ;
    if ( !cbs[i].checked ) continue ;
    call.push ( cbs[i].name.split(' ').join('_') ) ;
  }
  call = call.join("|") ;
  url = 'http://toolserver.org/~magnus/category_intersection.php?language=en&project=wikipedia&doit=1&namespace=0&categories=' + call ;
  window.location = url ;
}

function category_intersection_cancel () {
  d = document.getElementById('category_intersection_div') ;
  d.parentNode.removeChild ( d ) ;
}