User:Lupin/autoedit.js

function getCmd { var cmdRe=RegExp('[&?]autoedit=([^&]*)'); var h=document.location; var m; if (m=cmdRe.exec(h)) { try { return decodeURI(m[1]); } catch (someError) {} } return null; };

function substitute(data,cmdBody) { var fromRe=RegExp(cmdBody.from, cmdBody.flags); return data.replace(fromRe, cmdBody.to); };

function execCmds(data, cmdList) { for (var i=0; i<cmdList.length; ++i) { data=cmdList[i].action(data, cmdList[i]); } return data; }

function parseCmd(str) { // returns a list of commands if (!str.length) return []; switch (str.charAt(0)) { case 's': var p=parseSubstitute(str); if (p) { return [p].concat(parseCmd(p.remainder)); }   return false; default: return false; } };

function unEscape(str, sep) { return str.split('\\\\').join('\\').split('\\'+sep).join(sep); }

function parseSubstitute(str) { // takes a string like s/a/b/flags;othercmds and parses it if (str.length<4) return false; var sep=str.charAt(1); str=str.substring(2); var endFrom=findNext(str, sep); if (endFrom<0) return false; var from=unEscape(str.substring(0,endFrom), sep); //document.write('from='+from); str=str.substring(endFrom+1);

var endTo=findNext(str, sep); if (endTo<0) return false; var to=unEscape(str.substring(0,endTo), sep); //document.write(' to='+to); str=str.substring(endTo+1);

var flags=''; if (str.length) { var endFlags=findNext(str, ';'); if (endFlags<0) endFlags=str.length; var flags=str.substring(0,endFlags).split('\\'+sep).join(sep); str=str.substring(endFlags+1); } return {action: substitute, from: from, to: to, flags: flags, remainder: str};

};

function findNext(str, ch) { for (var i=0; i<str.length; ++i) { if (str.charAt(i)=='\\') i+=2; if (str.charAt(i)==ch) return i; } return -1; };

function runOnLoad(f) { if (window.addEventListener) { window.addEventListener("load",f,false); } else if (window.attachEvent) { window.attachEvent("onload",f); } else { window._old_popup_autoedit_onload = window.onload; window.onload = function { window._old_popup_autoedit_onload; f; } } };

function autoEdit { var cmdString=getCmd; if (!cmdString) return; try { var editbox=document.editform.wpTextbox1; } catch (dang) { return; } var cmdList=parseCmd(cmdString); var input=editbox.value; var output=execCmds(input, cmdList); editbox.value=output; };

runOnLoad(autoEdit);