spye

Detect when a DOM element is created or removed

  • spye

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
spye
000.0.56 years ago6 years agoMinified + gzip package size for spye in KB

Readme

Spye
Detect when a DOM element is created or removed.

Installation

Node.js

``` npm install spye --save ```

Browser

Local

```html ```

CDN unpkg

```html ```

Example

```javascript const Spye = require('spye'); new Spye('#my-element')
.onCreate((element)=> console.log('created', element))
.onRemove(()=> console.log('removed'));
// Create and append element to body let element = document.createElement('div'); element.setAttribute('id', 'my-element'); document.body.appendChild(element); // Remove element after 1s setTimeout(()=>{
document.body.removeChild(element);
}, 1000); ```

API

Spye

Kind: global class
* [new Spye(query, [opts])](#new_Spye_new)
* [.watch()](#Spye+watch) ⇒ [<code>Spye</code>](#Spye)
* [.unwatch()](#Spye+unwatch) ⇒ [<code>Spye</code>](#Spye)
* [.onCreate(callback)](#Spye+onCreate) ⇒ [<code>Spye</code>](#Spye)
* [.onRemove(callback)](#Spye+onRemove) ⇒ [<code>Spye</code>](#Spye)

new Spye(query, opts)

Create instance
<tr>
  <th>Param</th><th>Type</th><th>Default</th><th>Description</th>
</tr>
<td>query</td><td><code>Object</code></td><td></td><td><p>element that you want watch</p>
</tr><tr>
<td>[opts]</td><td><code>Object</code></td><td></td><td><p>configuration object</p>
</tr><tr>
<td>[opts.autoWatch]</td><td><code>boolean</code></td><td><code>true</code></td><td><p>auto watch</p>
</tr><tr>
<td>[opts.checkMs]</td><td><code>number</code></td><td><code>50</code></td><td><p>interval in milliseconds for every check</p>
</tr><tr>
<td>[opts.unwatchAfterCreate]</td><td><code>boolean</code></td><td><code>false</code></td><td><p>stop check after detect element creation</p>
</tr><tr>
<td>[opts.unwatchAfterRemove]</td><td><code>boolean</code></td><td><code>false</code></td><td><p>stop check after detect element remove</p>
</tr>  </tbody>

spye.watch() ⇒ Spye

Start watching
Kind: instance method of Spye

spye.unwatch() ⇒ Spye

Stop watching
Kind: instance method of Spye

spye.onCreate(callback) ⇒ Spye

Fired when element is created
Kind: instance method of Spye
<tr>
  <th>Param</th>
</tr>
<td>callback</td>
</tr>  </tbody>

spye.onRemove(callback) ⇒ Spye

Fired when element is removed
Kind: instance method of Spye
<tr>
  <th>Param</th>
</tr>
<td>callback</td>
</tr>  </tbody>

Changelog

You can view the changelog here

License

Spye is open-sourced software licensed under the MIT license

Author

Fabio Ricali