How can I monitor all custom events emitted in the browser?

☆樱花仙子☆ 提交于 2019-12-13 14:48:05

问题


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.


回答1:


I'd like to monitor all custom events fired within a web browser.

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.



来源:https://stackoverflow.com/questions/5984551/how-can-i-monitor-all-custom-events-emitted-in-the-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!