https://github.com/tpkn/waitforselector
Wait for an element(s) with a specific selector to appear
https://github.com/tpkn/waitforselector
Last synced: about 1 year ago
JSON representation
Wait for an element(s) with a specific selector to appear
- Host: GitHub
- URL: https://github.com/tpkn/waitforselector
- Owner: tpkn
- License: mit
- Created: 2019-06-02T12:17:03.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-05-13T11:02:38.000Z (about 6 years ago)
- Last Synced: 2024-04-23T12:38:26.354Z (about 2 years ago)
- Language: JavaScript
- Size: 26.4 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# waitForSelector [](https://www.npmjs.org/package/waitforselector)
Wait for an element(s) with a specific selector to appear
## API
```javascript
waitForSelector(selector, cb[, options])
```
### selector
**Type**: _String_ | _Array_
Css selector or an array of selectors
### cb
**Type**: _Function_
Callback function
### options.target
**Type**: _Element_
**Default**: `document.body`
### options.endless
**Type**: _Boolean_
**Default**: `false`
Sometimes elements can be appended, removed and appended again. This option allows you to track each append of an element into the DOM structure.
### options.race
**Type**: _Boolean_
**Default**: `false`
Wait until one of the given selectros appears in DOM
### options.timeout
**Type**: _Number_
**Default**: `0`
Stop waiting after this amount of milliseconds.
Calls `cb` function with no arguments. Turned off by default.
### @return
Returns `stop` method
## Usage
```javascript
// Single
waitForSelector('.block3', (elem) => {
// =>
})
// Multiple
waitForSelector(['.block1', '.block2', '.block4', '.block8'], (list) => {
// => ['.block4', '.block2', '.block8', '.block1']
})
// Race
waitForSelector(['.block7', '.block4', '.block5', '.block4'], (elem) => {
// =>
}, { race: true })
```
## Changelog
#### v1.3.0 (2020-02-29):
- moved from `setInterval` to `MutationObserver`
#### v1.2.2 (2019-09-14):
- `delay` option renamed to `interval`
- added `timeout` option