Hack 61. Warn Before Replying to Multiple Recipients in Gmail

 < Day Day Up > 

Don't embarrass yourself by sending private replies to everyone.

Using any email program, it's all too easy to accidentally hit the Reply All button and end up saying something to a large group that was meant for just one person. But the problem isn't limited to the Reply All button. If there are multiple people in the To: list, it's even easier to accidentally reply to them all, because the Reply button replies to everyone by default.

7.3.1. The Code

This user script runs in the Compose frame of Gmail, which can be identified by its query string parameter view=cv.

Use the View Frame Info menu command in the context menu to see the URL of a <frame> or <iframe>.


The script uses the following algorithm to detect a possible reply-all snafu:

  1. Listen for all click events on the page by using document.addEventListener.

  2. If the click event originated from a Send button, check the number of recipients. Recipients are usually separated by a comma followed by a space, but since you can type any amount of space around the comma, this script uses a regular expression.

  3. If there is more than one recipient, warn the user.

  4. If the user decides not to proceed, cancel the form submission. The script accomplishes this by calling the event object's stopPropagation method, which prevents the event from bubbling up to the <form> element where it would submit the form and send the message.

You can use addEventListener at any level in the document tree. Sometimes, listening at a high level and filtering by the target property is easier than finding a specific element and attaching an event listener to it.


Save the following user script as dontreplyall.user.js:

 // ==UserScript== // @name  Don't Reply-All // @namespace    http://youngpup.net/ // @description  Warn before replying to multiple recipients in Gmail // @include      http*://mail.google.com/mail/?*&view=cv* // ==/UserScript== // based on code by Aaron Boodman // and included here with his gracious permission var recipient_separator = /\s*\,\s*/g; document.addEventListener("click", function(e) { if (e.target.id == "send") { var form = document.getElementById("compose_form"); var to = removeEmptyItems( form.elements.namedItem('to').value.split(recipient_separator));  var cc = removeEmptyItems(  form.elements.namedItem('cc').value.split(recipient_separator));  var bcc = removeEmptyItems(  form.elements.namedItem('bcc').value.split(recipient_separator)); if ((to.length + cc.length + bcc.length) > 1) {   if (!confirm("WARNING!\n" +     "Do you really want to reply to all these people?\n\n" +     "To: " + to.join(", ") + "\n" +     "CC: " + cc.join(", ") + "\n" +     "BCC: " + bcc.join(", "))) {  e.stopPropagation();   }    } } }, true); function removeEmptyItems(arr) { var result = []; for (var i = 0, item; item = arr[i]; i++) { if (/\S/.test(item)) {    result.push(item); } } return result; } 

7.3.2. Running the Hack

After installing the user script (Tools Install This User Script), log into Gmail at http://mail.google.com and open any message. Replace the To: field with multiple test addresses and press Send. The script will display a dialog to confirm that you want to send your message to multiple recipients, as shown in Figure 7-3.

Figure 7-3. Confirmation message before replying to multiple recipients


If you hit OK, Gmail will send the message as usual. If you hit Cancel, you will stay in the message composition window and can edit the To: or Cc: list to trim the number of recipients.

Aaron Boodman

     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net