@1natsu/wait-element 中文文档教程
wait-element
检测元素在浏览器 DOM 中的外观
a.k.a promise-querySelector
- Promise API
- Driven by
MutationObserver
- Detect by
querySelecrtor
如果执行“wait-element”时目标元素已经存在,它会立即resolve
并返回该元素。
Install
$ npm install @1natsu/wait-element
Usage
Module specifiers
import {waitElement, waitDisappearElement} from '@1natsu/wait-element';
Basically
(async () => {
const el = await waitElement('.late-comming');
console.log(el);
//=> example: "<div class="late-comming">I'm late</div>"
})();
When specify a parent element (specify MutationObserve target)
(async () => {
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>"
})();
When setting timeout
(async () => {
const el = await waitElement('.late-comming', { timeout: 5000 }).catch(err => console.log(err));
console.log(el);
//=> If detected element: "<div class="late-comming">I'm late</div>"
//=> If timeouted: Error: Element was not found: '.late-coming'
})();
When cancel wait-element
(async () => {
let el;
try {
el = waitElement('.late-comming');
if (!isCondition) el.cancel();
} catch {
// some handling...
}
})();
Wait for the element to disappear
(async () => {
const el = await waitDisappearElement('.will-disappear');
// disappeared the element from dom tree
console.log(el);
//=> example: null
})();
API
waitElement(selector, [options])
waitDisappearElement(selector, [options])
等待元素
& waitDisappearElement
是相同的 api。 区别在于等待元素出现或消失。
selector
类型:string
格式为 CSS-selector
options
target
类型: HTMLElement
默认值:document
指定父节点(指定 MutationObserve 目标)。
当您知道要检测的元素的父节点时。
- Please also refer to the MutationObserver
timeout
类型:数字
默认值:0
单位:ms(毫秒)
默认没有超时。
waitElement#cancel()
类型:Function
停止等待元素。 取消是同步的。
基于 p-cancelable。 欣赏它。
Similar
非常相似的图书馆。
- element-ready
- Implementation method is different from this library.
License
麻省理工学院 © 1natsu172
wait-element
Detect the appearance of an element in the browser DOM
a.k.a promise-querySelector
- Promise API
- Driven by
MutationObserver
- Detect by
querySelecrtor
If the target element already exists when execution of "wait-element", it immediately resolve
and return the element.
Install
$ npm install @1natsu/wait-element
Usage
Module specifiers
import {waitElement, waitDisappearElement} from '@1natsu/wait-element';
Basically
(async () => {
const el = await waitElement('.late-comming');
console.log(el);
//=> example: "<div class="late-comming">I'm late</div>"
})();
When specify a parent element (specify MutationObserve target)
(async () => {
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>"
})();
When setting timeout
(async () => {
const el = await waitElement('.late-comming', { timeout: 5000 }).catch(err => console.log(err));
console.log(el);
//=> If detected element: "<div class="late-comming">I'm late</div>"
//=> If timeouted: Error: Element was not found: '.late-coming'
})();
When cancel wait-element
(async () => {
let el;
try {
el = waitElement('.late-comming');
if (!isCondition) el.cancel();
} catch {
// some handling...
}
})();
Wait for the element to disappear
(async () => {
const el = await waitDisappearElement('.will-disappear');
// disappeared the element from dom tree
console.log(el);
//=> example: null
})();
API
waitElement(selector, [options])
waitDisappearElement(selector, [options])
waitElement
& waitDisappearElement
is same api. Difference is waiting element for appear or disappear.
selector
Type: string
Format is CSS-selector
options
target
Type: HTMLElement
Default: document
Specify a parent node (specify MutationObserve target).
When you know the parent node of the element you want to detect.
- Please also refer to the MutationObserver
timeout
Type: number
Default: 0
Unit: ms(Millisecond)
There is no timeout by default.
waitElement#cancel()
Type: Function
Stop waiting for the element. Cancellation is synchronous.
Based on p-cancelable. Appreciate it.
Similar
The very similar library.
- element-ready
- Implementation method is different from this library.
License
MIT © 1natsu172
你可能也喜欢
- 4get 中文文档教程
- @1eeing/scroll-listener 中文文档教程
- @4loop.co/vue-paginate 中文文档教程
- @a15396222371/batman-component-template 中文文档教程
- @aaa-backend-stack/graphql-rest-bindings 中文文档教程
- @aarnet/jupyterlab-ext-aarnet-help 中文文档教程
- @aaronpowell/react-static-web-apps-auth 中文文档教程
- @abiramitoronto/lotide 中文文档教程
- @abrezina/dummy-data 中文文档教程
- @abtasty/product-image-optimization 中文文档教程