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

@cailiao/watch-dom

v0.1.7

Published

监听dom树节点的变化,并允许作出处理。(Listens for changes to dom tree nodes and allows processing.)

Downloads

7

Readme

watch-dom —— Monitor DOM changes

简体中文 | English

Description

Monitor changes to nodes on a specified DOM tree or changes to the size of element box models and allow processing of changes.

Getting Started

Install dependencies

npm i @cailiao/watch-dom

Import

the patch function

import patch from '@cailiao/watch-dom'

// Choosing to execute the patch function at the right time will create two prototype methods $watch and $watchBox on the global class Element.
patch()

// Or import only the specified methods as needed, watch is equivalent to the $watch method on the prototype, and watchBox is equivalent to the $watchBox method on the prototype.
import { watch, watchBox } from '@cailiao/watch-dom'

Usage

  • patch function
// Monitor changes to child nodes on the DOM tree. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = targetElement.$watch(record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()


// Monitor changes to the size of element box models. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = targetElement.$watchBox(record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()
// Pause monitoring
unwatch.pause()
// Resume monitoring
unwatch.resume()
  • import only needed
// Monitor changes to child nodes on the DOM tree. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = watch(targetElement, record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()


// Monitor changes to the size of element box models. targetElement is an instance of any built-in Element class, i.e., a DOM element.
const unwatch = watchBox(targetElement, record => { console.log(record) }, { subtree: true, childList: true })
// Cancel monitoring
unwatch()
// Pause monitoring
unwatch.pause()
// Resume monitoring
unwatch.resume()

Explanation

The patch() method inserts two methods, $watch and $watchBox, on the prototype of Element. The directly imported watch is equivalent to the $watch method on the prototype, and watchBox is equivalent to the $watchBox method on the prototype, with only slight differences in the parameters passed in.

$watch Function

Parameters:
  • callback: Required. Function type. Pass in one parameter,

    • record is an array of MutationRecord. It contains information about the modification of the DOM.

    • observer: A reference to ResizeObserver itself.

  • options: Required. Object type. It is the options parameter of MutationObserver.observe(),

    Options are as follows:

    • subtree Optional

      Set to true to extend monitoring to the entire subtree of nodes rooted at target. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to the target node. The default value is false.

    • childList Optional

      Set to true to monitor the target node (and, if subtree is true, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value is false.

    • attributes Optional

      Set to true to watch for changes to the value of attributes on the node or nodes being monitored. The default value is true if either of attributeFilter or attributeOldValue is specified, otherwise the default value is false.

    • attributeFilter Optional

      An array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.

    • attributeOldValue Optional

      Set to true to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; See Monitoring attribute values for details on watching for attribute changes and value recording. The default value is false.

    • characterData Optional

      Set to true to monitor the specified target node (and, if subtree is true, its descendants) for changes to the character data contained within the node or nodes. The default value is true if characterDataOldValue is specified, otherwise the default value is false.

    • characterDataOldValue Optional

      Set to true to record the previous value of a node's text whenever the text changes on nodes being monitored. The default value is false.

returns: Function

Returns a function to cancel the monitor.

$watchBox Function

Parameters:
  • callback: Required. Function type. Pass in two parameters,

    • record is an array of ResizeObserverEntry. It contains information about the modified size of elements.

    • observer: A reference to ResizeObserver itself.

  • options: Optional. Object type. It is the options parameter of ResizeObserver.observe(),The properties of options are as follows:

    • box Sets which box model the observer will observe changes to. Possible values are:

      • content-box (the default)

        Size of the content area as defined in CSS.

      • border-box

        Size of the box border area as defined in CSS.

      • device-pixel-content-box

        The size of the content area as defined in CSS, in device pixels, before applying any CSS transforms on the element or its ancestors.

returns: Function

Returns a function to cancel the monitor.

  • pause:Function type, causes the current monitor to pause monitoring.

  • resume:Function type, resumes monitoring to the current monitor. Should be called only after pause() has been called.

watch Function

Parameters:
  • target:Required. HTMLAllCollection type.

  • callback: Required. Function type. Pass in one parameter,

    • record is an array of MutationRecord. It contains information about the modification of the DOM.

    • observer: A reference to ResizeObserver itself.

  • options: Required. Object type. It is the options parameter of MutationObserver.observe(),

    • Options are as follows:

    • subtree Optional

      Set to true to extend monitoring to the entire subtree of nodes rooted at target. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to the target node. The default value is false.

    • childList Optional

      Set to true to monitor the target node (and, if subtree is true, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value is false.

    • attributes Optional

      Set to true to watch for changes to the value of attributes on the node or nodes being monitored. The default value is true if either of attributeFilter or attributeOldValue is specified, otherwise the default value is false.

    • attributeFilter Optional

      An array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.

    • attributeOldValue Optional

      Set to true to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; See Monitoring attribute values for details on watching for attribute changes and value recording. The default value is false.

    • characterData Optional

      Set to true to monitor the specified target node (and, if subtree is true, its descendants) for changes to the character data contained within the node or nodes. The default value is true if characterDataOldValue is specified, otherwise the default value is false.

    • characterDataOldValue Optional

      Set to true to record the previous value of a node's text whenever the text changes on nodes being monitored. The default value is false.

returns: Function

Returns a function to cancel the monitor.

watchBox Function

Parameters:
  • target:Required. HTMLAllCollection type.

  • callback: Required. Function type. Pass in two parameters,

    • record is an array of ResizeObserverEntry. It contains information about the modified size of elements.

    • observer: A reference to ResizeObserver itself.

  • options: Optional. Object type. It is the options parameter of ResizeObserver.observe(),The properties of options are as follows:

    • box Sets which box model the observer will observe changes to. Possible values are:

      • content-box (the default)

        Size of the content area as defined in CSS.

      • border-box

        Size of the box border area as defined in CSS.

      • device-pixel-content-box

        The size of the content area as defined in CSS, in device pixels, before applying any CSS transforms on the element or its ancestors.

returns: Function

Returns a function to cancel the monitor.

  • pause:Function type, causes the current monitor to pause monitoring.

  • resume:Function type, resumes monitoring to the current monitor. Should be called only after pause() has been called.