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 🙏

© 2024 – Pkg Stats / Ryan Hefner

element-ready

v7.0.0

Published

Detect when an element is ready in the DOM

Downloads

6,221

Readme

element-ready

Detect when an element is ready in the DOM

Install

npm install element-ready

Usage

import elementReady from 'element-ready';

const element = await elementReady('#unicorn');

console.log(element.id);
//=> 'unicorn'

API

elementReady(selector, options?)

Returns a promise for a matching element.

observeReadyElements(selector, options?)

Returns an async iterable which yields with each new matching element. Useful for user-scripts that modify elements when they are added.

import {observeReadyElements} from 'element-ready';

for await (const element of observeReadyElements('#unicorn')) {
	console.log(element.id);
	//=> 'unicorn'

	if (element.id === 'elephant') {
		break;
	}
}

selector

Type: string

CSS selector.

Prefix the element type to get a better TypeScript return type. For example, button.my-btn instead of .my-btn.

options

Type: object

target

Type: Element | document
Default: document

The element that's expected to contain a match.

stopOnDomReady

Type: boolean
Default: true

Automatically stop checking for the element to be ready after the DOM ready event. The promise is then resolved to undefined.

timeout

Type: number
Default: Infinity

Milliseconds to wait before stopping the search and resolving the promise to undefined.

waitForChildren

Type: boolean
Default: true

Since the current document’s HTML is downloaded and parsed gradually, elements may appear in the DOM before all of their children are “ready”.

By default, element-ready guarantees the element and all of its children have been parsed. This is useful if you want to interact with them or if you want to .append() something inside.

By setting this to false, element-ready will resolve the promise as soon as it finds the requested selector, regardless of its content. This is ok if you're just checking if the element exists or if you want to read/change its attributes.

predicate

Type: (element: HTMLElement) => boolean
Default: undefined

A predicate function will be called for each element that matches the selector. If it returns true, the element will be returned.

For example, if the content is dynamic or a selector cannot be specific enough, you could check .textContent of each element and only match the one that has the required text.

<ul id="country-list">
	<li>country a</li>
	...
	<li>wanted country</li>
	...
</ul>
import elementReady from 'element-ready';

const wantedCountryElement = await elementReady('#country-list li', {
	predicate: listItemElement => listItemElement.textContent === 'wanted country'
});

elementReadyPromise#stop()

Type: Function

Stop checking for the element to be ready. The stop is synchronous and the original promise is then resolved to undefined.

Calling it after the promise has settled or multiple times does nothing.

Related

  • dom-loaded - Check when the DOM is loaded like DOMContentLoaded