6

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
  • You could refactor the browser source code if you wanted to? Respect `:)` – Šime Vidas May 13 '11 at 00:50
  • Check out [this question](http://stackoverflow.com/questions/5107232/is-it-possible-to-programmatically-catch-all-events-on-the-page-in-the-browser) - it's about regular event types, but it might be useful... – Šime Vidas May 13 '11 at 00:52

1 Answers1

6

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.

RobG
  • 142,382
  • 31
  • 172
  • 209