User:Anomie/ajaxpreview.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
/* If you want to use this script, simply add the following line to your monobook.js:
 
importScript('User:Anomie/ajaxpreview.js'); // Linkback: [[User:Anomie/ajaxpreview.js]]
 
* (Please keep the comment so I can see how many people use this).
*/
 
importScript('User:Anomie/util.js');
 
var AJAXPreview={
    node:null,
    txt:null,
    timer:null,
    idx:0,
 
    spinner:function(){
        switch(AJAXPreview.idx++){
          case 0:
            AJAXPreview.node.innerHTML='<center style="font-size:50pt">|</center>';
            break;
          case 1:
            AJAXPreview.node.innerHTML='<center style="font-size:50pt">/</center>';
            break;
          case 2:
            AJAXPreview.node.innerHTML='<center style="font-size:50pt">–</center>';
            break;
          case 3:
            AJAXPreview.node.innerHTML='<center style="font-size:50pt">\\</center>';
            AJAXPreview.idx=0;
            break;
        }
    },
 
    callback:function(r){
        if(AJAXPreview.timer) window.clearInterval(AJAXPreview.timer);
        AJAXPreview.timer=null;
        if(!r.parse || !r.parse.text || !r.parse.text['*']){
            AJAXPreview.node.innerHTML='<div style="border:1px solid #f00;background-color:#fcc;color:#f00;text-align:center">Bad response</div>';
            throw new Error('Bad response');
        }
        AJAXPreview.node.innerHTML=r.parse.text['*']+'<br />';
 
        // Set a timeout to allow the browser a chance to parse the innerHTML
        if(window.LinkClassifier) window.setTimeout(function(){ LinkClassifier.classifyChildren(AJAXPreview.node); }, 250);
    },
 
    doPreview:function(ev){
        if(!ev) ev=window.event;
        var txt='__NOEDITSECTION__\n'+AJAXPreview.txt.value;
        if(txt.indexOf('<ref')>=0 && txt.indexOf('<references')<0 && txt.indexOf('{{reflist')<0){
            txt+='\n<hr />{{reflist|2}}';
 
            var r=/<ref\s+[^>]*name="([^\x22]+)"[^>\/]*\/>/ig;
            if(r.test(txt)){
                var wikitext=rawpage(wgPageName);
                if(wikitext) txt=txt.replace(r, function(x,m){
                    var m2=wikitext.match(new RegExp('<[rR][eE][fF]\\s+[^>]*[nN][aA][mM][eE]="'+RegExp.quote(m)+'"[^>/]*>.*?</[rR][eE][fF]>'));
                    return m2?m2[0]:x;
                });
            }
        }
 
        var x=document.getElementById('wikiDiff');
        if(x) x.parentNode.removeChild(x);
        if(AJAXPreview.timer) window.clearInterval(AJAXPreview.timer);
        AJAXPreview.timer=window.setInterval(AJAXPreview.spinner, 250);
        if(!api({action:'parse',text:txt,title:wgPageName,prop:'text'}, AJAXPreview.callback, true))
            return true;
 
        window.scrollTo(0,0);
        if(ev.preventDefault) ev.preventDefault();
        if(ev.stopPropagation) ev.stopPropagation();
        ev.returnValue=false;
        ev.cancelBubble=true;
        return false;
    },
 
    onLoad:function(){
        if(wgAction!='edit' && wgAction!='submit') return;
 
        var editForm=document.getElementById('editform');
        if(!editForm) return;
        var sectionField = editForm.elements['wpSection'];
        var isSection=(sectionField && sectionField.value!="");
 
        AJAXPreview.node=document.getElementById('wikiPreview');
        if(!AJAXPreview.node) return;
        AJAXPreview.txt=editForm.elements["wpTextbox1"];
        if(!AJAXPreview.txt) return;
 
        var p=editForm.elements["wpPreview"];
        var b=document.createElement('INPUT');
        b.type='button';
        b.value='Ajax Preview'+(isSection?' w/Refs':'');
        b.tabIndex=p.tabIndex;
        b.onclick=AJAXPreview.doPreview;
        p.parentNode.insertBefore(b,p);
        p.parentNode.insertBefore(document.createTextNode(' '),p);
        p.value='Preview';
    }
};
 
if(doneOnloadHook) AJAXPreview.onLoad(); //if imported dynamically
else addOnloadHook(AJAXPreview.onLoad);