User:Decltype/autoeval.js

function autoevalTab { // mw.util.addPortletLink("p-cactions", "javascript:subster( getSelectedText )", "subster", "substtab-link", "subst"); if(mw.config.get('wgAction') == "edit") $(window).load(function { $("body").mouseup(autoeval); } ); } $(autoevalTab);

function autoeval { var textarea = document.getElementById("wpTextbox1"); if(!textarea) return; var start = textarea.selectionStart; var end = textarea.selectionEnd; var text = textarea.value.substring(start, end); //alert(text); var template = //.exec(text); if(!template) return; var qtext = ""; $.post(mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + mw.config.get('wgPageName') + "&action=submit&wpPreview=true&live=true&wpTextbox1=" + qtext,   function(data)     { var textarea = document.getElementById("wpTextbox1");      var pre = $("preview", data).text; alert(pre);      var repl = / [^]*<\/div>[^]* ([^]+)<\/p>/.exec(pre)[1];

//     alert (text + " " + repl); textarea.value = textarea.value.replace(text, repl); textarea.setSelectionRange(start, end + repl.length - text.length); } ); }