kuhaku kuhaku - 2 months ago 8
HTML Question

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

Answer

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

Comments