0

I'm looking for a way to automatically fill in a username/password in a webpage using Greasemonkey.

The two tags are inputs, they have no ID but they're the only two inputs in the page, which appear when a 'log in' button is pressed.

I am a beginner in Greasemonkey (and javascript) and I was wondering how I could wait for these two inputs to appear.
Then there's two buttons, an 'ok' and a 'cancel' button. The 'ok' button is greyed, and setting the inputs with element0.value="password" doesn't ungrey the button.

How can I ungrey it using javascript?

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
shkra19
  • 309
  • 2
  • 4
  • 13
  • Edit your question to show the **exact** HTML containing the inputs and the button. Also link to the target page. Show what you have tried! – Brock Adams Apr 16 '13 at 06:11
  • The link to the target page won't be possible, it's a local page. But the two tags I'm interested in are of the form and . I managed to retrieve them both using var el =getElementsByTagName("input") and then el[0] and el[1]. But I'm not sure how to wait for them to appear (maybe check periodically until these inputs are present ?), nor how to ungrey the button. – shkra19 Apr 16 '13 at 06:15

1 Answers1

1

The question lacks the requested and required HTML, but in general, you would use techniques from "Choosing and activating the right controls on an AJAX-driven site".

Something like:

// ==UserScript==
// @name     _YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

waitForKeyElements ("input[type='password']", logIn);

function logIn (jNode) {
    var inputs  = $("input");

    //-- DO NOT HARDCODE LOGIN CREDENTIALS IN A SCRIPT!
    $(inputs[0]).val ("username");
    $(inputs[1]).val ("password");

    //-- Might need to trigger a change or mouse event here.

    waitForKeyElements (
        "JQUERY SELECTOR FOR THE OK BUTTON IN THE ACTIVE STATE",
        clickOK_Btn
    );
}

function clickOK_Btn (jNode) {
    triggerMouseEvent (jNode[0], "click");
}

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}


But do not hardcode login credentials. Use a sensitive information framework.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295