Skip to content

Instantly share code, notes, and snippets.

@tcannonfodder
Created July 6, 2018 14:13
Show Gist options
  • Save tcannonfodder/5cd26b23cde76d071e8a8dc576eff5c6 to your computer and use it in GitHub Desktop.
Save tcannonfodder/5cd26b23cde76d071e8a8dc576eff5c6 to your computer and use it in GitHub Desktop.
EventTarget.prototype._addEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(a,b,c) {
if(c==undefined)
c=false;
this._addEventListener(a,b,c);
if(!window.eventListenerList)
window.eventListenerList = {};
if(!window.eventListenerList[a])
window.eventListenerList[a] = [];
//this.removeEventListener(a,b,c); // TODO - handle duplicates..
// if(a == 'click'){debugger}
console.trace({event: a, element: this})
window.eventListenerList[a].push({listener:b,useCapture:c});
};
EventTarget.prototype.getEventListeners = function(a){
if(!window.eventListenerList)
window.eventListenerList = {};
if(a==undefined)
return window.eventListenerList;
return window.eventListenerList[a];
};
EventTarget.prototype.clearEventListeners = function(a){
if(!window.eventListenerList)
window.eventListenerList = {};
if(a==undefined){
for(var x in (this.getEventListeners())) this.clearEventListeners(x);
return;
}
var el = this.getEventListeners(a);
if(el==undefined)
return;
for(var i = el.length - 1; i >= 0; --i) {
var ev = el[i];
this.removeEventListener(a, ev.listener, ev.useCapture);
}
};
EventTarget.prototype._removeEventListener = Element.prototype.removeEventListener;
EventTarget.prototype.removeEventListener = function(a,b,c) {
if(c==undefined)
c=false;
this._removeEventListener(a,b,c);
if(!window.eventListenerList)
window.eventListenerList = {};
if(!window.eventListenerList[a])
window.eventListenerList[a] = [];
// Find the event in the list
for(var i=0;i<window.eventListenerList[a].length;i++){
if(window.eventListenerList[a][i].listener==b, window.eventListenerList[a][i].useCapture==c){ // Hmm..
window.eventListenerList[a].splice(i, 1);
break;
}
}
if(window.eventListenerList[a].length==0)
delete window.eventListenerList[a];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment