@1natsu/wait-element

中文文档
5.0.0发布于 2026/10/02MITTypeScriptES Module近 30 天下载 295.4万

Detect the appearance of an element in the browser DOM

原始使用文档
文档目录9 节
  1. a.k.a promise-querySelector
  2. Install
  3. Usage
  4. Module specifiers
  5. API
  6. waitElement(selector, [options])
  7. createWaitElement(initOptions)
  8. Similar
  9. License

wait-element

npm npm bundle size (minified) npm bundle size (minified + gzip)

Detect the appearance of an element in the browser DOM

a.k.a promise-querySelector

  • Promise API
  • Driven by MutationObserver
  • Detect by querySelecrtor
  • Possible to abort with AbortSignal

If the target element already exists when execution of "wait-element", it immediately resolve and return the element.

Install

npm install @1natsu/wait-element
yarn add @1natsu/wait-element
pnpm add @1natsu/wait-element
bun add @1natsu/wait-element

Usage

Module specifiers

import { waitElement } from "@1natsu/wait-element";

Basically

const el = await waitElement(".late-comming");
console.log(el);
//=> example: "<div class="late-comming">I'm late</div>"

Specify parent target element (specify MutationObserve target)

const parent = await waitElement("#parent");
const el = await waitElement(".late-comming", { target: parent });
console.log(el);
//=> example: "<div class="late-comming">I'm late</div>"

Setting timeout

const el = await waitElement(".late-comming", { signal: AbortSignal.timeout(1000) }).catch(err => console.log(err));
console.log(el);
//=> If detected element: "<div class="late-comming">I'm late</div>"
//=> If timeouted: DOMException: TimeoutError

Abort the waiting

try {
    const waitAbortable = new AbortController();

    const checkElement = waitElement(".late-comming", { signal: waitAbortable.signal });

    waitAbortable.abort("abort this time!");

} catch(error) {
    // After abort handling...
}

Custom detect condition

const el = await waitElement("#animal", {
  detector: (element) =>
        element?.textContent === "Tiger"
            ? { isDetected: true, result: element }
            : { isDetected: false },
});
console.log(el.textContent);
//=> example: Tiger
import { isNotExist } from "@1natsu/wait-element/detectors";

// when resolve if “not exist” or “disappear” at the time of call
const result = await waitElement(".hero", { detector: isNotExist });
//=> result: null

Unify waiting process

Unifies the process of finding an element. If set true, increases efficiency. Unify the same arguments(includes options) with each other.

const A = waitElement(".late-comming", {
    unifyProcess: true,
});

const B = waitElement(".late-comming", {
    unifyProcess: true,
});

const C = waitElement(".late-comming", {
    unifyProcess: true,
    signal: AbortSignal.timeout(1000)
});

const D = waitElement(".late-comming", {
    unifyProcess: false,
});

// Unified:
// A === B
// B !== C
// B !== D

API

waitElement(selector, [options])

selector

Type: string

Format is CSS-selector

options

Passed options is merged with default configs.

See TS definition for detailed information

createWaitElement(initOptions)

Custom waitElement function can be created.

Similar

The very similar library.

  • element-ready
    • Implementation method is different from this library.

License

MIT © 1natsu172

版本与发布记录

@1natsu/wait-element 共发布 17 个版本,首次发布于 2018/03/07;过去 12 个月发布了 3 个版本。

版本发布日期距今
5.0.0latest2026/10/021 个月前
4.2.02026/04/057 个月前
4.1.32026/04/057 个月前
4.1.22024/12/071.9 年前
4.1.12024/11/182 年前
4.1.02024/11/062 年前

其他发布通道:beta → 4.0.0-beta.2。

关于 @1natsu/wait-element 的常见问题

@1natsu/wait-element 是什么?

Detect the appearance of an element in the browser DOM

如何安装 @1natsu/wait-element?

在项目目录运行 npm install @1natsu/wait-element,或使用 pnpm add / yarn add / bun add。

@1natsu/wait-element 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@1natsu/wait-element 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@1natsu/wait-element 可以商用吗?

@1natsu/wait-element 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@1natsu/wait-element 的下载量有多少?

根据 npm 官方下载统计,@1natsu/wait-element 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,954,043 次,日均约 98,468 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@1natsu/wait-element 还在维护吗?

最新版本 5.0.0 发布于 2026/10/02(1 个月前);至今共发布 17 个版本,其中过去 12 个月发布了 3 个;首次发布于 2018/03/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。