User:Hippietrail/perlanguage.js

function WiktApiProxy { // helper functions function mwEncodeURIComponent(term) { return encodeURIComponent(term).replace(/%2F/g, "/"); }

function add_dice(h2, lang) { var a = document.createElement('a'); a.title = "Random " + lang + " entry."; a.href = "http://toolserver.org/~hippietrail/randompage.fcgi?langname=" + lang; var img = document.createElement('img'); img.alt = "random"; img.height="16"; img.width="24" img.src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/36/Two_red_dice_01.svg/32px-Two_red_dice_01.svg"; a.appendChild(img); h2.appendChild(document.createTextNode(" ")); h2.appendChild(a); }

function add_interlang(h2, code, term) { var sup = document.createElement('sup'); sup.style.fontSize = '66%'; sup.style.fontWeight = "normal"; var a = document.createElement('a'); var href = "http://" + code + ".wiktionary.org/wiki/"; href += mwEncodeURIComponent(term); a.href = href; a.appendChild(document.createTextNode("☞"+code)); sup.appendChild(a); h2.appendChild(document.createTextNode(" ")); h2.appendChild(sup); }

// toolserver has sent back results. add them to the page this.gotproxyresults = function(stuff) { // we will always give up if there's an error in the json response if ("error" in stuff) return;

// links under the language headings

var bc = document.getElementById("bodyContent"); var h2s = bc.getElementsByTagName("h2");

outer: for (var i = 0; i < h2s.length; ++i) { var spans = h2s[i].getElementsByTagName("span"); inner: for (var j = 0; j < spans.length; ++j) { if (spans[j].className == "mw-headline") { var langname = spans[j].textContent || spans[j].innerText;

// find a code for this lang if (typeof this.langcodesgotten[langname] == "object") { for (var k = 0; k < this.langcodesgotten[langname][langname].length; ++k) { var code = this.langcodesgotten[langname][langname][k]; if (code in stuff.results && stuff.results[code] != -1) { add_interlang(h2s[i], code, wgTitle); }                       }                    }                }            }        }    }

this.gotlangcodes = function(stuff) { // we will always give up if there's an error in the json response if ("error" in stuff) return;

this.langcodesgotten = stuff;

var codes = "";

for (lang in stuff) { if (codes) codes += ","; codes += stuff[lang][lang].join(","); }

if (codes) { mw.loader.load(               "http://toolserver.org/~hippietrail/apiproxy.fcgi?"                + "term=" + wgTitle + "&langs=" + codes                + "&callback=wiktApiProxy.gotproxyresults"); }   }

this.apiproxy = function { var bc = document.getElementById("bodyContent"); var langnames = "";

// article = page = entries if (wgNamespaceNumber == 0) { var h2s = bc.getElementsByTagName("h2");

outer: for (var i = 0; i < h2s.length; ++i) { var spans = h2s[i].getElementsByTagName("span"); inner: for (var j = 0; j < spans.length; ++j) { if (spans[j].className == "mw-headline") { var langname = spans[j].textContent || spans[j].innerText; if (langname != "English") { if (langnames) langnames += "|"; langnames += langname; }                       // add random dice link add_dice(h2s[i], langname);

continue outer; }               }            }            getlangcodes(langnames); }

function getlangcodes(langnames) { mw.loader.load(               "http://toolserver.org/~hippietrail/getlangcodes.fcgi?"                + "langs=" + langnames                + "&callback=wiktApiProxy.gotlangcodes"); }   } }

var wiktApiProxy;

$(function {    wiktApiProxy = new WiktApiProxy;    wiktApiProxy.apiproxy; })