@cailiao/watch-dom
v1.0.3
Published
监听dom树节点的变化,并允许作出处理。(Listens for changes to dom tree nodes and allows processing.)
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-domImport
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.Functiontype. Pass in one parameter,recordis an array of MutationRecord. It contains information about the modification of the DOM.observer: A reference to ResizeObserver itself.
options: Required.Objecttype. It is the options parameter of MutationObserver.observe(),Options are as follows:
subtreeOptionalSet to
trueto extend monitoring to the entire subtree of nodes rooted attarget. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to thetargetnode. The default value isfalse.childListOptionalSet to
trueto monitor the target node (and, ifsubtreeistrue, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value isfalse.attributesOptionalSet to
trueto watch for changes to the value of attributes on the node or nodes being monitored. The default value istrueif either ofattributeFilterorattributeOldValueis specified, otherwise the default value isfalse.attributeFilterOptionalAn array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.
attributeOldValueOptionalSet to
trueto 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 isfalse.characterDataOptionalSet to
trueto monitor the specified target node (and, ifsubtreeistrue, its descendants) for changes to the character data contained within the node or nodes. The default value istrueifcharacterDataOldValueis specified, otherwise the default value isfalse.characterDataOldValueOptionalSet to
trueto record the previous value of a node's text whenever the text changes on nodes being monitored. The default value isfalse.
returns: Function
Returns a function to cancel the monitor.
$watchBox Function
Parameters
callback: Required.Functiontype. 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.Objecttype. It is the options parameter of ResizeObserver.observe(),The properties of options are as follows:boxSets 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-boxSize of the box border area as defined in CSS.
device-pixel-content-boxThe 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:
Functiontype, causes the current monitor to pause monitoring.resume:
Functiontype, resumes monitoring to the current monitor. Should be called only afterpause()has been called.
watch Function
Parameters
target:Required.HTMLAllCollectiontype.callback: Required.Functiontype. 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.Objecttype. It is the options parameter of MutationObserver.observe(),Options are as follows:
subtreeOptionalSet to
trueto extend monitoring to the entire subtree of nodes rooted attarget. All of the other properties are then extended to all of the nodes in the subtree instead of applying solely to thetargetnode. The default value isfalse.childListOptionalSet to
trueto monitor the target node (and, ifsubtreeistrue, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value isfalse.attributesOptionalSet to
trueto watch for changes to the value of attributes on the node or nodes being monitored. The default value istrueif either ofattributeFilterorattributeOldValueis specified, otherwise the default value isfalse.attributeFilterOptionalAn array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.
attributeOldValueOptionalSet to
trueto 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 isfalse.characterDataOptionalSet to
trueto monitor the specified target node (and, ifsubtreeistrue, its descendants) for changes to the character data contained within the node or nodes. The default value istrueifcharacterDataOldValueis specified, otherwise the default value isfalse.characterDataOldValueOptionalSet to
trueto record the previous value of a node's text whenever the text changes on nodes being monitored. The default value isfalse.
returns: Function
Returns a function to cancel the monitor.
watchBox Function
Parameters
target:Required.HTMLAllCollectiontype.callback: Required.Functiontype. 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.Objecttype. It is the options parameter of ResizeObserver.observe(),The properties of options are as follows:boxSets 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-boxSize of the box border area as defined in CSS.
device-pixel-content-boxThe 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:
Functiontype, causes the current monitor to pause monitoring.resume:
Functiontype, resumes monitoring to the current monitor. Should be called only afterpause()has been called.
🤝 Support
Enjoying this project? Show your support by giving it a star! ⭐
Your stars help the project gain visibility and encourage further development.
