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.
/* Esperanto conversion script to emulate mediawiki's esperanto x-coding. [Version 0.2.0]
http://svn.wikimedia.org/viewvc/mediawiki/trunk/phase3/languages/classes/LanguageEo.php?view=co&content-type=text%2Fplain
*/

if(wgAction == 'edit' || wgAction == 'submit') {
  var eoTextbox = 'wpTextbox1';
} else if(wgCanonicalSpecialPageName == 'Upload') {
  var eoTextbox = 'wpUploadDescription';
}
if(window.eoTextbox) addOnloadHook(unconvertEsperanto)

function unconvertEsperanto() {
  //add handlers to convert just before save/preview/upload
  var eoBtns = ['wpSave','wpPreview','wpDiff','wpUpload'];
  for(var i=0;i<eoBtns.length;i++) {
    var btn = getElementByIdOrName(eoBtns[i]);
    if(btn) addClickHandler(btn,convertEsperanto)
  }

  var token = getElementByIdOrName('wpEditToken');
  if(token.value.indexOf('!eo!') == -1) { 
    token.value += '!eo!'
  } else {
    return;
  }

  var num = 0;
  var obj = document.getElementById(eoTextbox);
  var str = obj.value;

  var pattern = /[cghjsu][x]+/i;
  if(str.match(pattern)) {
    while(str.match(pattern)) {
      var eold = str.match(pattern)[0];
      var enew = eold.substr(0,1) + '__!__';
      eold = eold.substr(1);
      while(eold.length > 0) {
        enew += eold.substr(0,1) + 'x';
        eold = eold.substr(1);
      }
      str = str.replace(str.match(pattern),enew);
      num++;
    }    
  }

  var pattern2 = /[\u0109\u011D\u0125\u0135\u015D\u016D\u0108\u011C\u0124\u0134\u015C\u016C][x]*/i;
  var conv2 = {'\u0109':'c', '\u011D':'g', '\u0125':'h', '\u0135':'j', '\u015D':'s', '\u016D':'u', '\u0108':'C', '\u011C':'G', '\u0124':'H', '\u0134':'J', '\u015C':'S', '\u016C':'U'}
  if(str.match(pattern2)) {
    while(str.match(pattern2)) {
      var eold = str.match(pattern2)[0];
      var enew = conv2[eold.substr(0,1)] + '__!__x';
      eold = eold.substr(1);
      while(eold.length > 0) {
        enew += eold.substr(0,1) + eold.substr(0,1).toLowerCase();
        eold = eold.substr(1);
      }
      str = str.replace(str.match(pattern2),enew);
      num++;
    }
  }

  if(num > 0) obj.value = str.replace(/__!__/ig,'')
}

function convertEsperanto() {
  var token = getElementByIdOrName('wpEditToken');
  if(token.value.indexOf('!eo!') != -1) {
    token.value = token.value.replace(/!eo!/,'');
  } else {
    return;
  }

  var num = 0;
  var obj = document.getElementById(eoTextbox);
  var str = obj.value;
  var pattern = /[cghjsu][x]+/i;
  var conv = {'c':'\u0109', 'g':'\u011D', 'h':'\u0125', 'j':'\u0135', 's':'\u015D', 'u':'\u016D', 'C':'\u0108', 'G':'\u011C', 'H':'\u0124', 'J':'\u0134', 'S':'\u015C', 'U':'\u016C'}
  if(str.match(pattern)) {
    while(str.match(pattern)) {
      var eold = str.match(pattern)[0];
      var efst = eold.substr(0,1);
      if(eold.length % 2 == 0) {
        var enew = conv[efst] + '__!__';
        eold = eold.substr(2);
      } else {
        var enew = efst + '__!__';
        eold = eold.substr(1);
      }
      while(eold.length > 0) {
        enew += eold.substr(0,1);
        eold = eold.substr(2);
      }
      str = str.replace(str.match(pattern),enew);
      num++;
    }
  }

  if(num > 0) obj.value = str.replace(/__!__/ig,'')
}

//Needed because the upload form submit has no ID
function getElementByIdOrName(idorname,par) {
  var parent = (!par) ? document : par
  if(parent.getElementById(idorname)) {
    return parent.getElementById(idorname);
  } else if(parent.getElementsByName(idorname)[0]) {
    return parent.getElementsByName(idorname)[0];
  } else {
    return false;
  }
}