By D Rickard
—
Last update
Sep 19, 2005
—
Installed
4,550 times.
/*
--------------------------------------------------------------------------------
Yahoo Mail Login Assistant
--------------------------------------------------------------------------------
This is a Greasemonkey user script. To install it, you need
Greasemonkey 0.3 or later: http://greasemonkey.mozdev.org/
Then restart Firefox and revisit this script.
Under Tools, there will be a new menu item to "Install User Script".
Accept the default configuration and install.
--------------------------------------------------------------------------------
This script:
1. Ensures that you log in to Yahoo using a secure connection.
2. Saves a "drop-down" list of Yahoo usernames to select from on the login page.
Any time a new name is used, it gets automatically added to the list. Each
new username entered is added to the list. See the comment below marked by
"TO CLEAR THE USERNAME LIST" for instructions on how to clear the list of
usernames.
3. Does NOT save any password information.
--------------------------------------------------------------------------------
*/
// ==UserScript==
// @name Yahoo Mail Login Assistant
// @description Provides a drop-down list of Yahoo mail usernames and forces a secure login.
// @include *mail.yahoo.com*
// @include *login.yahoo.com*
// ==/UserScript==
(function() {
//==============================================================================
// Make the login secure.
//==============================================================================
// Is a non-secure Yahoo mail URL being used?
if ((window.location.href.indexOf('http://mail.yahoo.com') == 0)
&& (window.location.href.indexOf('logout=1') == -1)) {
// Switch to the secure URL for Yahoo mail.
window.location="https://login.yahoo.com/config/login?.src=ym";
// Is a non-secure Yahoo login URL being used?
} else if ((window.location.href.indexOf('http://login.yahoo.com') == 0)
&& (window.location.href.indexOf('logout=1') == -1)) {
// Switch to the secure URL for Yahoo login.
window.location="https://login.yahoo.com";
}
//==============================================================================
// Create and manage the usernames.
//==============================================================================
/* TO CLEAR THE USERNAME LIST:
* To clear the list of usernames, uncomment this next line and login to Yahoo.
* Then re-comment this line and login to Yahoo.
*/
// GM_setValue('uNameArray', '');
/**
* Add our style sheet to the current page.
*
* @param css the style sheet to add
*/
function infectStyles(css) {
var h = document.getElementsByTagName('head')[0];
if (!h) {
return;
}
var s = document.createElement('style');
s.type = 'text/css';
s.innerHTML = css;
h.appendChild(s);
}
/**
* Draw the username "drop down" div.
*/
function drawNames(userNameArray) {
// Create a new div to be used as a username "drop down".
nameDiv = document.createElement('div');
nameDiv.setAttribute('id', 'nameDiv');
// Apply some style and placement attributes to the div.
with (nameDiv.style) {
position= "absolute";
// Make the username "drop down" a little wider than the username
// text box so that longer usernames do not overrun the div.
width = loginWidth + 20;
margin = "0 0 0 1px";
background = "#ffffff";
border = "1px solid #000000";
}
// Create the list of usernames from GreaseMonkey storage.
var nameList = document.createElement('ul');
nameList.setAttribute('id', 'nameList');
var listItem;
if (typeof(userNameArray.length) != 'undefined') {
for (var i = 0; i < userNameArray.length; i++) {
listItem = document.createElement('li');
listItem.setAttribute('class', 'listItem');
listItem.innerHTML = userNameArray[i];
nameList.appendChild(listItem);
}
}
nameDiv.appendChild(nameList);
return nameDiv;
}
/**
* Shift focus to the password text box.
*/
function focusPassword() {
// The id for the password box on the normal Yahoo login page is
// 'passwd'. On some other Yahoo login pages it's 'password'.
var passwordBox = document.getElementById('passwd');
if (!passwordBox) {
passwordBox = document.getElementById('password');
}
if (passwordBox) {
passwordBox.focus();
}
}
// Get the login text box.
var input = document.evaluate(
"//input[@name='login']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
var login = input.snapshotItem(0);
// Was the login prompt found?
if (!login) {
// No login prompt found. Just exit.
return;
}
// Apply the default username if it exists.
var loginDefault = GM_getValue('ymunDefault');
if (loginDefault) {
if (loginDefault != '') {
login.value = loginDefault;
}
}
// Get the width of the username text box.
var loginWidth = document.defaultView.getComputedStyle(login, '')
.getPropertyValue('width');
// Shade the username text box background.
with (login.style) {
background = "#f0ffff";
}
// Get the username array from GreaseMonkey storage.
var userNames = eval(GM_getValue('uNameArray'));
if (userNames) {
if (userNames != null && userNames != '') {
// Draw the username "drop down" div.
var nameDiv = drawNames(userNames);
// Place the username div under the login text box.
login.appendChild(nameDiv);
}
} else {
// There were no usernames stored yet. Start a new array to save the
// the username the user enters.
userNames = new Array();
}
// If the user starts typing in the username text box, remove the "drop-down".
login.addEventListener('keypress',
function(event) {
var removeMe;
if (removeMe = document.getElementById('nameDiv')) {
removeMe.parentNode.removeChild(removeMe);
with (login.style) {
background = "#ffffff";
}
}
},
false);
// Get all the usernames in the "drop-down".
var items = document.evaluate(
"//li[@class='listItem']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
// Add a listener for mouse clicks for each username in the "drop down".
for (var j = 0; j < items.snapshotLength; j++) {
items.snapshotItem(j).addEventListener('click',
function(event) {
login.value = event.target.innerHTML;
var removeMe = document.getElementById('nameDiv');
removeMe.parentNode.removeChild(removeMe);
with (login.style) {
background = "#ffffff";
}
focusPassword();
},
false);
}
// Add a function to be called when the login form is submitted.
// This will allow the storage of any usernames not already stored.
document.login_form.onsubmit = function() {
var addName = true;
var newName = login.value;
// Determine if the username entered needs to be stored.
if (userNames.length > 0 ) {
// Has the username alrady been stored?
for (var i = 0; i < userNames.length; i++) {
if (userNames[i] == newName){
addName = false;
break;
}
}
}
if (addName) {
userNames.unshift(newName);
}
// Set the just-used username as the new default username.
GM_setValue('ymunDefault', login.value);
// Re-store all the usernames.
GM_setValue('uNameArray', uneval(userNames));
}
// Add the style sheet to use for the username "drop down".
infectStyles('#nameDiv {'+
+ '-moz-border-radius: 6px;'
+ '}'
+ '#nameList li{'
+ 'margin: 2px 5px 2px -35px;'
+ 'padding-left: 4px;'
+ 'color: #3a3a3a'
+ '}'
+ '#nameList li:hover{'
+ 'background: #b6c7e5;'
+ 'color: #000000;'
+ 'cursor: pointer;'
+ '-moz-border-radius: 3px;'
+ '}'
+ 'ul#nameList {'
+ 'list-style-type: none ! important;'
+ 'margin: 0;'
+ 'font-family: arial, "ms trebuchet", sans-serif;'
+ '}'
);
})();