Utilisateur:Dr Brains/NewCatInputbox.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.
// {{Projet:JavaScript/Script}} 
//<source lang=javascript>//<pre><nowiki>
if(mw.config.get('wgNamespaceNumber')==14){
     var NewCatInputbox_DefaultText = "Enter le nom de la page à créer dans cette catégorie";
     var NewCatInputbox_SubmitText = "Créer";
     if(mw.config.get('wgAction')=="view"||mw.config.get('wgAction')=="purge") addOnloadHook(NewCatInputbox_AddInputBox);
}
if(mw.config.get('wgAction')=="edit") addOnloadHook(NewCatInputbox_AddCat);

function NewCatInputbox_AddInputBox(){
     var Titre = document.getElementById("firstHeading");
     if(!Titre) Titre = document.getElementsByTagName('h1')[0]
     if(!Titre) return;

     var Form = document.createElement('form');
     Form.setAttribute("style", "float:right;display:inline;");
     Form.action = "javascript:NewCatInputbox_CheckInputBox();";

     var Input = document.createElement('input');
     Input.type = "text"
     Input.id = "NewCatInputbox_CatName";
     Input.size = 50;
     Input.title = NewCatInputbox_DefaultText;
     Input.value = NewCatInputbox_DefaultText;
     Input.onfocus = NewCatInputbox_OnFocus;
     Input.onblur = NewCatInputbox_OnBlur;
     Input.style.color = "grey";

     var Button = document.createElement('input');
     Button.type = "button"
     Button.value = NewCatInputbox_SubmitText;
     Button.onselect = NewCatInputbox_CheckInputBox;
     Button.onclick = NewCatInputbox_CheckInputBox;

     Form.appendChild(Input);
     Form.appendChild(Button);
     Titre.appendChild(Form);

}

function NewCatInputbox_OnFocus(){
     var Input = document.getElementById("NewCatInputbox_CatName");
     if(Input.value==NewCatInputbox_DefaultText) Input.value="";
     Input.style.color = "black";
}

function NewCatInputbox_OnBlur(){
     var Input = document.getElementById("NewCatInputbox_CatName");
     if(Input.value=="") Input.value=NewCatInputbox_DefaultText;
     if(Input.value==NewCatInputbox_DefaultText) Input.style.color = "grey";
}

function NewCatInputbox_CheckInputBox(){
     var Input = document.getElementById("NewCatInputbox_CatName");
     //alert(Input.value);
     if(Input.value==NewCatInputbox_DefaultText) return;
     if(Input.value==""){ 
          NewCatInputbox_OnBlur();
          return;
     }
     var NewPage = Input.value;
     var URL = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + NewPage.replace(/&/g, "%26") + '&action=edit&redlink=1&NewCatInputbox='+encodeURIComponent(mw.config.get('wgTitle'));
     document.location = URL;
}

function NewCatInputbox_AddCat(){
     var URL = decodeURIComponent(document.URL);
     if(URL.indexOf("&NewCatInputbox=")==-1) return;
     var MotherCat = URL.split("&NewCatInputbox=")[1];
     var Page = mw.config.get('wgPageName').replace(/_/g, " ")
     //alert("Page : "+ Page+"\nCatégorie : "+MotherCat);

     var TextArea = document.getElementById("wpTextbox1");
     var OldText = TextArea.value;
     if(OldText.indexOf("[["+mw.config.get('wgFormattedNamespaces')[14]+":"+MotherCat)!=-1){
          alert("La catégorie « "+MotherCat+" » est déjà présente");
          return;
     }
     var NouvelleCat = "[["+mw.config.get('wgFormattedNamespaces')[14]+":"+MotherCat+"]]";
     TextArea.value = "\n"+ NouvelleCat;
}





//</nowiki></pre></source>