admin管理员组文章数量:1315375
The first time, each day, I try to access a web page at work, I get redirected to an internally hosted web page with the IT guidelines and a form with two buttons "Agree" and "Disagree". Clicking "Agree" then allows external internet access for that day and sends you to the site you were originally looking for.
I want to make a Greasemonkey script that auto-submits the form, since I already have a batch file starting up all my normal apps on boot and this would allow me to just leave the PC while it's doing its 20 minute daily start-up ;)
The page only has the one form:
<form method="post">
<input type="submit" value="I Agree" class="agree" onsubmit="submitonce(this)" />
<input type="button" onclick="javascript:window.close();" value="I Disagree"
class="disagree"/>
</form>
And not sure if it matters, since I only need the click, but the function submitonce
is:
function submitonce(theform) {
//if IE 4+ or NS 6+
console.log("Submitting");
if (document.all || document.getElementById) {
//screen thru every element in the form, and hunt down "submit" and "reset"
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
if (tempobj.type.toLowerCase() == "submit" ||
tempobj.type.toLowerCase() == "reset")
//disable em
tempobj.disabled = true;
}
}
}
I have the rest of the source, but it doesn't look like there is anything else relevant. I haven't really coded before in Greasemonkey/JS, so any help would be appreciated. I'm playing around with an existing userscript that uses CtrlEnter to click the button.
Obviously I don't care if it's a virtual "click" or just a trigger of the submit function, since I'd say they are the same thing aren't they?
The first time, each day, I try to access a web page at work, I get redirected to an internally hosted web page with the IT guidelines and a form with two buttons "Agree" and "Disagree". Clicking "Agree" then allows external internet access for that day and sends you to the site you were originally looking for.
I want to make a Greasemonkey script that auto-submits the form, since I already have a batch file starting up all my normal apps on boot and this would allow me to just leave the PC while it's doing its 20 minute daily start-up ;)
The page only has the one form:
<form method="post">
<input type="submit" value="I Agree" class="agree" onsubmit="submitonce(this)" />
<input type="button" onclick="javascript:window.close();" value="I Disagree"
class="disagree"/>
</form>
And not sure if it matters, since I only need the click, but the function submitonce
is:
function submitonce(theform) {
//if IE 4+ or NS 6+
console.log("Submitting");
if (document.all || document.getElementById) {
//screen thru every element in the form, and hunt down "submit" and "reset"
for (i = 0; i < theform.length; i++) {
var tempobj = theform.elements[i];
if (tempobj.type.toLowerCase() == "submit" ||
tempobj.type.toLowerCase() == "reset")
//disable em
tempobj.disabled = true;
}
}
}
I have the rest of the source, but it doesn't look like there is anything else relevant. I haven't really coded before in Greasemonkey/JS, so any help would be appreciated. I'm playing around with an existing userscript that uses CtrlEnter to click the button.
Obviously I don't care if it's a virtual "click" or just a trigger of the submit function, since I'd say they are the same thing aren't they?
Share Improve this question edited Sep 9, 2013 at 6:48 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jul 19, 2012 at 6:56 vbevanvbevan 931 silver badge9 bronze badges 7- What browser(s) do you intend to use it on? – Fabrício Matté Commented Jul 19, 2012 at 7:01
-
They're not the same,
submit()
doesn't send the name and value of the submit button with the data. – Adi Commented Jul 19, 2012 at 7:06 -
1
Taking a second look at your html, I don't think
input
elements have anonsubmit
attribute. – Fabrício Matté Commented Jul 19, 2012 at 7:09 - It's to be used in Firefox, though I imagine something this simple would work in Chrome too. As for the onsubmit attrib, it's not my code, but hey maybe that function actually does nothing it just the submit moves users on :p – vbevan Commented Jul 19, 2012 at 7:33
-
1
@vbevan, the function
submitonce()
is only disabling thesubmit
andreset
buttons. Doesn't have anything with actual form submission. Anyway, your best bet is dda's answer. – Adi Commented Jul 19, 2012 at 7:36
2 Answers
Reset to default 6It should be a simple job:
// ==UserScript==
// @name myscript
// @namespace whatever
// @include http://path to the internal page
// ==/UserScript==
document.forms[0].submit();
Got some help from a friend who said this should work, though it's not nearly as simple as the answer from dda. Apparently this works in Chrome:
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 cancelled = !obj.dispatchEvent(evt);
/*
if(cancelled) {
// A handler called preventDefault
alert("cancelled");
} else {
// None of the handlers called preventDefault
alert("not cancelled");
}
*/
}
var StupidButton = document.querySelector('input[type="submit"][value="I Agree!"]');
ClicktheButton(StupidButton);
And it would also need the includes etc. that GreaseMonkey scripts always have.
本文标签: javascriptSubmit form using GreasemonkeyStack Overflow
版权声明:本文标题:javascript - Submit form using Greasemonkey - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741975248a2408081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论