[Greasemonkey] A function to ask for passwords

Nic Ferrier nferrier at tapsellferrier.co.uk
Tue Jul 5 13:12:52 EDT 2005


I recently wrote some GM to make logging into my bank account easier
(they ask for a combination of letters from my password). While doing
this I came up with this little script to ask for a password since
Javascript doesn't provide that.

I'll blog the bank GM later (and drop a short note here) but I thought
that this function might be immediately useful/helpful.

Here it is:


  // This is a portable function to ask for a password with javascript.
  // The continuation_func is a function taking one argument. 
  // It is called with the value that is in the entry box when the user presses OK.
  function gm_prompter(prompt, continuation_func)
  {
    if (prompt == null || continuation_func == null)
      // throw an exception?
      return;

    var div = document.getElementById("gm_promptbox");
    if (div == null)
      {
        div = document.createElement("div");
        div.id = "gm_promptbox";
      }

    div.style.display = "none";
    div.style.position = "fixed";
    div.style.height = "100";
    div.style.width = "400";
    div.style.top = (document.height / 2) - 50;
    div.style.left = (document.width / 2) - 200;
    div.style.textAlign = "center";
    div.style.border = "solid";
    document.body.appendChild(div);

    div.innerHTML = "<em>" + prompt + "</em><br/>"
      + "<form>"
      + "<input id='gm_entry' type='password' accesskey='P' value=''/> "
      + "<button id='gm_ok' type='button'>OK</button> "
      + "<button id='gm_cancel' type='button'>Cancel</button>"
      + "</form>"
      + "<h3>the greasy monkey asks a question</h3>";

    div.style.display = "block;";
    div.style.background = "#4BBFF9";

    // There may be bugs in the lexical scope implementation.
    cont_function = continuation_func;


    div.style.display = "block;";

    // Set the continuations on the onclick handler
    function ok_cont()
    {
      GM_log("gm_prompter OK continuation called");
      div.style.display = "none";
      input_element = document.getElementById("gm_entry");
      var value = input_element.value;
      input_element.value = null;
      cont_function(value);
    }

    function cancel_cont()
    {
      GM_log("gm_prompter Cancel continuation called");
      input_element = document.getElementById("gm_entry");
      input_element.value = null;
      div.style.display="none";
    }

    document.getElementById("gm_ok").onclick = ok_cont;
    document.getElementById("gm_cancel").onclick = cancel_cont;
  }





Nic Ferrier


More information about the Greasemonkey mailing list