User:Yair rand/editsection.js

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • 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.

mw.config.get('wgNamespaceNumber')==0 && jQuery(document).ready(function(){
 for(var a=jQuery("h3 .mw-editsection a,h4 .mw-editsection a,h5 .mw-editsection a"), i=0; i < a.length; i++){
  b(a[i])
  }
 function b(c){
  c.onclick=function(){
   function fh(w_,q){
    var ll=w_.search(/(^|\n)=/)+1, l=0;
    while(++l < q){
     ll = w_.indexOf("\n=", ll+1)
     }
    return w_.indexOf("=\n", ll+1)+2
    }
   var q = c.href.split("&section=")[1], qq = c.parentNode.parentNode, backup = document.createDocumentFragment(), theform, ll;
   while(qq.nextSibling && qq.nextSibling.nodeName.substr(0,1).toLowerCase() != 'h' && qq.nextSibling.className != "catlinks"){
    backup.appendChild(qq.nextSibling)
    }
   editor=new Editor();
   editor.withCurrentText(function(wikitext){
    new AdderWrapper(editor, {
     'createForm':function(){
      var t, ll = fh(wikitext, q), wt=wikitext.substr(ll);
      wt=wt.substr(0, wt.indexOf("\n=")==-1?undefined:wt.indexOf("\n="));
      return theform = newNode('form',
       newNode('textarea',wt,{'name':'wt','rows':wt.match(/\n/g).length}),
       newNode('input',{'type':'submit','value':'Preview'}),
       newNode('input',{'type':'button','value':'Cancel','click':function(){
        theform.parentNode.insertBefore(backup,theform);theform.parentNode.removeChild(theform)}}))
      },
     'fields':{'wt':function(txt){return txt}},
     'onsubmit':function(values,render){
      render(values.wt,function(newhtml){
       var newp = newNode('p');newp.innerHTML=newhtml;
       editor.addEdit({
        'edit':function(wikitext){
         var ll=fh(wikitext,q), wt=wikitext.substr(ll);wt=wt.indexOf("\n=");
         return ccc=wikitext.substr(0,ll)+values.wt.replace(/\n=/g,'\n{\{subst:blank}}=')+(wt==-1?"":wikitext.substr(ll+wt))},
        'redo':function(){qq.parentNode.insertBefore(newp, theform);theform.style.display='none'},
        'undo':function(){qq.parentNode.removeChild(newp);theform.style.display='block'},
        'summary':'edit section '+qq.getElementsByTagName("span")[1].innerHTML
        },newp)
       })
      }
     },qq.parentNode, qq.nextSibling)
     theform.firstChild.focus()
    })
   return false;
   }
  }
 })