spye

0.0.5 • Public • Published

Spye

Detect when a DOM element is created or removed.

Installation

Node.js

npm install spye --save

Browser

Local

<script src="node_modules/spye/dist/spye.min.js"></script>

CDN unpkg

<script src="https://unpkg.com/spye/dist/spye.min.js"></script>

Example

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])

Create instance

ParamTypeDefaultDescription
queryObject

element that you want watch

[opts]Object

configuration object

[opts.autoWatch]booleantrue

auto watch

[opts.checkMs]number50

interval in milliseconds for every check

[opts.unwatchAfterCreate]booleanfalse

stop check after detect element creation

[opts.unwatchAfterRemove]booleanfalse

stop check after detect element remove

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

Param
callback

spye.onRemove(callback) ⇒ Spye

Fired when element is removed

Kind: instance method of Spye

Param
callback

Changelog

You can view the changelog here

License

Spye is open-sourced software licensed under the MIT license

Author

Fabio Ricali

Package Sidebar

Install

npm i spye

Weekly Downloads

1

Version

0.0.5

License

MIT

Unpacked Size

33.8 kB

Total Files

10

Last publish

Collaborators

  • fabioricali