dom-reducer

DOM crawler, executing given reducer recursively beginning with provided node.

Usage no npm install needed!

<script type="module">
  import domReducer from 'https://cdn.skypack.dev/dom-reducer';
</script>

README

dom-reducer

CircleCI

Reduce designed for DOM crawling. It can be used for node info collection.

It can behave like Visitor pattern modifying nodes, and if you only need that functionality or breadth-first traversal, then use dom-visitor instead:

DOMReduce takes 3 arguments:

  • startingNode ex.: document.body,

  • reducer, a function called on every node with accumulator, ex.: (accu, node) => accu + (node.tagName === "DIV" ? 1 : 0)

  • initialValue can be anything but it's usually: 0, {}, [], ""

DOMReduce supports Browser, Node.js & AMD.

USAGE

Refer to index.spec.js for Node.js fakeDOM example

In browser: inject DOMReduce code unto a website through devtools or script tag, then:

const tagCount = (tag, node) =>
  DOMReduce(
    node, 
    (count, node) => count + (node.tagName === tag ? 1 : 0), 
    0
  );

tagCount("DIV", document.body);

Happy DOM crawling!