MediaWiki:Gadget-SecondWindowPreview.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
(function () {
    if (window.SecondWindowPreview) {
        return;
    }

    if (mw.config.get('wgNamespaceNumber') < 0) {
        return;
    }
    if (mw.config.get('wgAction') !== 'submit' && mw.config.get('wgAction') !== 'edit') {
        return;
    }
    if (mw.config.get('wgPageContentModel') === 'javascript' || mw.config.get('wgPageContentModel') === 'css') {
        return;
    }

    var SecondWindowPreview = new Object();
    window.SecondWindowPreview = SecondWindowPreview; // sert à la vérification anti-double inclusion au début du code

    SecondWindowPreview.Param = new Object();

    //////////////////// PARTIE PERSONNALISABLE /////////////////////////

    SecondWindowPreview.Param.PreviewTimeout = 2;

    //////////////////// FIN DE LA PARTIE PERSONNALISABLE /////////////////////////

    SecondWindowPreview.Variable = new Object();
    SecondWindowPreview.Variable.NewWindowPreview = null;
    SecondWindowPreview.Variable.NewWindowPreviewBody = null;
    SecondWindowPreview.Variable.Wikitext = "";
    SecondWindowPreview.Variable.KeyboardTimeout = null;

    SecondWindowPreview.Function = new Object();

    SecondWindowPreview.Function.ajax = {
        http:function(bundle){ var xmlhttp; try{ xmlhttp = new XMLHttpRequest(); }catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ xmlhttp = false; }}}; if(xmlhttp){ xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4){ SecondWindowPreview.Function.ajax.httpComplete(xmlhttp,bundle);}}; xmlhttp.open(bundle.method ? bundle.method : "GET",bundle.url,bundle.async == false ? false : true); if (bundle.headers) { for (var field in bundle.headers){ try{ xmlhttp.setRequestHeader(field,bundle.headers[field]); }catch(err){}}}; xmlhttp.send(bundle.data ? bundle.data : null); }; return xmlhttp;},
        httpComplete: function(xmlhttp,bundle){ if(xmlhttp.status == 200 || xmlhttp.status == 302){ if(bundle.onSuccess) bundle.onSuccess(xmlhttp,bundle); }else if(bundle.onFailure){ bundle.onFailure(xmlhttp,bundle); }else{ }}
    };

    SecondWindowPreview.Function.AddHiddenCatsLink = function(cl){
        if( !(hc = SecondWindowPreview.Function.getElementWithId(cl,'mw-hidden-catlinks')) ) return;
        if($(hc).hasClass('mw-hidden-cats-user-shown')) return;
        if($(hc).hasClass('mw-hidden-cats-ns-shown')) $(hc).addClass('mw-hidden-cats-hidden');
        var nc = SecondWindowPreview.Function.getElementWithId(cl, 'mw-normal-catlinks');
        if(!nc) {
            var catline = document.createElement('div');
            catline.id = 'mw-normal-catlinks';
            var a = document.createElement('a');
            a.href = '/wiki/Catégorie:Accueil';
            a.title = 'Catégorie:Accueil';
            a.appendChild(document.createTextNode('Catégories'));
            catline.appendChild(a);
            catline.appendChild(document.createTextNode(' : '));
            nc = cl.insertBefore(catline, cl.firstChild);
        }
        else nc.appendChild(document.createTextNode(' | '));
        var lnk = document.createElement('a');
        lnk.id = 'mw-hidden-cats-link';
        lnk.title = 'Cet article contient des catégories cachées';
        lnk.style.cursor = 'pointer';
        lnk.style.color = 'black';
        lnk.onclick = SecondWindowPreview.Function.toggleHiddenCats;
        lnk.appendChild(document.createTextNode('[+]'));
        nc.appendChild(lnk);
    }

    SecondWindowPreview.Function.toggleHiddenCats = function(){
        if( $(hc).hasClass('mw-hidden-cats-hidden') ) {
            $(hc).removeClass('mw-hidden-cats-hidden');
            $(hc).addClass('mw-hidden-cat-user-shown');
            hclink.innerHTML = '[–]';
        } else {
            $(hc).removeClass('mw-hidden-cat-user-shown');
            $(hc).addClass('mw-hidden-cats-hidden');
            hclink.innerHTML = '[+]';
        }
    }

    SecondWindowPreview.Function.getFormParams = function(Form){
        var Params = new Array();
        var Tags = new Array("textarea", "select", "input");
        for(var a=0,l=Tags.length;a<l;a++){
            var Elements = Form.getElementsByTagName(Tags[a]);
            for(var b=0,m=Elements.length;b<m;b++){
                var Element = Elements[b];
                var ElName = Element.name;
                if(!ElName) continue;
                var ElValue = Element.value;
                var ElType = Element.type;
                if(Tags[a].toLowerCase()=='input' && (ElType == "checkbox" || ElType == "radio") && Element.checked){
                    Params[ElName] = ElValue;
                }else if(Tags[a].toLowerCase()=='input' && (ElType == "text" || ElType == "hidden") ){
                    Params[ElName] = ElValue;
                }else if(Tags[a].toLowerCase()!='input'){
                    Params[ElName] = ElValue;
                }
            }
        }
        return Params;
    }

    SecondWindowPreview.Function.UpdatePreview = function(){
        var Textarea = document.getElementById("wpTextbox1");
        if(!Textarea) return;
        SecondWindowPreview.Variable.Wikitext = Textarea.value;
        var Form = document.editform;
        var Params = SecondWindowPreview.Function.getFormParams(Form);
        var ParamsInURL = new Array();
        for(var P in Params){
            if(typeof(Params[P])=="string") ParamsInURL.push(P+"="+encodeURIComponent(Params[P]));
        }
        ParamsInURL  = ParamsInURL.join("&")+"&wpPreview=1";
        var action = Form.action;
        var headers = new Array();
        headers['Content-Type'] = 'application/x-www-form-urlencoded';
        SecondWindowPreview.Function.ajax.http({ url: action,
                      method: "POST",
                      headers: headers,
                      data: ParamsInURL,
                      onSuccess:SecondWindowPreview.Function.UpdatePreviewReplace
        });
    }

    SecondWindowPreview.Function.UpdatePreviewReplace = function(Req, data){
        var OldTemp = document.getElementById("TempDiv");
        if(OldTemp) OldTemp.parentNode.removeChild(OldTemp);
        var Temp = document.createElement('div');
        Temp.id = "TempDiv";
        Temp.style.display = "none";
        document.body.appendChild(Temp);
        Temp.innerHTML = Req.responseText;
        var WindowBody = SecondWindowPreview.Variable.NewWindowPreview.document.getElementsByTagName("body")[0];
        if(WindowBody){
            SecondWindowPreview.Function.Fullscreen(WindowBody);
            var Preview = SecondWindowPreview.Function.getElementWithId(WindowBody,"wikiPreview");
            var NewPreview  = SecondWindowPreview.Function.getElementWithId(Temp, "wikiPreview");
            if(Preview && NewPreview){
                NewPreview.parentNode.removeChild(NewPreview);
                Preview.parentNode.insertBefore(NewPreview, Preview);
                Preview.parentNode.removeChild(Preview);
            }
            var Cats = SecondWindowPreview.Function.getElementWithId(WindowBody,"catlinks");
            var NewCats  = SecondWindowPreview.Function.getElementWithId(Temp, "catlinks");
            if(Cats && NewCats){
                NewCats.parentNode.removeChild(NewCats);
                Cats.parentNode.insertBefore(NewCats, Cats);
                Cats.parentNode.removeChild(Cats);
                SecondWindowPreview.Function.AddHiddenCatsLink(NewCats);
            }
        }
        Temp.parentNode.removeChild(Temp);
    }

    SecondWindowPreview.Function.SetTimeOut = function(forcepreview){
        var Textarea = document.getElementById("wpTextbox1");
        if(!Textarea || (SecondWindowPreview.Variable.Wikitext == Textarea.value && !forcepreview)) return;
        SecondWindowPreview.Variable.KeyboardTimeout = clearTimeout(SecondWindowPreview.Variable.KeyboardTimeout);
        SecondWindowPreview.Variable.KeyboardTimeout = setTimeout("SecondWindowPreview.Function.UpdatePreview();", SecondWindowPreview.Param.PreviewTimeout * 1000);
    }

    SecondWindowPreview.Function.getElementWithId = function(node, id){
        return node.querySelector('#'+id);
    }

    SecondWindowPreview.Function.InitWindow = function(){
        var OK = false;
        var WindowBody = SecondWindowPreview.Variable.NewWindowPreview.document.getElementsByTagName("body")[0];
        if(WindowBody){
            var Content = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw-content-text");
            var Preview = document.getElementById("wikiPreview");
            var Textarea = document.getElementById("wpTextbox1");
            if(Content && Preview && Textarea){
                while(Content.firstChild){ Content.removeChild(Content.firstChild); };
                Preview.parentNode.removeChild(Preview);
                Content.appendChild(Preview);
                SecondWindowPreview.Function.Fullscreen(WindowBody);
                SecondWindowPreview.Variable.Wikitext = Textarea.value;
                Textarea.onkeyup = function(){ SecondWindowPreview.Function.SetTimeOut(false); }
                if(mw.config.get('wgAction')=="edit") SecondWindowPreview.Function.SetTimeOut(true);
                OK = true;
            }
        }
        if(!OK) setTimeout("SecondWindowPreview.Function.InitWindow();", 500);
    }


    SecondWindowPreview.Function.Fullscreen = function(WindowBody) {
        if(mw.config.get('skin') =="monobook"){
            var fullLink1 = SecondWindowPreview.Function.getElementWithId(WindowBody,"column-one");
            if(!fullLink1){
                var fullLink1 = SecondWindowPreview.Function.getElementWithId(WindowBody,"sidebox");
            }
            fullLink1.setAttribute( "style","display:none");
            var fullLink2 = SecondWindowPreview.Function.getElementWithId(WindowBody,"footer");
            fullLink2.setAttribute( "style","display:none");
            var fullLink3 = SecondWindowPreview.Function.getElementWithId(WindowBody,"p-cactions");
            fullLink3.setAttribute( "style","display:none");
            var fullLink4 = SecondWindowPreview.Function.getElementWithId(WindowBody,"p-personal");
            fullLink4.setAttribute( "style","display:none");
            var fullLink5 = SecondWindowPreview.Function.getElementWithId(WindowBody,"column-content");
            fullLink5.setAttribute( "style","margin: 0 0 0 0");
            var fullLink6 = SecondWindowPreview.Function.getElementWithId(WindowBody,"content");
            fullLink6.setAttribute( "style","margin: 0 0 0 0");
        }
        if(mw.config.get('skin') == "vector"){
            var fullLink1 = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw-head");
            fullLink1.setAttribute( "style","display:none");
            var fullLink2 = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw-panel");
            fullLink2.setAttribute( "style","display:none");
            var fullLink3 = SecondWindowPreview.Function.getElementWithId(WindowBody,"footer");
            fullLink3.setAttribute( "style","display:none");
            var fullLink4 = SecondWindowPreview.Function.getElementWithId(WindowBody,"content");
            fullLink4.setAttribute( "style","margin: 0 0 0 0");
            var fullLink5 = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw-page-base");
            fullLink5.setAttribute( "style","height:0em");
        }
        if(mw.config.get('skin') == "modern"){
            var fullLink1 = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw_portlets");
            fullLink1.setAttribute( "style","display:none");
            var fullLink2 = SecondWindowPreview.Function.getElementWithId(WindowBody,"p-cactions");
            fullLink2.setAttribute( "style","display:none");
            var fullLink3 = SecondWindowPreview.Function.getElementWithId(WindowBody,"p-personal");
            fullLink3.setAttribute( "style","display:none");
            var fullLink4 = SecondWindowPreview.Function.getElementWithId(WindowBody,"footer");
            fullLink4.setAttribute( "style","display:none");
            var fullLink5 = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw_contentwrapper");
            fullLink5.setAttribute( "style","margin-left: 0em ");
            var fullLink6 = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw_content");
            fullLink6.setAttribute( "style","margin: 0 0 0 0");
        }
        var Content = SecondWindowPreview.Function.getElementWithId(WindowBody,"mw-content-text");
        if(Content){
          while(Content.previousSibling){Content.parentNode.removeChild(Content.previousSibling); };
        }
        var Title = SecondWindowPreview.Function.getElementWithId(WindowBody,"firstHeading");
        if(Title){
          while(Title.previousSibling){Title.parentNode.removeChild(Title.previousSibling); };
          Title.parentNode.removeChild(Title);
        }
        var Portails = SecondWindowPreview.Function.getElementWithId(WindowBody,"bandeau-portail");
        if(Portails) Portails.parentNode.removeChild(Portails);
    }

    SecondWindowPreview.Function.Init = function(){
        if(SecondWindowPreview.Variable.NewWindowPreview!=null) return; // la fenêtre existe déjà (fonction Init() déjà appelée par ailleurs ?)
        var URL = document.URL.split("action=edit").join("action=view").split("action=submit").join("action=view");
        SecondWindowPreview.Variable.NewWindowPreview = window.open(URL,"NewWindowPreview","menubar=no,titlebar=no,toolbar=no,location=no,scrollbars=yes,height=250,width=500",false);
        if(SecondWindowPreview.Variable.NewWindowPreview==null) return; // l'ouverture de la fenêtre a échoué (pop-up blocker)
        document.body.onunload = function(){
            try{ SecondWindowPreview.Variable.NewWindowPreview.close()}catch(e){ }
        }
        SecondWindowPreview.Function.InitWindow();
    }


    $(SecondWindowPreview.Function.Init);

})();