# @blackglory/query

> A module for querying elements from pages

Latest version **0.6.0** (published 2026-01-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install @blackglory/query
pnpm add @blackglory/query
yarn add @blackglory/query
bun add @blackglory/query
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2026-01-14 |
| First published | 2019-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 2 |
| Unpacked size | 42.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | BlackGlory |
| Maintainers | black_glory |
| Keywords | query, css, xpath |

## Links

- npm: https://www.npmjs.com/package/@blackglory/query
- Repository: https://github.com/BlackGlory/query
- Homepage: https://github.com/BlackGlory/query#readme
- Issues: https://github.com/BlackGlory/query/issues
- npm.io page: https://npm.io/package/@blackglory/query

## Dependencies (2)

- [extra-dom](https://npm.io/package/extra-dom.md) ^0.7.0
- [@blackglory/prelude](https://npm.io/package/@blackglory/prelude.md) ^0.4.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.6.0 (latest) — 2026-01-14
- 0.5.7 — 2023-06-11
- 0.5.6 — 2022-11-07
- 0.5.5 — 2022-08-01
- 0.5.4 — 2021-12-12
- 0.5.3 — 2021-10-14
- 0.5.2 — 2021-07-13
- 0.5.1 — 2021-07-03
- 0.5.0 — 2021-05-17
- 0.4.4 — 2021-05-07
- 0.4.3 — 2021-04-24
- 0.4.2 — 2021-04-02
- 0.4.1 — 2021-04-02
- 0.4.0 — 2021-04-02
- 0.3.13 — 2021-03-17
- … 16 more at https://npm.io/package/@blackglory/query/versions

## README

# query
A module for querying elements from pages.

## Install
```sh
npm install --save @blackglory/query
# or
yarn add @blackglory/query
```

## Usage
```ts
import { query, css, xpath } from '@blackglory/query'

const elements = query(
  css`main`
, xpath`.//h2[text()="Title"]`
, xpath`../div`
)
```

## API
```ts
type ISelectorResult =
| null
| undefined
| Node
| Iterable<Node>

type ISelector =
| ((node: Node) => ISelectorResult)
| ((this: Document, node: Node) => ISelectorResult)
| ISelector[]
```

### query
```ts
function query<T extends Node>(
  this: void | Document
, root: Node | Iterable<Node>
, ...selectors: ISelector[]
): T | undefined
function query<T extends Node>(
  this: void | Document
, ...selectors: [ISelector, ...ISelector[]]
): T | undefined
```

### queryAll
```ts
function queryAll<T extends Node>(
  this: void | Document
, root: Node | Iterable<Node>
, ...selectors: ISelector[]
): T[]
function queryAll<T extends Node>(
  this: void | Document
, ...selectors: [ISelector, ...ISelector[]]
): T[]
```

### Selectors
#### css
```ts
function css<T extends Element>(
  strings: TemplateStringsArray
, ...values: string[]
): (node: Node) => Iterable<T>
function css<T extends Element>(
  selector: string
): (node: Node) => Iterable<T>
```

#### xpath
```ts
function xpath<T extends Node>(
  strings: TemplateStringsArray
, ...values: string[]
): (this: Document, node: Node) => Iterable<T>
function xpath<T extends Node>(
  expression: string
): (this: Document, node: Node) => Iterable<T>
```

#### ancestor
```ts
function ancestor<T extends Node & ParentNode>(
  predicate: (node: Node, nth: number) => unknown = () => true
): (node: Node) => T | undefined
```

#### nthAncestor
```ts
function nthAncestor<T extends Node>(nth: number): (node: Node) => T | undefined
```

#### nextSibling
```ts
function nextSibling<T extends ChildNode>(
  predicate: (node: Node, nth: number) => unknown = () => true
): (node: Node) => T | undefined
```

#### previousSibling
```ts
function previousSibling<T extends ChildNode>(
  predicate: (node: Node, nth: number) => unknown = () => true
): (node: Node) => T | undefined
```

#### nextNthSibling
```ts
function nextNthSibling<T extends ChildNode>(
  nth: number
): (node: Node) => T | undefined
```

#### previousNthSibling
```ts
function previousNthSibling<T extends ChildNode>(
  nth: number
): (node: Node) => T | undefined
```

#### nextElementSibling
```ts
function nextElementSibling<T extends Element>(
  predicate: (node: Node, nth: number) => unknown = () => true
): (node: Node) => T | undefined
```

#### previousElementSibling
```ts
function previousElementSibling<T extends Element>(
  predicate: (node: Node, nth: number) => unknown = () => true
): (node: Node) => T | undefined
```

#### nextNthElementSibling
```ts
function nextNthElementSibling<T extends Element>(
  nth: number
): (node: Node) => T | undefined
```

#### previousNthElementSibling
```ts
function previousNthElementSibling<T extends Element>(
  nth: number
): (node: Node) => T | undefined
```

#### followingSiblings
```ts
function followingSiblings<T extends ChildNode>(
  predicate: (node: ChildNode, nth: number) => unknown = () => true
): (node: Node) => Iterable<T>
```

#### precedingSiblings
```ts
function precedingSiblings<T extends ChildNode>(
  predicate: (node: ChildNode, nth: number) => unknown = () => true
): (node: Node) => Iterable<T>
```

#### followingElementSiblings
```ts
function followingElementSiblings<T extends Element>(
  predicate: (node: Element, nth: number) => unknown = () => true
): (node: Node) => Iterable<T>
```

#### precedingElementSibling
```ts
function precedingElementSibling<T extends Element>(
  predicate: (node: Element, nth: number) => unknown = () => true
): (node: Node) => Iterable<T>
```

---
_Source: https://npm.io/package/@blackglory/query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
