O'Reilly logo

Greasemonkey Hacks by Mark Pilgrim

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #62. Warn Before Sending Gmail Messages with Missing Attachments

Never again forget to attach a file to your email.

It's too easy to forget to attach files when sending email. You send off the message, and a few minutes later you get a puzzled reply asking, "Was there supposed to be a file with this?"

Some desktop mail applications use heuristics to check for this condition and prompt you before sending your message. However, I have never seen this incorporated in a web mail application. Using Greasemonkey, we can guess with pretty good accuracy that a message was supposed to contain an attachment if it contains words such as attachment or files. If such a message actually contains no files, then we can show a warning before sending the message.

The Code

This user script runs in both the Reply and Compose frames in Gmail. These can be identified by their view=cw and view=page query string parameters.

The script listens for click events on the Send buttons. When the user clicks one of them, the script gets the contents of the message text box and scans each line of the message for any occurrences of specific keywords. Since messages can contain quoted text copied from a previous message, we intentionally ignore lines that start with >.

If we find any occurrences of attachment or files, we check whether there are any attachments. If there are no attachments, we prompt the user to confirm that she really wants to send the message.

Tip

You can access forms, form elements, images, and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required