User:FelixReimann/taxobox.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* <nowiki>
 *
 * taxobox.js by [[User:FelixReimann]] with help from [[User:Ricordisamoa]]
 * based on [[User:Ricordisamoa/Wikispecies.js]]
*/
$(document).ready(function(){
    // only on entities, not properties or other namespaces
    if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
    if ( mw.config.get('wgAction') != 'view' ) return ;
    if( typeof wbEntity== 'undefined' ) return;
    
    var entity=JSON.parse( wbEntity );
    if( typeof entity.claims=='undefined' ) return;
    
    if( Object.keys( entity.claims ).indexOf( 'P225' ) != -1 ){// 'taxon name' is present
        var language = mw.user.options.get('language');
        var count = 5;
        if (mw.config.get('taxoboxCount')) {
        	count = mw.config.get('taxoboxCount');
        }
        var template = '<div class="wikibase-taxobox">{{Taxobox|qid=' + mw.config.get('wgPageName') + '|count=' + count + '|lang=' + language + '}}<div class="wikibase-taxobox-references" style="clear:right;"><references/></div></div>';
        // https://www.wikidata.org/w/api.php?action=parse&text={{Taxobox|qid=Q36146}}&prop=text&contentmodel=wikitext&format=jsonfm
        $.getJSON(mw.util.wikiScript('api'),
            {
                action:'parse',
                format:'json',
                text:template,
                contentmodel:'wikitext',
                prop:'text'
            },
            function(data){
                var text=data.parse.text;
                $('.wikibase-sitelinkgrouplistview').append(text['*']); // TODO insert text
            });
	}
});
//</nowiki>