Hack 38. Automatically Log into Web Mail and Other Sites

 < Day Day Up > 

Automate the hassle of using web-based login forms.

Firefox has an option to remember usernames and passwords in login forms. But even when it remembers your login and autofills the form, you're still left with one last click to submit the form and log into the site. This is definitely an improvement over needing to remember the password you used for each site, but over time, it can still be annoying, since most sites will force you to reenter your password once or twice a week. This hack works in conjunction with Firefox's autofill capabilities to autosubmit these autofilled login forms.

4.11.1. The Code

This user script runs on all pages. It looks for the first form that contains a text field marked as a password field (<input type="password">), and checks whether the password field contains an autofilled value. If so, it simulates a click on the form's Submit button to automatically log into the site.

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

 // ==UserScript== // @name   AutoLoginJ // @namespace   http://www.squarefree.com/userscripts // @description   Automatically submit autofilled login forms // @include   * // ==/UserScript== // based on code by Jesse Ruderman // and included here with his gracious permission // http://www.squarefree.com/userscripts/autologinj.user.js function submitFirstPasswordForm() { for (var elmForm, i=0; elmForm=document.forms[i]; ++i) { var numPasswordElements = 0; for (var j=0; elmFormElement=elmForm.elements[j]; ++j) if (elmFormElement.type == "password" && elmFormElement.value && elmFormElement.value.toLowerCase() != "password") { ++numPasswordElements; } if (numPasswordElements != 1) { continue; } /*  * The obvious way to submit a login form is form.submit().  * However, this doesn't work with some forms, such as  * the Google AdWords login, because they do stuff  * in the onclick handler of the submit button. So we  * need to find the submit button and simulate a click.  */ var elmSubmit = document.evaluate(".//input[@type='image']", elmForm, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; if (!elmSubmit) { elmSubmit = document.evaluate(".//input[@type='submit']", elmForm, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;    }    if (!elmSubmit) { continue; }    /* * Give a visual indication that we're auto-submitting the * form, then simulate a click on the submit button. */    elmSubmit.focus();    elmSubmit.style.MozOutline = "2px solid purple";    elmSubmit.click(); } } window.addEventListener("load", function() { /*  * Using setTimeout to give Firefox's password manager a chance  * to autofill the form.  */ setTimeout(submitFirstPasswordForm, 0); }, false); 

4.11.2. Running the Hack

For this hack, I will use Passport as an example, but the user script works on any site that uses a form-based login. This hack works only if you have told Firefox to remember your password. If you have previously told Firefox not to save your Yahoo! Mail password, go to Tools Options Privacy Saved Passwords View Saved Passwords Passwords Never Saved and remove Passport from the list of sites for which Password Manager will never save login information.

Furthermore, many sites use a proprietary HTML attribute to tell your browser not to offer the choice of remembering your login information. Such sites will defeat the purpose of this user script, which works only after the browser autofills the login form with saved information. You can use "Allow Password Remembering" [Hack #32] to fight back against such sites and allow your browser to remember your login information, and then use this script to automatically log in after the login form is autofilled.

Go to http://login.passport.net/ and log into Microsoft Passport. Firefox will ask you whether you want to save the username and password for this site, as shown in Figure 4-13. Click Yes to remember your Passport login information.

Now log out of Passport and install the user script from Tools Install This User Script. Revisit or refresh http://login.passport.net/. You should see the login page, and then Firefox will automatically fill in your saved Passport username and password. Shortly after, the script will automatically submit the login form.

Figure 4-13. Remember this password?


     < 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