Utilisateur:Dr Brains/myName.js
/*
Code original : [[:en:User:ais523/highlightmyname2.js]]
Ajoute un bouton dans la zone de recherche permettant de surligner dans la page toutes les occurrences du texte rentré.
((Projet:JavaScript/Script))
((Boîte déroulante début|titre=Code source))<!--
*/
// --><syntaxhighlight lang=javascript>
//<nowiki>
var countcolors = 0;
var surlignerCouleurs = new Array();
///////////////////////////////////////////////// PARTIE PERSONNALISABLE //////////////////////////////////////////
// Couleurs successives de surlignage
surlignerCouleurs[0] = "#DDBBBB"
surlignerCouleurs[1] = "#BBBBBB"
surlignerCouleurs[2] = "#DDBBDD"
surlignerCouleurs[3] = "#DDDDBB"
surlignerCouleurs[4] = "#BBBBDD"
surlignerCouleurs[5] = "#DDBB99"
surlignerCouleurs[6] = "#DD99BB"
surlignerCouleurs[7] = "#DD9999"
surlignerCouleurs[8] = "#BB99BB"
surlignerCouleurs[9] = "#BBBB99"
surlignerCouleurs[10] = "#99BB99"
surlignerCouleurs[11] = "#9999BB"
///////////////////////////////////////////////// FIN DE LA PARTIE PERSONNALISABLE //////////////////////////////////////////
var maxcountcolors = surlignerCouleurs.length;
function surlignerUnMot(n, p, string){ // node, parent node, string
while(n!=null) {
if(n.nodeType==3) {
if(n.data.toLowerCase().indexOf(string.toLowerCase())!=-1) {
var ix=n.data.toLowerCase().indexOf(string.toLowerCase());
var t1=ix?document.createTextNode(n.data.substr(0,ix)):null;
var t2=document.createTextNode(n.data.substr(ix,string.length));
var t3=ix+string.length==n.data.length?null:
document.createTextNode(n.data.substr(ix+string.length));
var s1=document.createElement("span");
s1.style.backgroundColor=surlignerCouleurs[countcolors];
s1.appendChild(t2);
var s2=document.createElement("span");
if(t1!=null) s2.appendChild(t1);
s2.appendChild(s1);
if(t3!=null) s2.appendChild(t3);
p.replaceChild(s2,n);
if(t3!=null) surlignerUnMot(t3,s2, string ); //find remaining occurences in the new nodes
n=s2.nextSibling;
} else {
n=n.nextSibling;
}
} else{
if(n.firstChild!=null) surlignerUnMot(n.firstChild,n,string);
n=n.nextSibling;
}
}
}
function surlignerLeMot(){
var Document = document.getElementById('bodyContent'); // Monobook, Poussin, Mon Interface, Simple, Vector
if(!Document)Document = document.getElementById('mw_contentholder'); // Moderne
if(!Document)Document = document.getElementById('article'); // Bleu de Cologne, Nostalgia, Standard
if(!Document)Document = document.body; // Autres
var Input = document.getElementById('searchInput');
if(!Input) return;
var Mot = Input.value;
if( (Mot!=null) && (Mot!='') ){
surlignerUnMot( Document.firstChild , Document , Mot);
countcolors = (countcolors+1);
if(countcolors>maxcountcolors) countcolors = 0;
}
}
function ajouterBoutonSurlignage(){
var OldSearchForm = document.getElementById("searchform");
if(!OldSearchForm) return;
if(typeof(surlignerCouleurs_Custom)==="object") surlignerCouleurs = surlignerCouleurs_Custom;
OldSearchForm.setAttribute("style", "text-align:center");
var NewForm = '<input type="button" class="searchButton" style="width:123px;display:inline;" title="Cliquer pour surligner" value="Surligner" onclick="surlignerLeMot();" onselect="surlignerLeMot();"/>';
OldSearchForm.innerHTML = OldSearchForm.innerHTML + NewForm;
}
$( ajouterBoutonSurlignage);
//</nowiki>
//</syntaxhighlight>
//((Boîte déroulante fin))
Text is available under the CC BY-SA 4.0 license; additional terms may apply.
Images, videos and audio are available under their respective licenses.