Utilisateur:Dr Brains/TestExist.js

Une page de Wikipédia, l'encyclopédie libre.
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.
var A = new XMLHttpRequest();
A.onreadystatechange = function(e){
    if (!e) var e = window.event;
    if (this.readyState == 4){
        if (this.status == 200){
            var docXML = this.responseXML;
            var texteTitre = document.getElementById('textIfExist')
            if (docXML.getElementsByTagName('page')[0].getAttribute('missing') == ''){
                texteTitre.style.color='red';
                texteTitre.style.fontWeight='normal';
                texteTitre.style.fontStyle='normal';
                texteTitre.title="Cette page n'existe pas";
            }else{
                document.getElementById('textIfExist').style.color='green'; 
                if (docXML.getElementsByTagName('redirects').length > 0){
                    texteTitre.style.fontStyle='italic';
                    texteTitre.style.fontWeight='normal';
                    texteTitre.title='Cette page est une redirection';
                    if (e.keyCode == 13) {
                        texteTitre.value = docXML.getElementsByTagName('r')[0].to;
                        A.open(mw.config.get('wgServer') + '/w/api.php?action=query&format=xml&redirects&titles=',texteTitre.value);
                        A.send(null);
                    }
                }else{
                    texteTitre.style.color='blue';
                    texteTitre.style.fontStyle='normal';
                    texteTitre.style.fontWeight='bold';
                    texteTitre.title='Cette page existe';
                }
            }
        }
    }
}

function keyPress(e){
    if (!e) var e = window.event;
    if (this.value != '' && e.keyCode == 13){
        A.open('GET', mw.config.get('wgServer')+'/w/api.php?action=query&format=xml&redirects&titles='+document.getElementById('textIfExist').value, true);
        A.send(null);
    }
}

var TestExist_DefaultValue = 'Page existe ?';

function a(){
    if((skin == "monobook")||(skin == "modern")||(skin == "chick")||(skin == "myskin")||(skin == "simple")){
         Conteneur = document.getElementById('searchform');
         StyleInput = "width:80%; font-size:90%; font-weight:normal; color:grey; margin-top:0.3em;";
    }else if(skin == "vector"){
         Conteneur = document.getElementById('searchform');
         StyleInput = "display:inline !important; font-size:90%; font-weight:normal; color:grey;";
    } else {
         Conteneur = document.getElementById('searchform2');
         if(!Conteneur)Conteneur = document.getElementById('searchform');
         StyleInput = "width:80%; font-size:90%; font-weight:normal; color:grey; margin-top:0.3em;";
    }
    Conteneur.setAttribute("style", "text-align:center");
    Conteneur.innerHTML += '<input id="textIfExist" title="Rouge = article inexistant ; Vert italique = redirection ; Bleu gras = article existant." '
    + 'type="text" style="'+ StyleInput +'" value="'+TestExist_DefaultValue+'" '
    + 'onfocus="if(this.value==\''+TestExist_DefaultValue+'\') this.value=\'\';" '
    + 'onblur="if(this.value==\'\') {this.value=\''+TestExist_DefaultValue+'\'; this.style.color = \'grey\'; this.style.fontWeight = \'normal\'; this.style.fontStyle = \'normal\';}"/>';
    document.getElementById('textIfExist').onkeypress = keyPress;
}
$(a);