Skip to content

Instantly share code, notes, and snippets.

@asuth
Created February 10, 2011 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save asuth/819654 to your computer and use it in GitHub Desktop.
Save asuth/819654 to your computer and use it in GitHub Desktop.
Looks in your "To" field and sees if you have an email address in your "From" addresses with the same domain. Changes <select> onblur
// ==UserScript==
// @name Gmail "From" Address auto-selector
// @version 0.17
// @description Looks in your "To" field and sees if you have an email address in your "From" addresses with the same domain. Changes <select> onblur
// @author Andrew Sutherland, https://github.com/asuth
// @include http://mail.google.com/*
// @include https://mail.google.com/*
// @include http://*.mail.google.com/*
// @include https://*.mail.google.com/*
// ==/UserScript==
(function() {
var blurHandler = function() {
var domains = {'' : 0},
mostCommon = '';
// parse email addresses in TO: field, find most common domain
this.value.split(',').forEach(function(str) {
var addr_part = str.split('@');
if (addr_part.length > 1) {
var domain = addr_part[1].split('>')[0];
domains[domain] = domains.hasOwnProperty(domain) ? domains[domain]+1 : 1;
if (domains[domain] > domains[mostCommon])
mostCommon = domain;
}
});
if (mostCommon == '')
return;
var form = this,
c = 0;
while (form.nodeName != 'FORM' && c++ < 20)
form = form.parentNode;
if (!form.hasOwnProperty('from'))
return;
// select the first option with a matching domain name
var from_options = form.from.options;
for (var i = 0, m = from_options.length; i < m; i++) {
if (from_options[i].text.indexOf(mostCommon) > -1) {
from_options[i].selected = true;
break;
}
}
};
// wait for the compose field to show up
// never clear this interval because we might compose multiple
// emails and we have to target new copies of the form
var interval = setInterval(function() {
var forms = document.getElementsByTagName('form');
for (var i = 0, m = forms.length; i < m; i++) {
if (!forms[i].hasOwnProperty('to'))
continue;
// addEventListener discards exact duplicates, so don't worry about
// adding many times
forms[i].to.addEventListener('blur', blurHandler, false);
}
}, 1500);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment