Jump to content

User:Grolltech/wikispeciestab.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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 lifted from [[User_talk:Equazcion#Script_question]]

// ** "speciestabMainTab" is a variable you need to define before you clone it.
var speciestabMainTab = $('#left-navigation li:not(.selected)').slice(0,1).css('opacity','0.9');
 
// Clone main page tab
var speciestabTab = speciestabMainTab.clone(true).attr('id', speciestabMainTab.attr('id')+'-').css('opacity','0.9').removeClass('new');
 
// ** URL for wikispecies 
var speciestabURL = 'http://' + 'species.wikimedia.org/wiki/' + mw.config.get('wgPageName');
 
// Set WikiSpecies tab URL, text, and title 
speciestabTab.find('a').attr('href', speciestabURL).empty().text('WikiSpecies').attr('title', "Navigate to this page on WikiSpecies");
 
$.ajax({
   url:"http://species.wikimedia.org/w/api.php?action=query&titles=" + encodeURIComponent(mw.config.get('wgPageName')) + "&format=xml", 
   dataType: "xml", 
   type: "GET",
   success:parseIt
});
 
// Ajax function on data return: If 'missing' field is defined, page is missing, so add 'new' class to tab, which turns it red
function parseIt (xml){
   var missing = $(xml).find('page').attr('missing');
   if (typeof missing != "undefined") speciestabTab.addClass('new');
}
 
speciestabTab.insertBefore($('#right-navigation li').slice(0,1));
 
// ** Making tab red still doesn't work for some reason, haven't figured that out yet, 
// ** but the ajax url is correct as you can see if you navigate to it manually: 
// ** http://species.wikimedia.org/w/api.php?action=query&titles=monkey&format=xml