Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8407761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:27:20+00:00 2026-06-09T23:27:20+00:00

I am writing a Chrome Extension to convert HTML pages into a different format.

  • 0

I am writing a Chrome Extension to convert HTML pages into a different format.

If I use document.getElementsByTagName("*") and iterate over that collection, I can see all the tags. However, it’s a flat representation. I need to detect the opening and closing “events”, like a SAX parser, so that my translated output maintains proper containment/nesting.

What is the right way to do this in JavaScript? It seems a little awkward to have to do this manually. Is there any other way to do this?

To illustrate what I mean…

   <html>
       <body>
           <h1>Header</h1>
           <div>
               <p>some text and a missing closing tag
               <p>some more text</p>
           </div>
           <p>some more dirty HTML
        </body>
    <html>

I need to get the events in this order:

    html open
    body open
    h1 open
    text
    h1 close
    div open
    p open
    text
    p close
    p open
    text
    p close
    div close
    p open
    text
    p close
    body close
    html close

I get the feeling it’s up to me to track the SAX-parser-like events as part of my iteration. Are there any other options available to me? If not, can you point me to any sample code?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:27:22+00:00Added an answer on June 9, 2026 at 11:27 pm

    Just traverse each node and all the children of each node. When a level of children is exhausted, the tag is closed.

    function parseChildren(node) {
    
        // if this a text node, it has no children or open/close tags
        if(node.nodeType == 3) {
            console.log("text");
            return;
        }
    
        console.log(node.tagName.toLowerCase() + " open");
    
        // parse the child nodes of this node
        for(var i = 0; i < node.childNodes.length; ++i) {
            parseChildren(node.childNodes[i]);
        }
    
        // all the children are used up, so this tag is done
        console.log(node.tagName.toLowerCase() + " close");
    }
    

    To traverse the whole page, just do parseChildren(document.documentFragment). You can replace the console.log statements with whatever behavior you like.

    Note that this code reports a lot of text nodes, because the whitespace between tags counts as a text node. To avoid this, just expand the text handling code:

        if(node.nodeType == 3) {
            // if this node is all whitespace, don't report it
            if(node.data.replace(/\s/g,'') == '') { return; }
    
            // otherwise, report it
            console.log("text");
            return;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a chrome extension that dynamically writes some html pages and their
I'm writing a Chrome Extension that changes calls to document.all so that it can
I am writing a Chrome extension that needs to modify pages in a specific
I'm writing a Google Chrome extension that finds all titles on pages like this
I'm writing a Chrome Extension that adds functionality to certain pages a user visits.
I am writing a chrome extension that is a 'content script' I want to
I am writing a Chrome Extension that inserts a DIV on top of an
I am writing codes for a Chrome extension that needs to access minus.com, which
I'm writing a Chrome extension that needs to maintain a list of <object> and
I am writing a chrome extension that change part of the url in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.