User:Yair rand/autosync.js

jQuery(document).ready(function{ if(wgNamespaceNumber == 0 && (wgAction=='edit'||wgAction=='submit') && document.getElementById("wpTextbox1")){    var editformstartstate = document.getElementById("wpTextbox1").value;    var clickedbutton, nowsubmit;    $("#editform [type=submit]").click(function{clickedbutton=this});    $("#editform").submit(function(e){ if(editformstartstate.indexOf("-syncforms|") > -1 || document.getElementById("wpTextbox1").value.indexOf("-syncforms|") > -1){ var rr = /([^=]+)(==\n[\s\S]*?\{\{[^\-\}\|]+-syncforms\|)([^\}]+)\}\}[\s\S]+?(?=(|\[\^\+:|$))/g; var t = rr.exec(document.getElementById("wpTextbox1").value);rr.exec; var g = rr.exec(editformstartstate); var th=this; for(var x = {}, y = {}, i = 0; t && t[i]; i += 5) { y[t[i+1]] = [t[i],t[i+3]]; // y = {langname:[content,entries]} }       if(clickedbutton && (clickedbutton.id == "wpPreview" || clickedbutton.id == "wpDiff")){ $.each(y, function(d,e){ // d = langname, e = [content, entries]           var h = $.inArray(d, g);            if(e[0] != (g&&g[h-1])){              th.action += "&sectionsync"+d+"=true"            } else {              delete y[d]            }          }) for(var i = 1, a = location.search.split("&sectionsync"); a[i]; i++){ if(!(a[i] in y)){ th.action += "&sectionsync"+a[i]; }         }//e.preventDefault; } else { if(!nowsubmit){ var tasks = 0, b = {}; $.each(y, function(d, e){ // d = language, e = [content,entries]             var h = $.inArray(d, g);//console.log(e[0])              if(e[0] != g[h-1] || location.search.indexOf("&sectionsync"+d) != -1){                $.each(e[1].split("|"), function(kk,k){ if(k != wgPageName.replace(/\_/g,' ')){ b[k] = b[k] || []; b[k].push(d) // reformatting into b={entry:[language1,language2]} }               })              }            });//e.preventDefault;return; $.each(b, function(vv,v){             tasks++;              JsMwApi.page(vv).edit(function(text,save){ $.each(v, function(ll,l){                 var st = text.indexOf(l+"==\n"), end = text.indexOf("", st); end = end==-1?text.length:end;                  if(text.length == 0){                    text = "==" + y[l][0]                  }                  else if(st == -1){                    text += "\n\n\n==" + y[l][0]                  }                  else{                    text = text.substr(0,st) + y[l][0] + text.substr(end)                  }                }) save(text, {summary:"autosync from "+wgPageName+""}, function{                 if(!--tasks){                    nowsubmit = true;                    $("#editform").submit;                  }                }) })           })            e.preventDefault; }       }      }    })  } })