Search code examples
google-chromegmailgreasemonkeyuserscripts

Chrome Userscript (Greasemonkey) - Stop Gmail from sending an email


I am writing a Chrome Userscript (Greasemonkey) extension to display a confirmation dialog when the user clicks on Gmail's Send button (in the compose window etc.).

I have manged to attach to the click even of the button and show a dialog when the button is clicked, by using:

addEventListener("click", function(e) { ......... }, true);

But I cannot stop the email from being sent. I have tried using:

e.stopPropagation();
e.preventDefault();
return false;

How can I stop Gmail from sending the email?


Solution

  • I think that those mentioned by you can prevent default action that is built-in in browser, and stop propagation of event to parent elements in DOM hierarchy. You probably need to get the Gmail's event listener and do something with it - wrap it with your function (so, remove original event listener and bind your function, which displays a dialog and then invoke Gmail's one). Currently, when you only add an event listener, there are two independent event handlers.

    Those posts might be useful:
    How to find event listeners on a DOM node?
    How to check if any JavaScript event listeners/handlers attached to an element/document?