Last active
August 29, 2015 14:16
-
-
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
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
(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