Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
How to skip a specific Div-Area while replacing text in DOM-Nodes

With this edit I try to be more specific with my question and add my last try to solve the problem: A browser add-on replaces some words on a website. This part works! Now there is some text which should not be replaced. This text is within the class "msg_content", e.g.

<span class="msg_content">don't replace my words</span>.

This part does the replacement-job and should skip elements with class "msg_content":

function replaceText (node) {
    if (node.nodeType === Node.TEXT_NODE) {
        //skip msg_content
        try {
          let note = document.querySelector('.msg_content');
          if (node == note){
            console.log("skip chat-area");
            return;
          }
        } catch (e) {
          console.log(e);
        }
        let content = node.textContent;
        for (let [word, emoji] of langMap) {
          const regex = regexs.get(word);
          content = content.replace(regex, emoji);
        }
        node.textContent = content;
  }
  else {
    for (let i = 0; i < node.childNodes.length; i++) {
      replaceText(node.childNodes[i]);
    } 
  }
}
replaceText(document.body);

The replacement works finde, but it doesn't skip the msg_content.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to check if a string is empty you can use string.length I added string.trim() in case the div contains any whitespace which is common in HTML:

function replaceText (node) {
    if (node.nodeType === Node.TEXT_NODE) {
        // My try to skip specific area here (doesn't work)
        //if (document.getElementById("chatBar")) {
        //  return;
        //}
        let content = node.textContent;
        if (!content.trim().length) return; // skip if content is empty
        for (let [word, emoji] of langMap) {
          const regex = regexs.get(word);
          content = content.replace(regex, emoji);
        }
        node.textContent = content;
  }
  else {
    // This node contains more than just text, call replaceText() on each
    // of its children.
    for (let i = 0; i < node.childNodes.length; i++) {
      replaceText(node.childNodes[i]);
    } 
  }
}
replaceText(document.body);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda