I am trying to automate the submission of a form on a webpage using a Greasemonkey script. The username and password fields in the form are already filled by Firefox when the page loads, so all I need is for the form to automatically submit and login.
Here is the code for the form:
<form class="enableAutoFocus" method="post" id="login_form" action="http://localhost/plan/login_form">
<div id="login-form">
<input name="came_from" value="" type="hidden">
<input name="next" type="hidden">
<input name="ajax_load" type="hidden">
<input name="ajax_include_head" type="hidden">
<input name="target" type="hidden">
<input name="mail_password_url" type="hidden">
<input name="join_url" type="hidden">
<input name="form.submitted" value="1" type="hidden">
<input name="js_enabled" id="js_enabled" value="0" type="hidden">
<input name="cookies_enabled" id="cookies_enabled" value="" type="hidden">
<input name="login_name" id="login_name" value="" type="hidden">
<input name="pwd_empty" id="pwd_empty" value="0" type="hidden">
<div class="field">
<label for="__ac_name">Login Name</label>
<input style="margin-right: 0px; padding-right: 0px;" size="15" name="__ac_name" value="" id="__ac_name" type="text"><img title="Max field length is unknown" style="position:relative; z-index: 999; cursor:pointer; vertical-align: bottom; border: 0; width: 14px; height: 19px; display:none;" class="ife_marker" src="chrome://informenter/skin/marker.png" id="__ac_name_ife_marker_1">
</div>
<div class="field">
<label for="__ac_password">Password</label>
<input style="margin-right: 0px; padding-right: 0px;" size="15" name="__ac_password" id="__ac_password" type="password"><img title="Max field length is unknown" style="position:relative; z-index: 999; cursor:pointer; vertical-align: bottom; border: 0; width: 14px; height: 19px; display:none;" class="ife_marker" src="chrome://informenter/skin/marker.png" id="__ac_password_ife_marker_2">
</div>
<div class="formControls">
<input class="context" name="submit" value="Log in" type="submit">
</div>
</div>
</form>
The first form on the page is a search form, so initially I used document.forms[0].submit();
It was successful.
Then I modified the code as follows:
document.forms[1].submit();
However, this resulted in an error reported by the console:
Error: document.forms[1].submit is not a function
After some research, I found the following code and gave it a try.
function ClicktheButton(obj) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window,
0, 0, 0, 0, 0, false, false, false, false, 0, null);
var canceled = !obj.dispatchEvent(evt);
}
var StupidButton = document.querySelector('input[type="submit"][value="Log in"]');
ClicktheButton(StupidButton);
The click function works, but the username and password fields remain blank, preventing a successful login.
If anyone could provide some explanation and assistance, I would greatly appreciate it. Thanks.