如何监控浏览器中发出的所有自定义事件?
我想监视网络浏览器中触发的所有自定义事件。任何标准浏览器都可以。
需要明确的是,我知道您可以附加事件处理程序来查看何时触发“通常”事件,但如何可靠地检测嵌入对象或 jQuery 脚本是否触发自定义事件?
我可以重构浏览器源代码来挂钩事件循环,但这似乎相当极端。
I'd like to monitor all custom events fired within a web browser. Any standard browser will do.
To be clear, I know you can attach event handlers to see when "usual" events are fired, but how can I reliably detect if an embedded object or jQuery script fires a custom event?
I could refactor the browser source code to hook the event loop, but that seems rather extreme.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能。 DOM 事件模型通过为特定事件类型设置侦听器来工作,因此如果您不知道事件的类型,则无法侦听它。没有办法监听所有事件,例如没有
addEventListener('*',...)
。另外,您不知道如何调用自定义事件。它们可能不会将事件分派到 DOM 中(例如,某些库实现了自己的事件注册和处理系统),因此即使您可以跟踪事件的分派,也没有通用方法可以知道事件侦听器何时被调用。
有些库还模拟事件冒泡,但同样,除非您知道事件的类型,否则无法侦听它。
但是,您可以实现自己的事件管理系统,并实现一个函数来侦听所有已设置侦听器的事件或使用您的系统分派的事件。
I don't think you can. The DOM event model works by setting listeners for specific event types, so if you don't know the type of the event, you can't listen for it. There is no way to listen for all events, e.g. there is no
addEventListener('*',...)
.Also, you don't know how custom events are called. They may not dispatch an event into the DOM (e.g. some libraries implement their own event registration and handling systems) so there is no general way of knowing when event listeners are being called, even if you can track the dispatch of the event.
Some libraries also simulate event bubbling, but again, unless you know the type of event, you can't listen for it.
However, you could implement your own event management system and implement a function to listen for all events for which listeners are set or events dispatched using your system.