« MediaWiki:Gadget-AncreTitres.js » : différence entre les versions

Un livre de Wikilivres.
Pabix (discussion | contributions)
m désolé !
Seb35 (discussion | contributions)
m a renommé MediaWiki:JSScripts/AncreTitres en MediaWiki:Gadget-AncreTitres.js: transfert suite au système de gadgets
(Aucune différence)

Version du 2 décembre 2007 à 17:44

//[[Image:AncreTitres JS.png|thumb|300px|Illustration]]<source lang="javascript" line="">
/*
 * AncreTitres
 *
 * Cette fonction fournit un lien vers une section de page en cliquant
 * sur le lien [ancre] à droite du titre de section.
 *
 * Auteurs : Pabix, Phe
 * Date de dernière révision : 15 août 2007
 */
 
var AncreTitres={
nom_ancre : "[URL]",
nom_lien_interne : "[[lien]]",
description : "Obtenir une URL vers cette section",
descinterne : "Obtenir un [[Lien#interne]]",
linkcolor : "blue",
fontSize : "50%",
afficheE : true,
afficheI : true,
noeud_precedent : function(noeud) {
        while (noeud.previousSibling.nodeType!=1){
    	    noeud = noeud.previousSibling;
	    if (noeud==null) return undefined;
        }
        return noeud.previousSibling;
}
}
function ancrer(){
	if (!document.getElementById('content')) return;
	var titres = document.getElementsByTagName("span");
	var i = 0;
	var titre_courant;
	for (i = 0 ; i < titres.length ; i++){
		var titre_courant = titres[i].parentNode;	
		if (!(titres[i].className.match(/mw-headline/gi))) continue;
		if (!AncreTitres.noeud_precedent(titre_courant)) continue;
		var ancre_nom;
		if (AncreTitres.noeud_precedent(titre_courant).lastChild != null){
			ancre_nom = AncreTitres.noeud_precedent(titre_courant).lastChild.name;
		} else {
			ancre_nom = AncreTitres.noeud_precedent(titre_courant).name;
		}
		var small;
		var link;
		if (AncreTitres.afficheE) {
			small = document.createElement('small'); with(small) {
				innerHTML=AncreTitres.nom_ancre;
				title=AncreTitres.description;
				style.color=AncreTitres.linkcolor;
				style.fontSize=AncreTitres.fontSize;
			}
			link = document.createElement('a'); 
			link.setAttribute("ancre", wgServer + "/w/index.php?title=" + wgPageName + "#" + ancre_nom) ;
			link.setAttribute("onclick", "window.prompt('Lien : ', this.getAttribute('ancre'));");
			link.appendChild(small);
			titre_courant.appendChild(link);
		}
		if (AncreTitres.afficheI) {
			small = document.createElement('small'); with(small) {
				innerHTML=AncreTitres.nom_lien_interne;
				title=AncreTitres.descinterne;
				style.color=AncreTitres.linkcolor;
				style.fontSize=AncreTitres.fontSize;
			}
			link = document.createElement('a');
			link.setAttribute("ancre", "[[" + wgPageName + "#" + ancre_nom + "]]");
			link.setAttribute("onclick", "window.prompt('Lien : ', this.getAttribute('ancre'));");
			link.appendChild(small);
			titre_courant.appendChild(link);
		}
	}
}
addOnloadHook(ancrer);