|
| 1 | +// Taken and modified from https://github.com/colxi/getEventListeners to be compiled into ES5, allowing running in older browsers |
| 2 | + |
| 3 | +(function() { |
| 4 | + 'use strict'; |
| 5 | + |
| 6 | + // save the original methods before overwriting them |
| 7 | + Element.prototype['_addEventListener'] = Element.prototype.addEventListener; |
| 8 | + Element.prototype['_removeEventListener'] = Element.prototype.removeEventListener; |
| 9 | + |
| 10 | + Element.prototype.addEventListener = function<K extends keyof ElementEventMap>( |
| 11 | + type: K, |
| 12 | + listener: (this: Element, ev: ElementEventMap[K]) => any, |
| 13 | + options?: boolean | AddEventListenerOptions |
| 14 | + ): void { |
| 15 | + if (options === undefined) options = false; |
| 16 | + |
| 17 | + // declare listener |
| 18 | + this._addEventListener(type, listener, options); |
| 19 | + |
| 20 | + if (!this.eventListenerList) this.eventListenerList = {}; |
| 21 | + if (!this.eventListenerList[type]) this.eventListenerList[type] = []; |
| 22 | + |
| 23 | + // add listener to event tracking list |
| 24 | + this.eventListenerList[type].push({ |
| 25 | + type: type, |
| 26 | + listener: listener, |
| 27 | + useCapture: options, |
| 28 | + }); |
| 29 | + }; |
| 30 | + |
| 31 | + Element.prototype.removeEventListener = function<K extends keyof ElementEventMap>( |
| 32 | + type: K, |
| 33 | + listener: (this: Element, ev: ElementEventMap[K]) => any, |
| 34 | + options?: boolean | EventListenerOptions |
| 35 | + ): void { |
| 36 | + if (options === undefined) options = false; |
| 37 | + |
| 38 | + // remove listener |
| 39 | + this._removeEventListener(type, listener, options); |
| 40 | + |
| 41 | + if (!this.eventListenerList) this.eventListenerList = {}; |
| 42 | + if (!this.eventListenerList[type]) this.eventListenerList[type] = []; |
| 43 | + |
| 44 | + // Find the event in the list, If a listener is registered twice, one |
| 45 | + // with capture and one without, remove each one separately. Removal of |
| 46 | + // a capturing listener does not affect a non-capturing version of the |
| 47 | + // same listener, and vice versa. |
| 48 | + for (let i = 0; i < this.eventListenerList[type].length; i++) { |
| 49 | + if ( |
| 50 | + this.eventListenerList[type][i].listener === listener && |
| 51 | + this.eventListenerList[type][i].useCapture === options |
| 52 | + ) { |
| 53 | + this.eventListenerList[type].splice(i, 1); |
| 54 | + break; |
| 55 | + } |
| 56 | + } |
| 57 | + // if no more events of the removed event type are left,remove the group |
| 58 | + if (this.eventListenerList[type].length == 0) delete this.eventListenerList[type]; |
| 59 | + }; |
| 60 | + |
| 61 | + Element.prototype.getEventListeners = function<K extends keyof ElementEventMap>(type?: K) { |
| 62 | + if (!this.eventListenerList) this.eventListenerList = {}; |
| 63 | + |
| 64 | + // return requested listeners type or all them |
| 65 | + if (type === undefined) return this.eventListenerList; |
| 66 | + return this.eventListenerList[type]; |
| 67 | + }; |
| 68 | + |
| 69 | + /* |
| 70 | + Element.prototype.clearEventListeners = function(a){ |
| 71 | + if(!this.eventListenerList) |
| 72 | + this.eventListenerList = {}; |
| 73 | + if(a==undefined){ |
| 74 | + for(var x in (this.getEventListeners())) this.clearEventListeners(x); |
| 75 | + return; |
| 76 | + } |
| 77 | + var el = this.getEventListeners(a); |
| 78 | + if(el==undefined) |
| 79 | + return; |
| 80 | + for(var i = el.length - 1; i >= 0; --i) { |
| 81 | + var ev = el[i]; |
| 82 | + this.removeEventListener(a, ev.listener, ev.useCapture); |
| 83 | + } |
| 84 | + }; |
| 85 | + */ |
| 86 | +})(); |
0 commit comments