Utilisateur:Automatik/Ancres-titre.js

Définition, traduction, prononciation, anagramme et synonyme sur le dictionnaire libre Wiktionnaire.

Remarque: après avoir sauvegardé, vous devez vider le cache de votre navigateur pour que les changements prennent effet. Mozilla, cliquez sur Actualiser (ou ctrl-r). Internet Explorer / Opera: ctrl-f5. Safari: cmd-r. Konqueror ctrl-r.

/**
 * 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, Bayo -- Correctif apporté par Chphe
 * Date de dernière révision : 26 septembre 2009
 */
 
var AncreTitres={
nom_ancre : "[URL]",
nom_lien_interne : "[[lien]]",
description : "Obtenir une URL vers cette section",
descinterne : "Obtenir un [[Lien#interne]]",
linkcolor : "",
fontSize : "xx-small",
fontWeight : "normal",
afficheE : true,
afficheI : true,
}
function ancrer(){
	if (!document.getElementById('content')) return;
	var titres = document.getElementsByTagName("span");
	var i = 0;
	var titre_courant;
        if (!AncreTitres.afficheI && !AncreTitres.afficheE) return;
	for (i = 0 ; i < titres.length ; i++){
		var titre_courant = titres[i].parentNode;	
		if (!(titres[i].className.match(/mw-headline/gi))) continue;

		var ancre_nom = titres[i].id;
		 /* Ou mieux : titre de section (type de mot) ? */
                if (titres[i].parentNode.className == 'titredef') {
			 ancre_nom = titres[i].parentNode.id;
		} else if (titres[i].firstChild) {
                          if (titres[i].firstChild.className == 'sectionlangue') {
		          	 ancre_nom = titres[i].firstChild.id;
                          }
                } else {
                         ancre_nom = titres[i].id;
                }
		
		var small;
		var link;
 
		span = document.createElement('span');
		with(span ) {
			if (AncreTitres.linkcolor != "") style.color=AncreTitres.linkcolor;
			style.fontSize=AncreTitres.fontSize;
			style.fontWeight=AncreTitres.fontWeight;
			className = 'noprint';
		}
 
		if (AncreTitres.afficheE) {
			link = document.createElement('a');
			with(link) {
				innerHTML=AncreTitres.nom_ancre;
				title=AncreTitres.description;
				setAttribute("ancre", wgServer + "/w/index.php?title="
					+ wgPageName + "#" + ancre_nom
				);
				setAttribute("onclick", "window.prompt("
					+ "'Lien : ', this.getAttribute('ancre'));"
				);
                        	setAttribute("href", "javascript:;");										}
			span.appendChild(document.createTextNode(' '));
			span.appendChild(link);
		}
		if (AncreTitres.afficheI) {
			link = document.createElement('a');
			with(link) {
				innerHTML=AncreTitres.nom_lien_interne;
				title=AncreTitres.descinterne;
				setAttribute("ancre", "[[" + wgPageName + "#" + ancre_nom + "]]");
				setAttribute("onclick", "window.prompt("
					+ "'Lien : ', this.getAttribute('ancre'));"
				);
                        	setAttribute("href", "javascript:;");
			}
			span.appendChild(document.createTextNode(' '));
			span.appendChild(link);
		}
		titre_courant.appendChild(span);
	}
}
$(ancrer);