Parse html of current page

I want to make a script that will parse the html of the current page, filtering out certain div classes and for now to write their contents to a file or remove everything else but them on the page.

I guess the best way would be to run a Tampermonkey script on that page. I looked on http://userscripts-mirror.org/ but didn't find a script like this.

Is there a javascript html parser that can run on chrome?

Something that could work like this maybe?

 var divClasses = parseCurrentPage("div class x");
 // then do something on divClasses and then show only them

Answers


Jquery can do all of this and more. I would recommend reading up on it https://learn.jquery.com/

Once you have included jquery a simple grab all "div" elements selector would be something like this: var divClasses = $('div'); If you want to only grab certain div elements you can easily do this using selectors, either by adding class, id, and/or parent/hierarchy level restrictions to the selector - read more here https://api.jquery.com/category/selectors/

Then after you did your something you want to do on the div elements you can again use jquery to only show them using jquery's 'append' function.

Simple call the 'append' function with the div element you want to append on the parent html element $('selector-to-grap-div\'s-parent-html').append(myDiv); if you need to grab the parent of one of the div elements then you can use jquery's 'parent' function


filtering out certain div classes

You can use document.getElementsByClassName()

var elements = document.getElementsByClassName(names); // or:
var elements = rootElement.getElementsByClassName(names);
  • elements is a live HTMLCollection of found elements.
  • names is a string representing the list of class names to match; class names are separated by whitespace
  • getElementsByClassName can be called on any element, not only on the document. The element on which it is called will be used as the root of the search.

I believe you can make it easy with jQuery... just get the content of <body> and do your queries with jquery.


Need Your Help

how to create a maven goal?

maven maven-plugin

I use Maven 3.0.3. I need to run some static tests, in a Maven goal, without recompiling the project.

Print Report in Microsoft Dynamics AX 2009 through X++

axapta x++

I am trying to print sales confirmation report on a button click which I have added on Sales Order Detail form in Microsoft Dynamics AX 2009. On click event of that button, I have written following...