Git Product home page Git Product logo

wait-element's Introduction

wait-element

Travis (.org) 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

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.

timeout

Type: number
Default: 0
Unit: ms(Millisecond)

There is no timeout by default.

observeConfigs

Type: MutationObserverInit
Default:

{
	childList: true,
	subtree: true,
	attributes: true,
}

passed configs is merged with default configs.

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

wait-element's People

Contributors

1natsu172 avatar sectsect avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sectsect

wait-element's Issues

TS support

Need type definition file completely.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.