User:ZBroz/common.js

// /** Returns the content of a parameter passed via URL. Does not * treat booleans in a dedicated manner, returning their string * content instead. If the parameter is not found, returns either * a default value (iDefaultValue) or an empty string. * Based on Wiktionary's MediaWiki:Edit.js. */ function getURLParameter(parameterName, /*optional*/ iDefaultValue) { var defaultValue = typeof iDefaultValue != 'undefined' ? iDefaultValue : "";

if (! (window.location.search && window.location.search.split("?")[1])) return defaultValue; try { urlParts=window.location.search.split("?")[1].split("&"); } catch (e) { return defaultValue; } //not an edit page for (var k = 0; k < urlParts.length; ++k) { pair=urlParts[k].split("=") if (pair[0]==parameterName) return decodeURIComponent(pair[1].replace(/\+/gi," ")); }   return defaultValue; }

/** Alerts the user by adding a new div to the center of the page rather than * via a pop up. */ function divAlert(message) { var div = document.createElement("div");

div.style.position = "fixed"; div.style.zIndex = 100; div.style.top = "40%"; div.style.left = "30%"; div.style.marginRight = "30%"; div.style.padding = "15pt"; div.style.backgroundColor = "brown"; div.style.color = "white"; div.style.fontSize = "15pt"; div.innerHTML = message; document.body.appendChild(div); }

/** Adds a translation or several translations in one languages to the page, * to the translation section with the heading containg the gloss. * There can be multiple translations, separated by semicolon. * A single translation element can contain gender indicated by a single letter, * separated from the translation using space, as in "jezevec m". Supported * genders are m, f, n, and c. There is no support for transliteration. */ function addTranslations(language, langCode, translationString, gloss, buttonToPress) {

/** Returns a pair of [translation, gender] given a string with a translation followed by a space and then by a single letter indicating the gender. If no gender is found, return a pair of [translation, ""]. */ function genderSplit(translationWithGender) { var translation=translationWithGender; genderMatches = translationWithGender.match(/^(.*) ([mfnc])$/); if (genderMatches!=null) return [genderMatches[1], genderMatches[2]]; else return [translation,""]; };

if (gloss=="" || translationString=="") { divAlert("The gloss or the translation is empty. No translation added."); return; } if (language=="" || langCode=="") { divAlert("The language or language code is missing. No translation added."); return; }

// Trim the translation string, split it by semicolon and create the new translation line var translations=translationString.replace(/^ +/g,"").replace(/ +$/g,"").split(/ *; */); var newTranslationLine = "* "+language+": "; for (i=0; i0) newTranslationLine+= ", "

genderSplitPair = genderSplit(translations[i]); translation=genderSplitPair[0]; gender=genderSplitPair[1]; newTranslationLine += ""+translation+(gender!=""?""; }  var content=document.getElementById("wpTextbox1").value;  var i = content.indexOf("{{trans-top|"+gloss,0);  if (i<0) {    divAlert("The gloss was not found on the page: \""+gloss+"\".  No translation added.");    //console.log("The gloss was not found on the page: \""+gloss+"\".  No translation added."); Does not seem to do anything.    return; }  var j = content.indexOf("{{trans-bottom}}",i);  var part1 = content.substring(0,i);  var part2 = content.substring(i,j);  var part3 = content.substring(j,content.length);  if (part2.indexOf(language,0)>=0) {    divAlert("A "+language+" translation is already present in the translation table with the gloss \""+gloss+"\". No translation added.")    return; }  // Break the part2 by newlines and insert the new translation at the right place.  var part2lines = part2.split("\n");  var newPart2 = ""; var newTranslationAdded=false; for (i=0; i0) newPart2 += "\n"; // Add the new translation if it was not yet added and if this is the right place. // Example of the presentation of a language on the line: "* Danish:", "* Aragonese:   var languageArray = part2lines[i].match(/^\* \[?\[?(.*?)\]?\]?:/);    if (languageArray!=null) {      var lineLanguage=languageArray[1];      if (!newTranslationAdded && (language < lineLanguage)) {        newPart2 += newTranslationLine+"\n";        newTranslationAdded = true; }}    newPart2 += part2lines[i]; }  if (!newTranslationAdded) { // Thus, the added language is the last translation to be added.    newPart2 += newTranslationLine+"\n";    newTranslationAdded = true; }

document.getElementById("wpTextbox1").value=part1+newPart2+part3; var summary = "/* translation gloss:"+gloss+" */ "; for (i=0; i0) summary+= "; " genderSplitPair = genderSplit(translations[i]); translation=genderSplitPair[0]; summary += "+"+langCode+":"+translation+""; } document.getElementById("wpSummary").value=summary; if (buttonToPress) document.getElementById(buttonToPress).click; // buttonToPress: "wpSave", "wpPreview", "wpDiff" }

function enableAddingTranslations { if (getURLParameter("task")=="addtrans") addTranslations(getURLParameter("language"), getURLParameter("langCode"),                   getURLParameter("translation"), getURLParameter("gloss"), getURLParameter("button")); }

$(enableAddingTranslations);

//-- // Ruakh's Tbot //-- //importScript('User:Ruakh/Tbot.js'); importScript('User:ZBroz/Tbot.js');

/* Tbot.getHeadLine['cs~adjective'] = Tbot.getHeadLine['cs~adverb'] = Tbot.getHeadLine['cs~noun'] = Tbot.getHeadLine['cs~proper noun'] = Tbot.getHeadLine['cs~verb'] = function (tbotData) { var ret = ''; return ret; };

$(function { Tbot.greenifyTranslinks('cs'); });