User:Dan Polansky/Scripting

Various JavaScript for Wiktionary:

Bookmarklets
Some bookmarklets that I use for creation of Czech entries:
 * Insert Czech noun:
 * Insert Czech adjective:
 * Insert Czech verb:
 * Insert Czech adverb:
 * Replace "related to" with "relating to":
 * Note: better done using WT:AWB, as this is much faster.
 * Insert Czech adverb:
 * Replace "related to" with "relating to":
 * Note: better done using WT:AWB, as this is much faster.
 * Note: better done using WT:AWB, as this is much faster.
 * Note: better done using WT:AWB, as this is much faster.

Random from category
A bookmarkjet by JesseW from Wiktionary talk:Random page:
 * javascript:void((function {function rltr(n) { if (n>0) {return 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'[Math.floor(Math.random*52)]+rltr(n-1);} else {return ""}; }; function prc { if (req.readyState==4 && req.status==200) { var q=req.responseXML.documentElement.getElementsByTagName("cm"); document.location="/wiki/"+q[0].getAttribute("title"); } }; req=new XMLHttpRequest; req.onreadystatechange=prc; req.open("GET", document.location.protocol + "//"+document.location.host + "/w/api.php?action=query&list=categorymembers&cmtitle=" + ((document.location.pathname.indexOf("Category") !=-1 ? document.location : document.getElementById("catlinks").firstChild.childNodes[2].firstChild).pathname.replace("/wiki/","")) + "&cmstartsortkey=" + rltr(3) + "&cmlimit=1&format=xml",true); req.send(null)}))

The same bookmarklet formatted:

Issues:
 * The code picks a random sequence of three letters distinguishing lowercase and uppercase letters, and picks a word starting with that sequence. But the results so produced are not uniformly distributed across the category. Specifically, around one half of found terms start with a capital letter, while much less than half of words from a typical category start with a capital letter. A fix: (1) determine the number of entries in the category, (2) pick a random number between 1 and the number of entries, (3) pick the entry at the position of the picked random number. But how to implement this?
 * A workaround: ignore words starting with capital letters, by picking only sequences of lowercase letters.
 * Only handles entries in Latin script; not in Greek or other scripts.