1.0.28 • Published 7 years ago

dom-element-watcher v1.0.28

Weekly downloads
2
License
BSD-2-Clause
Repository
github
Last release
7 years ago

DOMElementWatcher

A tiny and super simple library that watches for changes to the DOM. Give it one or more CSS selectors, and it will call you back with matching elements as and when they become available.

Tested in Chrome, Firefox, Safari, Edge and IE 9-11.

IE 9 and 10 require polyfills for WeakMap and MutationObserver (pre-built versions included - see below).

Installation

npm install dom-element-watcher --save

Then add a reference to one of:

./node_modules/dom-element-watcher/dist/DOMElementWatcher.js             # native (~4KB)
./node_modules/dom-element-watcher/dist/DOMElementWatcher.min.js         # native, minified (<1KB)
./node_modules/dom-element-watcher/dist/DOMElementWatcher.IE9-10.js      # polyfilled for old IE (~17KB)
./node_modules/dom-element-watcher/dist/DOMElementWatcher.IE9-10.min.js  # polyfilled for old IE, minified (~6KB)

eg:

<script src="./node_modules/dom-element-watcher/dist/DOMElementWatcher.min.js"></script>

Usage

// create the watcher
var elementWatcher = new DOMElementWatcher();

// for all DIV elements with class="my-class", either existing now or added to the DOM in the future, 
// set their background color to red
elementWatcher.when('div.my-class', -1, function (element) {
    element.style.background = 'red';
});

// temporarily pause watching...
elementWatcher.pause();

// ... and resume once more
elementWatcher.resume();

// then perhaps later, stop watching permanently.
elementWatcher.stop()

API

.when(selector, index, callback)

Adds the selector to the list of watched changes. If a matching element is already present in the DOM, the callback will be executed immediately. The callback will be executed a maximum of once on each matched element.

  • selector: A CSS selector against which to match elements. Selectors can be arbitrarily complex (whatever is supported by the browser's native querySelectorAll()).
  • index: In the case where multiple elements are matched by the selector, this restricts the match to this array index. Pass in -1 to match all elements.
  • callback: The function to call when an element is matched. It is passed the matched HTMLElement as its argument.

.pause()

Temporarily stop watching for changes. Callbacks will not be executed.

.resume()

Resumes watching for changes. Callbacks will be executed once more.

.stop()

Permanently stops watching for changes and disconnects the MutationObserver. Watcher cannot be used after calling this method.

Live demo

npm install
npm run build

Then open demo.html.

Development

git clone https://github.com/mikechamberlain/dom-element-watcher.git
cd dom-element-watcher
npm install
npm run build

See also

The mutation-summary library might also be of interest. It's a lot larger and more complex, but has way more features and gives you a full breakdown of exactly how the DOM changed.

1.0.28

7 years ago

1.0.27

7 years ago

1.0.26

7 years ago

1.0.25

7 years ago

1.0.24

7 years ago

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.20

7 years ago

1.0.19

7 years ago

1.0.18

7 years ago

1.0.17

7 years ago

1.0.16

7 years ago

1.0.15

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago