How to automatically link to words with javascript?

Asked

Viewed 1,471 times

3

If the page contains the words "contact", "help" and others.. turn them into a link:

<a href='http://meusite.com/pagina1.html'>contato</a>

<a href='http://meusite.com/pagina2.html'>ajuda</a>

Example:

You may need to helping, if you have any doubts please contact with

2 answers

5


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 change document.querySelectorAll("body *:not(script)"); for getElementsByClassName("nome");?

  • a few words are followed by &nbsp; example &nbsp;Confirmação how do I work with those words too?

  • Change two lines: var re = new RegExp("([\\s|&nbsp;]"+i+"(?:(?=[,<.\\s])))", "gi"); and return ' <a href="'+links[i]+'">'+a.match(/[A-zÀ-ú]+/)[0]+'</a>';

  • 1

    great script, but how do you pick up 2 words or more? for example "contato aqui": "linkcontato.html", or "abaixo-assinado": "linkcontato.html"

  • @Markvaaz Code changed and improved.

  • @Jonyboy I changed the answer code, now it’s much better. Replace it with what was before. Abs!

  • 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>?

  • @Markvaaz :not() is pseudo-class. It would work if you do it this way: document.querySelectorAll("body *:not(script):not(img)")

  • The first code worked better for me because this new one is changing words like for example galinha where the word added is only linha

  • @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 a if(/\w/.test(bodi[x].innerHTML)){

  • @Adding Markvaaz \\b in regex I think it solved the chicken problem (I updated in the answer the example).

  • Yes \\b solved the problem

  • @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 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>

Show 9 more comments

2

Assuming this text is inside an HTML element, example:

<div class="texto">Você talvez precise de ajuda, caso tenha duvidas entre em contato conosco</div>

You can use this function:

function substitiuirPalavraPorLink (palavras) {
  var textos = document.getElementsByClassName('texto');

  if (textos) {
    for (i = 0; i < textos.length; i++) {
      for (j = 0; j < palavras.length; j++) {
        if (textos[i].innerHTML.search(palavras[j].palavra) ) {
          textos[i].innerHTML=textos[i].innerHTML.replace( palavras[j].palavra, criarLink(palavras[j].link, palavras[j].palavra));
        }
      }
    }
  }

}

function criarLink (link, palavra) {
  return '<a href="' + link + '">' + palavra + '</a>';
}

function Substituir(){
//define quais palavras serão substituidas pelo respctivo link
var palavras = [{
    palavra: 'contato',
    link: 'http://meusite.com/pagina1.html'
  },
  {
    palavra: 'ajuda',
    link: 'http://meusite.com/pagina1.html'
  }
];
substitiuirPalavraPorLink(palavras);
}
<div class="texto">Você talvez precise de ajuda, caso tenha duvidas entre em contato conosco</div>

<div class="texto">Caso tente entrar em contato, não peça ajuda</div>

<input type='button' onCLick="Substituir();" value="Substituir" />

Basically what the routine does is find all tags with the Text class using the method getElementsByClassName and replace the provided words in a list.

  • And if I want it to be inside <body> and change to document.getElementsByTagName("BODY"); has any problem? there may be some malfunction on the page?

  • If you guarantee that words will not be property of a specific tag, I see no problemass

  • i want the script to run when the page loads where I put it window.onload?

  • i applied the script to the page and returned some errors can help me? https://stackoverflowteste.blogspot.com/2018/07/teste.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.