Last active
June 30, 2023 11:53
-
-
Save pratheeshrussell/7cfce006f03fa72eecaa9ecd786b4e47 to your computer and use it in GitHub Desktop.
Monkey patching event listeners
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Event Listener Interceptor</title> | |
<script> | |
// Monkey patching the addEventListener method of HTMLElement prototype | |
(function() { | |
var originalAddEventListener = HTMLElement.prototype.addEventListener; | |
HTMLElement.prototype.addEventListener = function(type, listener, options) { | |
var patchedListener = function(event) { | |
if(type=='click'){ | |
console.log('Click Event intercepted:', event); | |
} | |
listener.call(this, event); | |
}; | |
originalAddEventListener.call(this, type, patchedListener, options); | |
}; | |
})(); | |
</script> | |
</head> | |
<body> | |
<button id="myButton">Click Me</button> | |
<input type="text" id="myInput"> | |
<select id="mySelect"> | |
<option>Option 1</option> | |
<option>Option 2</option> | |
</select> | |
<script> | |
// Adding event listeners for different event types | |
document.getElementById('myButton').addEventListener('click', function(event) { | |
console.log('Button clicked!'); | |
}); | |
document.getElementById('myInput').addEventListener('input', function(event) { | |
console.log('Input value changed!'); | |
}); | |
document.getElementById('mySelect').addEventListener('change', function(event) { | |
console.log('Select option changed!'); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment