User:Fran Rogers/dimorphism.js
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.
/**
* Gender dimorphism script by [[User:Fran Rogers]]
* Displays a user's gender in the title of their user/user talk page for you
* if they've specified it in their preferences. Say goodbye to embarrassing
* pronoun mistakes!
*/
// If on a user or user talk page, and not a subpage...
if ((mw.config.get('wgNamespaceNumber') == 2 || mw.config.get('wgNamespaceNumber') == 3) &&
!/\//.test(mw.config.get('wgTitle'))) {
// add a hook to...
$(function() {
if(typeof sajax_init_object === 'undefined') return;
// init AJAX and request the user's gender from the API
var a = sajax_init_object();
a.open("GET", mw.config.get('wgServer') + mw.config.get('wgScriptPath') +
"/api.php?format=json&action=query&list=users&ususers=" +
escape(mw.config.get('wgTitle').replace(/ /, "_")) + "&usprop=gender",
true);
// when response arrives...
a.onreadystatechange = function() {
if(a.readyState == 4 && a.status == 200) {
// parse the JSON response
var genderText =
eval("(" + a.responseText + ")").query.users[0].gender;
// U+2640 and U+2642 are female and male signs respectively.
var genderSymbol = "";
if (genderText == "female") {
genderSymbol = "♀";
} else if (genderText == "male") {
genderSymbol = "♂";
}
// if gender was specified, append the symbol
if (genderSymbol != "") {
document.getElementById("firstHeading").innerHTML +=
" " + genderSymbol;
}
}
};
// send the API request
a.send();
});
}