Skip to content

Instantly share code, notes, and snippets.

@cScarlson
Created April 8, 2020 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cScarlson/875a9fca7ab7084bb608fb66adff0463 to your computer and use it in GitHub Desktop.
Save cScarlson/875a9fca7ab7084bb608fb66adff0463 to your computer and use it in GitHub Desktop.
Listen for any event on any target.
clear();
/**
* @param : source := EventTarget
* * EventTarget.prototype
* * Node (Element, Attr, etc)
* @usage : [Node].addEventListener('*', ({ detail: e }) => {...}, false);
*/
function proxyEventTargetSource(source) {
var emit = source.dispatchEvent; // obtain reference
function proxy(event) {
var { type } = event, any = new CustomEvent('*', { detail: event }); // use original event as detail
if (!{ '*': true }[ type ]) emit.call(this, any); // only emit "any" if type is not any.type ('*')
return emit.call(this, event);
}
source.dispatchEvent = proxy; // attempt overwrite
return (source.dispatchEvent === proxy); // indicate if its set after we try to
}
// proxyEventTargetSource(EventTarget.prototype); // all targets
proxyEventTargetSource(document); // single target
var e = new CustomEvent('any!', { detail: true });
document.addEventListener('*', (e) => console.log('type: %s, original: %s, e: %O', e.type, e.detail.type, e), false);
document.dispatchEvent(e);
@vishalkalola1
Copy link

How to get all events from https://github.com/clappr/clappr in * .

@pwFoo
Copy link

pwFoo commented Apr 13, 2021

Hi @cScarlson, should it work for really any event or just custom events? Ignores click events and so on?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment