npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-listener-helper

v1.1.3

Published

This library allows you to get a list of event listeners attached to the target node, confirms the existence of event listener registered on the target node, deletes all event listeners registered on the target node, registers event listeners with listene

Downloads

635

Readme

event-listener-helper

npm version CircleCI codecov Codacy Badge

This library allows you to:

  • Get a list of event listeners attached to the target node
  • Confirms the existence of event listener registered on the target node
  • Deletes all event listeners registered on the target node
  • Registers event listeners with name (rather than a reference)

These benefits can be received by calling addEventListener and removeEventListener via this library.

MIT License

Examples

  • Add named event listener to the button element
  const eh = new EventListenerHelper();

  const btn = document.querySelector('#myButton');

  eh.addEventListener(btn, 'click', () => {
    alert('Hello!');
  }, { listenerName: 'my-listener' });
  • Remove named event listener from the button element
  const eh = new EventListenerHelper();

  const btn = document.querySelector('#myButton');

  eh.addEventListener(btn, 'click', () => {
    alert('Hello!');
  }, { listenerName: 'my-listener' });

  eh.clearEventListener(btn, 'click', 'my-listener');
    
  • Get registered listeners on the button element
eh.getEventListeners(btn, 'click');
  • Check the existence of a listener who has already registered
eh.hasEventListeners(btn, 'click');

Demo

https://riversun.github.io/event-listener-helper/

How to install

  • NPM
npm install event-listener-helper

or

  • use <script> tag from CDN
 <script src="https://cdn.jsdelivr.net/npm/event-listener-helper/lib/event-listener-helper.js"></script>

API Details

JSDoc here

EventListenerHelper

Kind: global class
Author: Tom Misawa ([email protected],https://github.com/riversun)

new EventListenerHelper()

This library allows you to: get a list of event listeners attached to the target node, confirms the existence of event listener registered on the target node, deletes all event listeners registered on the target node, registers event listeners with name (rather than a reference). These benefits can be received by calling addEventListener and removeEventListener through this library.

MIT License

eventListenerHelper.addEventListener(eventTarget, eventType, listener, [options]) ⇒

Kind: instance method of EventListenerHelper

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | eventType | String | A case-sensitive string representing the event type to listen for. | | listener | function | The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function. See The event listener callback for details on the callback itself. | | [options] | Object | An options object specifies characteristics about the event listener. The available options are: listenerName A StringBy assigning listenerName, the specified listener function (callback function) can be specified.In other words, it is possible to retrieve the listener function later using this listenerName as a key.listenerName must be unique. capture A Boolean indicating that events of this type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. once A Boolean indicating that the listener should be invoked at most once after being added. If true, the listener would be automatically removed when invoked. addEventListener by Mozilla Contributors is licensed under CC-BY-SA 2.5. |

eventListenerHelper.removeEventListener(eventTarget, eventType, [listener], [options]) ⇒

The EventListenerHelper#removeEventListener method removes from the EventTarget an event listener previously registered with EventListenerHelper#addEventListener. The event listener to be removed is identified using option. listenerName and a combination of the event type, the event listener function itself, and various optional options that may affect the matching process; see Matching event listeners for removal

Kind: instance method of EventListenerHelper

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | eventType | String | A string which specifies the type of event for which to remove an event listener. | | [listener] | function | (Either the listener or options.listenerName must be specified. If both are specified, options.listenerName takes precedence.) The object which receives a notification (an object that implements the Event interface) when an event of the specified type occurs. This must be an object implementing the EventListener interface, or a JavaScript function. See The event listener callback for details on the callback itself. | | [options] | Object | (Either the listener or options.listenerName must be specified. If both are specified, options.listenerName takes precedence.) An options object specifies characteristics about the event listener. The available options are: listenerName A StringBy assigning listenerName, the specified listener function (callback function) can be specified.In other words, it is possible to retrieve the listener function later using this listenerName as a key.listenerName must be unique. capture A Boolean indicating that events of this type will be dispatched to the registered listener before being dispatched to any EventTarget beneath it in the DOM tree. removeEventListener by Mozilla Contributors is licensed under CC-BY-SA 2.5. |

eventListenerHelper.getEventListeners(eventTarget, [eventType]) ⇒

Get a listener definition matching the specified eventTarget and eventType (optional). Please note that the return value is immutable.

Kind: instance method of EventListenerHelper
Returns: Example of the returned value when only eventTarget is specified: [ { eventType:click, listener:[ { listener:func, options:{ listenerName:my-test-listener-1 } }, { listener:func, options:{ capture:true, listenerName:my-test-listener-2 } } ] }, { eventType:mousemove, listener:[ { listener:func, options:{ once:true, listenerName:my-test-listener-3 } } ] } ] Example of returned value when eventType is also specified as an argument: [ { options:{ listenerName:my-test-listener-1 }, listener:func1 }, { options:{ capture:true, listenerName:my-test-listener-2 }, listener:func2 }, { options:{ once:true, listenerName:my-test-listener-3 }, listener:func3 } ]

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | [eventType] | String | A case-sensitive string representing the event type to listen for. |

eventListenerHelper.getAllEventListeners() ⇒

You can get listeners for "inputElement" 's "click" event by map chain.

Kind: instance method of EventListenerHelper
Returns: const listeners=result.get(inputElement).get('click');

eventListenerHelper.getEventListener(eventTarget, eventType, listenerName) ⇒ function

Get a listener with the specified eventTarget, eventType and listenerName. The listenerName must be unique for one eventTarget and eventType combination, but it does not have to be unique for different eventTargets or different eventTypes.

Kind: instance method of EventListenerHelper
Returns: function - Returns null if no listener function is found

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | eventType | String | A case-sensitive string representing the event type to listen for. | | listenerName | String | The listener name of the listener you want to find |

eventListenerHelper.hasEventListeners(eventTarget, eventType) ⇒ boolean

Returns whether or not there are more than one event listener for the given eventTarget and eventType.

Kind: instance method of EventListenerHelper

| Param | | --- | | eventTarget | | eventType |

eventListenerHelper.hasEventListener(eventTarget, eventType, listenerName) ⇒ boolean

Returns whether a listenerName exists for the specified eventTarget and eventType.

Kind: instance method of EventListenerHelper

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | eventType | String | A case-sensitive string representing the event type to listen for. | | listenerName | String | The listener name of the listener you want to find |

eventListenerHelper.clearAllEventListeners()

Removes all registered events through the addEventListener method.

Kind: instance method of EventListenerHelper

eventListenerHelper.clearEventListeners(eventTarget, [eventType])

Remove all listeners matching the specified eventTarget and eventType (optional).

Kind: instance method of EventListenerHelper

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | [eventType] | String | A case-sensitive string representing the event type to listen for. |

eventListenerHelper.clearEventListener(eventTarget, [eventType], listenerName)

Removes the eventListener with eventTarget, eventType, and listenerName as arguments. The functions are the same as those of removeEventListener, except for the way to give arguments.

Kind: instance method of EventListenerHelper

| Param | Type | Description | | --- | --- | --- | | eventTarget | EventTarget | EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them. EventTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5. | | [eventType] | String | A case-sensitive string representing the event type to listen for. | | listenerName | String | The listener name of the listener you want to find |

eventListenerHelper.getAllEventTargets() ⇒

Get all registered eventTargets through the #addEventListener method.

Kind: instance method of EventListenerHelper

eventListenerHelper.searchEventListenersByName(listenerName) ⇒

Get all listeners(listener definition) with a given listenerName. Since listeners need only be unique to the eventTarget and eventType, it is possible to have the same listenerName for different eventTargets and eventTypes.

Kind: instance method of EventListenerHelper
Returns: [ { options: { listenerName: 'my-test-listener' }, listener: [Function: func] }, { options: { capture: true, listenerName: 'my-test-listener' }, listener: [Function: func] }, { options: { once: true, listenerName: 'my-test-listener' }, listener: [Function: func] }, { options: { once: true, listenerName: 'my-test-listener' }, listener: [Function: func] } ]

| Param | Type | Description | | --- | --- | --- | | listenerName | String | The listener name of the listener you want to find |