[whatwg/dom] [Proposal] Add EventTarget.getEventListeners() (#412)

Originally posted at WICG: https://discourse.wicg.io/t/proposal-eventtarget-prototype-geteventlisteners/2015

**Purpose:** This would provide a way to get all event listeners added to an element via `addEventListener`.

**Signature:** `element.getEventListeners([type])`

**Returns:** Array of objects. Each object contains properties for `type`, `callback`, `options` for the arguments that registered the corresponding event listener. Events registered via inline event handlers are not included.

**Use cases:** This would enable removing events based on arbitrary criteria, instead of requiring a reference to the callback, which causes unnecessary couplings. Typically libraries deal with this by providing their own abstractions for adding events that track the listeners manually. However, this is fragile, as it means listeners not registered via the library cannot be retrieved or removed. Some libraries deal with this by hijacking `addEventListener` to keep track of listeners, but this is very intrusive for a library and it doesn't help with any listeners registered before the library was included. Browsers already keep track of event listeners, so it should be relatively easy to expose them, and is on par with the Extensible Web Manifesto principle of exposing browser "magic" via JS APIs.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/412

Received on Thursday, 16 February 2017 21:06:00 UTC