Skip to content

Instantly share code, notes, and snippets.

@9point6
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
};
// Skip this number of events
var skips = {
'keydown': 1
}
var addEventListenerFunc = function(type, handler, useCapture) {
if (!this.__eventListeners[type]) {
this.__eventListeners[type] = [];
this.__skippedListeners[type] = 0;
}
// die if the limits are reached for this type
if (this.__eventListeners[type].length >= limits[type]) {
return;
}
// die if the skipping this type
if (this.__skippedListeners[type] < skips[type]) {
this.__skippedListeners[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 = {};
attachTo[i].prototype.__skippedListeners = {};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment