Created
April 8, 2020 20:36
-
-
Save cScarlson/875a9fca7ab7084bb608fb66adff0463 to your computer and use it in GitHub Desktop.
Listen for any event on any target.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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
How to get all events from https://github.com/clappr/clappr in * .