User:QwerpQwertus/js.js

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
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.
//Based on Gender dimorphism script by [[User:Fran Rogers]]
 
// this a user page or user talk page
if ((wgNamespaceNumber == 2 || wgNamespaceNumber == 3) &&
    !/\//.test(wgTitle)) {
  // then when we visit said page...
  addOnloadHook(function() {
    var a = sajax_init_object();
    //Ask the api the user's gender in json 
    a.open("GET", wgServer + wgScriptPath + 
                  "/api.php?format=json&action=query&list=users&ususers=" + 
                  escape(wgTitle.replace(/ /, "_")) + "&usprop=gender",
           true);
 
    // wait till the api awnsers
    queryapi.onreadystatechange = function() {
      if(queryapi.readyState == 4 && queryapi.status == 200) {
        // parse - extract the gender word from the response (in json)
        var genderText = 
          eval("(" + a.responseText + ")").query.users[0].gender;
 
        // U+2640 is female and U+2642 male and U+00BF is an upside down question mark as found at http://en.wikipedia.org/wiki/Miscellaneous_Symbols - set them
        var genderSymbol = "";
        if (genderText == "female") {
          genderSymbol = "♀";//Female - use that symbol
        } else if (genderText == "male") {
          genderSymbol = "♂";//male - use that symbol
        } else if (genderText == "undefined") {
          genderSymbol = "¿";//not set - use that symbol
        }
 
 
        // add the symbol
        if (genderSymbol != "") {
          document.getElementById("firstHeading").innerHTML += 
            " " + genderSymbol;
        }
      }
    };
 
    // ask the api - the rest of the script is waiting
    queryapi.send();
  });
}