// ==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+tris
    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)
    1.3+tris - add rudimentary rate-limit (by Tristan Horn)

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

*/

var globalKey = "AutoLoginJ call counter";
var maxCallCount = 3;           // this many calls are okay...
var maxCallInterval = 3000;     // ...in this many milliseconds.

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 && GM_getValue(globalKey, 0) < maxCallCount) {
        // 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();

        // Increment the call counter.
        GM_setValue(globalKey, GM_getValue(globalKey, 0) + 1);

        // 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
);

// Reset the call counter to zero after maxCallInterval milliseconds.
// (not really ideal)
window.setTimeout(function() { GM_setValue(globalKey, 0); }, maxCallInterval);
