Skip to content

Instantly share code, notes, and snippets.

@9point6
Last active August 29, 2015 14:16
Show Gist options
  • Save 9point6/8d972e20fc53b97129a9 to your computer and use it in GitHub Desktop.
Save 9point6/8d972e20fc53b97129a9 to your computer and use it in GitHub Desktop.
Event Debugging Shim - allows limiting and skipping of event listeners as well as inspection of attached listeners
(function () {
// Limit the number of events to be attached
var limits = {
'keydown': 100
};
var addEventListenerFunc = function(type, handler, useCapture) {
if (!this.__eventListeners[type]) {
this.__eventListeners[type] = [];
}
// die if the limits are reached for this type
if (this.__eventListeners[type].length >= limits[type]) {
return;
}
this.__eventListeners[type].push(handler.toString());
return this._addEventListener.apply(this, arguments);
};
var getEventListenerFunc = function(type) {
return this.__eventListeners[type];
};
// setup the DOM and window objects
var attachTo = [0, HTMLDocument, Element, window];
for (var i in attachTo) {
if (!attachTo[i].prototype) {
continue;
}
attachTo[i].prototype._addEventListener = attachTo[i].prototype.addEventListener;
attachTo[i].prototype.addEventListener = addEventListenerFunc;
attachTo[i].prototype.getEventListener = getEventListenerFunc;
attachTo[i].prototype.__eventListeners = {};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment