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

@kensingtontech/monitoring

v1.0.8

Published

An efficient, simple, and lightweight library to watch the DOM for elements added, removed, appeared, disappeared, or resized.

Downloads

3

Readme

Monitoring

A simple, efficient, and lightweight ES module to monitor the DOM for when elements are added, removed, have appeared, have disappeared, or are resized. Internally, this library uses the MutationObserver, IntersectionObserver, and ResizeObserver APIs.

import monitoring from 'monitoring';

const monitor = monitoring(document.body);

// watch for new elements added to the DOM
monitor.added('div', div => console.log('div added:', div));

// watch for elements removed from the DOM
monitor.removed('.ad', ad => console.log('advert removed:', ad));

// watch for elements to become appear on the page
// what it means to appear or be visible is complicated, details here:
// https://developers.google.com/web/updates/2019/02/intersectionobserver-v2
monitor.appeared('#content', content => console.log('content is visible:', content));

// watch for elements that are no longer visible on the page
monitor.disappeared('img', img => console.log('img is no longer visible:', img));

// watch for when elements are resized
monitor.resized('textarea', textarea => console.log('textarea resized:', textarea));

Installation

Add to your project using NPM:

$ npm install monitoring --save

You can add monitoring directly in your site or download the latest minified version from jsdelivr:

<script type="module">
  import monitoring from 'https://cdn.jsdelivr.net/npm/monitoring/dist/monitoring-latest.min.mjs';
  
  const monitor = monitoring(document.body);
  ...
</script>

Getting callback details

Callbacks also recieve the observer entry that triggered the callback. This table shows the type of entry each methods recieves:

| Method | Entry type | | ------ | ---------- | | added | MutationObserverEntry | | removed | MutationObserverEntry | | appeared | IntersectionObserverEntry | | disappeared | IntersectionObserverEntry | | resized | ResizeObserverEntry |

Here are some examples of how the entry information can be used:

const monitor = monitoring(document.body);

monitor.added('div', (div, entry) => {
  console.log(`div added along with ${entry.addedNodes.length} other nodes`);
});

monitor.appeared('img', (img, entry) => {
  console.log(`An image is ${entry.intersectionRatio*100}% visible`);
});

monitor.resized('textarea', (textarea, entry) => {
  console.log(`textarea is now ${entry.contentRect.width} pixels wide`);
});

Stopping monitors

You can cancel a monitor by calling its cancel method. This cancels all callbacks registered against that monitor:

const monitor = monitoring(document.body);
const divAdded = monitor.added('div', console.log);
const divRemoved = monitor.added('div', console.log);

// stops the monitor, including divAdded and divRemoved
monitor.cancel(); 

You can also cancel a specific callback:

const monitor = monitoring(document.body);
const divAdded = monitor.added('div', console.log);
const divRemoved = monitor.added('div', console.log);

// cancels divAdded, doesn't effect divRemoved
divAdded.cancel(); 

Finally, you can cancel by returning false from within the callback. Note: Your callback must return false, not a falsey value like null or undefined.

const monitor = monitoring(document.body);

// cancels the callback after its first call
monitor.added('div', div => {
  console.log('div added!');
  return false; 
});

IFrames

Monitors support an iframes option to include monitoring elements within iframes of the same origin. For example:

const monitor = monitoring(document.body, {iframes: true});

monitor.added('div', div => {
  if (div.ownerDocument != document) {
    console.log('new div in an iframe!');
  }
});

Existing elements

By default, the added method will return all existing elements in the DOM that match the given selector and monitor for new ones. You can ignore existing elements by setting the existing option to false:

const monitor = monitoring(document.body);
monitor.added('.my_class', my_callback, {existing: false});

Performance

Monitors reuse their observers so you should avoid declaring new monitors for the same element. For example:

// this only uses one monitor for two callback - do this :-)
const monitor = monitoring(document.body);
monitor.added('div.my_class', div => console.log('my_class added');
monitor.removed('div.my_class', div => console.log('my_class removed');

// this uses two monitors, one for each callback - don't do this :-(
monitoring(document.body).added('div.my_class', div => console.log('my_class added');
monitoring(document.body).removed('div.my_class', div => console.log('my_class removed');

Observers were designed to be an efficient alternative to polling the DOM. However, monitoring large chunks of the DOM, like document or document.body is still expensive. I recommend monitoring the smallest portion of the DOM necessary and cancelling as soon as the monitor is no longer needed.

Here is an example of using a monitor to find more specific elements for monitoring:

// monitor the document body for a #content div
monitoring(document.body).added('#content', content => {

  // monitor the #content div for our class
  monitoring(content).added('.my_class', div => 
    console.log('found our class in the content div!');
  );

  // this cancels the document body monitor
  return false;
});

How is monitoring different from arrive.js?

arrive.js is an excellent library and was the inspiration for this project. However, there are some differences:

// works! 
document.body.arrive('div', console.log);

// does not work :-(
frames[0].document.body.arrive('div', console.log);

// works, but you need jquery 
$(frames[0].document.body).arrive('div', console.log);