There's a Firefox extension which modifies the native preventDefault
function via
Object.defineProperty(Event.prototype, 'preventDefault', ...
before my JavaScript script executed. So, when I call preventDefault
function, the modified function would be called instead of the original one. The behavior of the function is changed by the extension.
For me, there's two way to bypass the extension:
Get the native preventDefault function before the extension executed, but my script always executed after the extension even I put my script in head tag.
Implement the preventDefault function by myself, but I don't know how to.
Or this's some alternative ways?
- Get the native preventDefault function before the extension executed, but my script always executed after the extension even I put my script in head tag.
This is not possible. The extension has chosen to run prior to your HTML being loaded (i.e. the extension is running at document_start
). Extensions get to run code first, if they want to. This is as the browser is designed/intended. If the extension changes the prototype, you will not have direct access to the function which has been overridden.
- Implement the preventDefault function by myself, but I don't know how to.
Basically, you can't. The .preventDefault()
method is the way that you can affect the browser-internal data structure that represents the event in a way that will cause the browser to not call the default action. Even removing the element from the DOM will not prevent the default action. There's no way for you to affect if the default action will be taken, or not, other than calling .preventDefault()
.
All you can do is detect that the .preventDefault()
method has been overridden and alert the user that your website does not function properly with that extension installed/active. In this case, you could, probably, detect that the .preventDefault()
method has been changed by comparing what is returned by Event.prototype.preventDefault.toString()
to what you expect it to return when the method has not been overridden.