User:Yair rand/autosync.js
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
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 += "§ionsync"+d+"=true"
} else {
delete y[d]
}
})
for(var i = 1, a = location.search.split("§ionsync"); a[i]; i++){
if(!(a[i] in y)){
th.action += "§ionsync"+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("§ionsync"+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();
}
}
}
})
}
})