Skip to content

Instantly share code, notes, and snippets.

@taikulawo
Last active May 4, 2020 07:15
Show Gist options
  • Save taikulawo/680fcfe42a3e7f9fe180e1456e24cb4d to your computer and use it in GitHub Desktop.
Save taikulawo/680fcfe42a3e7f9fe180e1456e24cb4d to your computer and use it in GitHub Desktop.
检测DOM变动

引入

var node = document.createElement('script')
node.src = 'https://gist.githubusercontent.com/iamwwc/680fcfe42a3e7f9fe180e1456e24cb4d/raw/c2e914eee5ac70b655c98550cec8264fcc86fe05/monitor.js'

document.querySelector('body').appendChild(node)
const targetNode = document.querySelector('body');
// Options for the observer (which mutations to observe)
const config = {childList: true };
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
for (let i = 0 ; i < mutationsList.length ; i ++) {
const mutation = mutationsList[i]
const target = mutation.target
const length = target.children.length
const addedNodes = mutation.addedNodes
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config)
!function () {
window.monitordom = function (domnode, cb,config = {childList: true}) {
const targetNode = domnode
// Callback function to execute when mutations are observed
const callback = function (mutationsList, observer) {
for (let i = 0; i < mutationsList.length; i++) {
cb(mutationsList[i])
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config)
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment