npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@maze014/dom-fetch

v1.2.1

Published

Fetch and extract DOM elements from a URL or local HTML file using a CSS query selector.

Readme

domFetch

Fetch and extract DOM elements from a URL or local HTML file using a CSS query selector.

@maze014/dom-fetch lets you retrieve HTML elements and choose how they are represented (raw element, HTML string, children HTML, or a structured breakdown).


Installation

$ npm install @maze014/dom-fetch

Usage

Basic example (fetch from a URL)

import { selectElements } from "@maze014/dom-fetch";

const source = "https://example.com";
const selector = "h1";

const elements = await selectElements(source,selector);

console.log(elements);

So this returns an array of outerHTML strings by default against the https://example.com address.


Custom usages

Depeding on your needs, you can use the option argument to define how the utility should manage your request. You can fetch from multiple sources:

  • by html file
  • by runtime string
  • by an headless browser
  • by url (default:basic usage)

Fetch from a local HTML file

import { selectElements } from "@maze014/dom-fetch";

const source = "./index.html";
const selector = ".article";
const option = { source: "file" } 

const elements = await selectElements(source,selector,options);

Fetch from runtime string

import { selectElements } from "@maze014/dom-fetch";
const source = "<!DOCTYPE html>" +`
<html>
  <body>
    <a class='article'>Click here<a/>
  </body>
</html>`;

const selector = ".article";
const options = { source: "string" } 

const elements = await selectElements(source,selector,options);

Fetch from an headless blowser

In some cases, the content needs to be the result of javascript execution. You may, then, wait for the content to be generated like for example an SPA. We got you covered with the headless source option!

We set a strategy leveraging puppeteer for that matter.

import { selectElements } from "@maze014/dom-fetch";

const source = "https://example-with-spa.com";
const selector = "main";
const options = { source: "headless" }

const elements = await selectElements(source,selector,options);

console.log(elements);

⚠️ Be aware that what you can get from the web console can differ from what you can get with domFetch. Please make sure to check what the output is before continuing your process.


Change output format

const elements = await selectElements(
  "https://example.com",
  "a",
  { output: "breakdown" }
);

Output formats

The output option controls how each matched element is returned.

| Output value | Description | | ------------- | -------------------------------------------------- | | "html" | The full HTML of the matched element (outerHTML) | | "children" | The inner HTML of the matched element | | "object" | The raw DOM Element | | "breakdown" | A structured object describing the element |

Breakdown output example

{
  tag: "a",
  text: "Click here",
  html: "<a>Click here</a>",
  attributes: {
    href: "/about",
    class: "link"
  }
}

Within a node script from a resource

First, create a simple js file that displays the paragraphs of the NodeJS page

// nodeParagraphs.mjs

const source = "https://nodejs.org/en/learn/getting-started/introduction-to-nodejs";
const selector = "main p";

const paragraphs = await selectElements(source, selector)

console.log(paragraphs.join(""))

Then write out in an html file

$ node nodeParagraphs.mjs > paragraphs.html

Within a node script from a local file

First, create a simple js file that displays the paragraphs of the NodeJS page

// nodeParagraphs.mjs

const source = "./example/nodePage.html";
const selector = "main p";
const options = { ... };

const paragraphs = await selectElements(source, selector, options)

console.log(paragraphs.join(""))

Then write out in an html file

$ node nodeParagraphs.mjs > paragraphs.html

API

selectElements(source: string, selector: string, options?: FetchOptions): Promise<any[]>

Fetches elements matching a CSS selector from a given source.

Parameters

| Parameter | Type | Default | Description | | ---------------- | ------------------------------------------------- | -------- | ----------------------------------------- | | source | string | — | URL or relative file path containing HTML | | selector | string | — | CSS selector (uses querySelectorAll) | | options.output | object or html or children or breakdown | "html" | Format of returned elements | | options.source | url or file or string or headless | "url" | Defines how the source is fetched |

Returns

Promise<any[]>
An array of elements formatted according to the selected output option.


FetchOptions

type FetchOptions = {
  output?: "object" | "html" | "children" | "breakdown";
  source?: "url" | "file" | "string" | "headless";
};

Tests

A test project is available via this repository.


License

MIT