3
This solution uses replace
with Regular Expression to replace occurrences. However, it is necessary that the text to be analyzed is inside some tag.
Example:
document.addEventListener("DOMContentLoaded", function(){
var links = {
"contato aqui": "linkcontatoaqui.html",
"abaixo-assinado": "linkaabaixoassinado.html",
"ajuda": "linkajuda.html",
"linha": "linklinha.html"
}
var bodi = document.querySelectorAll("body *:not(script)");
for(var x=0; x<bodi.length; x++){
var html = bodi[x].innerHTML;
for(var i in links){
var re = new RegExp("\\b(?![^<]*?>)("+i+")(?!>)", "gi");
html = html.replace(re, ' <a href="'+links[i]+'">$1</a>');
}
bodi[x].innerHTML = html;
}
});
Contato ← este não é alterado porque não está dentro de uma tag
<div class="ajuda">
<div data-url="http://ajuda.com">ajuda</div>
<br>
abaixo-assinado galinha ou linha
<br>
Você talvez precise de ajuda, caso tenha duvidas entre em Contato conosco
<p>Você talvez precise de Ajuda caso tenha duvidas entre em contato.</p>
</div>
<p>Você talvez precise de Ajuda caso tenha duvidas entre em contato aqui ajuda.</p>
The expression "\\b(?![^<]*?>)("+i+")(?!>)"
will search the HTML of the elements for each name of the object items links
and do the replace
by the group $1
and applying their respective value in the tag <a>
.
By the regular expression pattern, only words that are not within tag limiters will be replaced <>
(attributes, properties etc.). O gi
are the expression options. O g
will capture all occurrences, and the i
will ignore the case sensitive (note that in the example both the word "help" and "help" have been replaced).
This selector document.querySelectorAll("body *:not(script)");
will select all elements that are in the body
(except texts outside of tags, as stated at the beginning of this reply) and the :not(script)
will prevent content from being selected from scripts, which does not matter in this case.
In the object links
you can create a list of words, followed by the link you want for each one:
var links = {
"contato": "linkcontato.html",
"ajuda": "linkajuda.html" ← link da palavra
} ↑
palavra a ser
substituída
Edit
To skip tags that do not have innerHTML you can include the contents of the first for
in a if
who makes that check:
for(var x=0; x<bodi.length; x++){
if(bodi[x].innerHTML.length){ // ← ignora tags sem innerHTML
var html = bodi[x].innerHTML;
for(var i in links){
var re = new RegExp("\\b(?![^<]*?>)("+i+")(?!>)", "gi");
html = html.replace(re, ' <a href="'+links[i]+'">$1</a>');
}
bodi[x].innerHTML = html;
}else{
console.log(bodi[x]);
}
}
if I want it to take only inside a
div
with a specific class I must changedocument.querySelectorAll("body *:not(script)");
forgetElementsByClassName("nome");
?– user117425
a few words are followed by
example Confirmação
how do I work with those words too?– user117425
Change two lines:
var re = new RegExp("([\\s| ]"+i+"(?:(?=[,<.\\s])))", "gi");
andreturn ' <a href="'+links[i]+'">'+a.match(/[A-zÀ-ú]+/)[0]+'</a>';
– Sam
great script, but how do you pick up 2 words or more? for example
"contato aqui": "linkcontato.html",
or"abaixo-assinado": "linkcontato.html"
– Mark Vaaz
@Markvaaz Code changed and improved.
– Sam
@Jonyboy I changed the answer code, now it’s much better. Replace it with what was before. Abs!
– Sam
I had changed
(/[A-zÀ-ú]+/)
for(/[A-zÀ-ú\s-]+/)
but as you put it really is better, however I have a doubt about that part:document.querySelectorAll("body *:not(script)");
the pseudo element:not()
is css right? I tried to add:not(img)
and it didn’t work for me, as I do to ignore so much<script>
how much<img>
?– Mark Vaaz
@Markvaaz
:not()
is pseudo-class. It would work if you do it this way:document.querySelectorAll("body *:not(script):not(img)")
– Sam
The first code worked better for me because this new one is changing words like for example
galinha
where the word added is onlylinha
– user117425
@Markvaaz It’s true... I’ll see to that. One way to ignore tags that do not have innerHTML (such as <img>) without having to put :not(img), would be to put the contents of the first
for
within aif(/\w/.test(bodi[x].innerHTML)){
– Sam
@Adding Markvaaz
\\b
in regex I think it solved the chicken problem (I updated in the answer the example).– Sam
Yes
\\b
solved the problem– user117425
@Markvaaz Only a correction in the if I said above, the best would be:
if(bodi[x].innerHTML.length){
... don’t need to use.test
. I’ll include that in my reply as a suggestion.– Sam
@sam I decided to change the link style
style='color:#232323'
and I realized that the links already present on the site are also being replaced and this would ruin for example a button made from a link like:<a href='#' style='background-color:#000; padding:10px;'>Botão</a>
– user117425