主要使用用了js的MutationObserver的API接口提供了监视对DOM树来实现页面DOM的监听
MutationObserver文档: MutationObserver.
废话不多说上代码
```javascript
// 阻止后续DOM加载
class StopLoadingDOM {
stop(DOM = 'body', stop_time = 60000) { //使用了ES6的默认参数 默认是监听整个body
// Firefox和Chrome早期版本中带有前缀
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
// 创建观察者对象
const observer = new MutationObserver((mutations, e)=> {
mutations.forEach((mutation)=> {
mutation.addedNodes.forEach(e => e.remove()); //后期注入的DOM元素进行删除
});
});
// 配置观察选项:
var config = {
attributes: true,
childList: true,
characterData: true,
attributeOldValue: true,
characterDataOldValue: true,
attributeOldValue: true,
subtree: true
};
// 选择目标节点
const target = document.querySelector(DOM);
if (target) {
// 传入目标节点和观察选项
target && observer.observe(target, config);
// 随后,你还可以停止观察
setTimeout(() => { observer.disconnect(); }, stop_time);
} else {
throw new EvalError(`The incoming DOM was not found`);
}
}
}