AutoLoginJ

By Jesse Ruderman Last update Jun 19, 2005 — Installed 17,154 times. Daily Installs: 15, 28, 19, 18, 8, 16, 13, 13, 19, 12, 11, 20, 8, 10, 8, 10, 13, 14, 4, 10, 16, 9, 14, 9, 9, 15, 9, 7, 9, 15, 12, 8
// ==UserScript==
// @name          AutoLoginJ
// @namespace     http://www.squarefree.com/userscripts
// @description   Automatically submits login forms where Firefox has remembered the password.
// @include       *
// @include       https://adwords.google.com/select/*
// @include       https://secure.skype.com/store/member/login.html
// @include       https://panel.dreamhost.com/*
// ==/UserScript==

/*

  Author: Jesse Ruderman - http://www.squarefree.com/
  
  Version: 1.3
    1.0 - first version
    1.1 - fix some JS strict warnings
    1.2 - bonk the first submit button, not the last submit button (fixes e.g. delicious)
    1.3 - skip if password field's value is "Password" (fixes e.g. ddrfreak)

  Competing scripts and extensions:
   * http://labs.beffa.org/greasemonkey/AutoLogin.user.js
   * http://www.extensionsmirror.nl/index.php?showtopic=962

*/



function submitFirstPasswordForm()
{

  for (var form, i=0; form=document.forms[i]; ++i) {

    var numPasswordElements = 0;
    var submitButton = null;
    
    var formElement, j;
  
    for (j=0; formElement=form[j]; ++j)
      if (formElement.type == "password" && formElement.value && formElement.value.toLowerCase() != "password")
        ++numPasswordElements;
        
    if (numPasswordElements == 1) { // probably a login form (and not a change-password form or something like that)

      /*
       * 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.  Instead, find a 
       * submit button and bonk it.
       */
       
      // look for a submit button
      for (j=0; formElement=form[j]; ++j)
        if (formElement.type == "submit") {
          submitButton = formElement;
          break; 
        }

      // also look for (and prefer) <input type=image>
      // why this extra loop? <input type=image> isn't form[j] for any j
      // (see bug 113197 or http://whatwg.org/specs/web-forms/current-work/#additions)
      for (j=0; formElement=form.getElementsByTagName("input")[j]; ++j)
        if (formElement.type == "image") {
          submitButton = formElement;
          break;
        }

      if (submitButton) {
        // Give a visual indication that we're submitting the login form automatically.
        submitButton.focus();
        submitButton.style.MozOutline = "2px solid purple";

        // Submit the form by calling click() on the submit button.
        submitButton.click();
  
        // Break out of both loops.
        return; 
      }
    }
  }

}

window.addEventListener(
  "load", 
  function() { 
    // Use a setTimeout so Firefox's password manager has a chance to fill in the password.
    setTimeout(submitFirstPasswordForm, 0); 
  }, 
  false
);