Skip to content

Instantly share code, notes, and snippets.

@9point6
Last active August 29, 2015 14:16

Revisions

  1. 9point6 revised this gist Mar 11, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions event-debugger.js
    Original file line number Diff line number Diff line change
    @@ -4,16 +4,28 @@
    '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);
    };
    @@ -32,5 +44,6 @@
    attachTo[i].prototype.addEventListener = addEventListenerFunc;
    attachTo[i].prototype.getEventListener = getEventListenerFunc;
    attachTo[i].prototype.__eventListeners = {};
    attachTo[i].prototype.__skippedListeners = {};
    }
    })();
  2. 9point6 renamed this gist Mar 11, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. 9point6 created this gist Mar 11, 2015.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    (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 = {};
    }
    })();