我想要猴子补丁事件侦听器注册。
I found 这个答案展示如何做到这一点addEventListener
:
const nativeEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(...args) {
if (this.matches('div') && args[0] === 'click') {
console.log('listener is being added to a div');
debugger;
}
nativeEventListener.apply(this, args);
}
// then, when an event listener is added, you'll be able to intercept the call and debug it:
document.querySelector('div').addEventListener('click', () => {
console.log('clicked');
});
但这不会涵盖onclick
, onkeydown
等作业。
我不知道如何对那些做同样的事情,因为
const nativeOnClick = HTMLElement.prototype.onclick;
抛出类型错误
TypeError: 'get onclick' called on an object that does not implement interface HTMLElement.
现在想知道有没有特殊的方法可以找回具体来说setter 和 getter 的onclick
等等,但到目前为止我的谷歌搜索还没有运气。
您可以通过以下方式获得原始的 setter 函数:
const originalSetter = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'onchange').set;
如果你想重新定义一个属性,你应该考虑使用Object.defineProperty().
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)