With (Tamper|Violent|Grease)monkey, you can run any JavaScript code on any web pages. This allows you to rewrite the content and style of web pages and override their behaviors. However, manipulating the event listeners is a bit more difficult. In particular, to disable or delete a registered event listener, you must have a reference to the event listener function object, which is usually not available. Chrome DevTools (or similar browser developer functionalities) can do this, but in any case it is not available from Tampermonkey.
This code snippet will help you to remove event listeners by inserting a hook into EventTarget.proptotype.addEventListener
, the standard method for registering event listeners, and logging all calls to it. For example, to remove all click
events, you can use the following code:
//...
(function() {
'use strict';
window.addEventListener('load', () => {
// remove all click event listeners
for (const eventTarget in eventListeners) {
if ('click' in eventListeners[eventTarget]) {
for (const args of eventListeners[eventTarget]['click']) {
eventTarget.removeEventListener('click', ...args)
}
}
}
})
injectAddEventListenerHook(/^click$/)
})()
The key of the eventListeners
object is EventTarget
; i.e. such as Element
. You can remove the event listeners for specific HTML elements as follows:
const eventTarget = document.querySelector('a.special-button')
if (eventTarget in eventListeners && 'click' in eventListeners[eventTarget]) {
for (const args of eventListeners[eventTarget]['click']) {
eventTarget.removeEventListener('click', ...args)
}
}
This hack can easily be defeated by caching the original EventTarget.prototype.addEventListener
function in the web page's own script. However, I have never found a web page that does so.