User:Dan Polansky/Scripting
Appearance
Various JavaScript for Wiktionary:
Bookmarklets
[edit]Some bookmarklets that I use for creation of Czech entries:
- Insert Czech noun:
javascript:rod=prompt('Rod:');eng=prompt('Anglicky:');insertTags('==Czech==\n\n===Noun===\n{{cs-noun|g='+rod+(wgTitle.match(/%20/)?'|sg=[['+wgTitle.replace(/%20/g,']]%20[[')+']]':'')+'}}\n\n#%20[['+(eng==wgTitle?'#English|':'')+eng+']]\n','','');document.editform.wpSummary.value='+Czech%20noun';document.editform.wpSave.focus()
- Insert Czech adjective:
javascript:eng=prompt('Anglicky:');insertTags('==Czech==\n\n===Adjective===\n{{cs-adj}}\n\n#%20[['+eng+']]\n','','');document.editform.wpSummary.value='+Czech%20adjective';document.editform.wpSave.focus()
- Insert Czech verb:
javascript:eng=prompt('Anglicky:');insertTags('==Czech==\n\n===Verb===\n{{cs-verb}}\n\n#%20to%20[['+eng+']]\n','','');document.editform.wpSummary.value='+Czech%20verb';document.editform.wpSave.focus()
- Insert Czech adverb:
javascript:eng=prompt('Anglicky:');insertTags('==Czech==\n\n===Adverb===\n{{cs-adv}}\n\n#%20[['+eng+']]\n','','');document.editform.wpSummary.value='+Czech%20adverb';document.editform.wpSave.focus()
- Replace "related to" with "relating to":
javascript:content=document.editform.wpTextbox1.value;content=content.replace(/(of),?(%20or%20)related(%20to)/gi,'$1$2relating$3');document.editform.wpSummary.value+='of%20or%20related%20to/of%20or%20relating%20to';document.editform.wpTextbox1.value=content;document.editform.wpSave.focus()
- Note: better done using WT:AWB, as this is much faster.
Random from category
[edit]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:
javascript:void(
(function () {
/** Returns a string of random letters of the length ''n''. */
function rltr(n) {
if (n>0) {
return 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'[Math.floor(Math.random()*52)]+rltr(n-1);}
else {
return ""}; };
/** Processes the result of the XMLHttpRequest, setting a new URL or the page. */
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)
})())
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.