Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!