-
-
Save philfreo/902684 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Gmail "From" Address auto-selector | |
// @version 0.18 | |
// @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 ignoreGmail = true; | |
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]; | |
if (ignoreGmail && domain.toLowerCase() === 'gmail.com') { | |
return; | |
} | |
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