User:Conrad.Irwin/citationstab.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

function citations_tab(){
  
  var mt_caption = 'entry';
  var mt_title = 'View the content page [alt-c]';
  var ct_caption = 'citations';
  var ct_title = 'View the citations page';

  var lookup = new Object();
  var ct_class = '';
  var safeTitle = encodeURIComponent(wgTitle.replace(/ /g,"_"));

  //Where to put Citations tab
  var insbef = document.getElementById('ca-edit');
  if(! insbef) insbef = document.getElementById('ca-viewsource');
  if(! insbef) return false;
  
  if( wgCanonicalNamespace == 'Citations' ){  
 
    //Remove accesskeys etc from Citations tab
    var ct = document.getElementById('ca-nstab-citations');
    ct.removeAttribute('accesskey');
    ct.setAttribute('title',ct_title);
    
    //Reset discussion tab to point to Talk:
    var dt = document.getElementById('ca-talk');
    
    for(var i=0;i<dt.childNodes.length;i++){
      var anc = dt.childNodes[i];
      if(anc.nodeName.toUpperCase()=='A'){
        anc.setAttribute('href',wgArticlePath.replace("$1","Talk:"+safeTitle));
        lookup['Talk:'+wgTitle] = dt;
      }
    }
    if(dt.className && dt.className.indexOf('new'))
      dt.className = dt.className.replace('new','');
    
    //Create main tab before citations tab
    mw.util.addPortletLink('p-cactions',wgArticlePath.replace("$1",safeTitle),mt_caption,'ca-nstab-main',mt_title,null,ct)
    lookup[wgTitle] = document.getElementById('ca-nstab-main');
    
    //Move Citations tab to correct position
    var tabbar = ct.parentNode;
    tabbar.removeChild(ct);
    tabbar.insertBefore(ct,insbef);
    
  }else if( wgCanonicalNamespace == '' || wgCanonicalNamespace == 'Talk' ){
  
    mw.util.addPortletLink('p-cactions',wgArticlePath.replace("$1",'Citations:'+safeTitle),ct_caption,'ca-nstab-citations',ct_title,null,insbef); 
    lookup['Citations:'+wgTitle]=document.getElementById('ca-nstab-citations');

  }else if( wgCanonicalNamespace == 'Citations_talk' ){ //Redirect to normal talkpage

    document.location = wgArticlePath.replace("$1",encodeURIComponent('Talk:'+wgTitle.replace(/ /g,"_")))
                          + '?rdfrom=' + encodeURIComponent('Citations_talk:'+wgTitle.replace(/ /g,"_")) + '&redirect=no';

  }else{ //Nothing to see here...
  
    return false;
  
  }
  
  //Now check for red pages
  var ajaxer = sajax_init_object();
  if(! ajaxer) return false;
  
  var url = wgScriptPath+ '/api.php?format=txt&action=query&prop=info&titles=';
  var spl = '';
  for(var page in lookup){
    url+=spl+encodeURIComponent(page);
    spl='|';
  }
  
  ajaxer.onreadystatechange = function(){
    if( ajaxer.readyState == 4 ){
      if( ajaxer.status == 200 ){
        var resps = ajaxer.responseText.split('(');
        for(var i in resps){
          if(resps[i].indexOf('[missing]')>0 ){
            var start = resps[i].indexOf("[title] => ")+11;
            var end = resps[i].indexOf("\n",start);
            make_tab_red(lookup[resps[i].substr(start,end-start)]);
          }
        }
      }
    }
  }

  ajaxer.open("GET", url);
  ajaxer.send();

  function make_tab_red(tab){

    tab.className = tab.className?'new':tab.className+' new';

    for( var i=0;i<tab.childNodes.length;i++ ){
      var lnk = tab.childNodes[i];    
      if(lnk.nodeName.toUpperCase() == 'A' ){
        var href = lnk.getAttribute('href');
        lnk.setAttribute('href',href+(href.indexOf('?')>0?'&':'?')+'action=edit');
      }
    }
  }
}

$( citations_tab );