问题
I am trying to create a javascript that will automatically press a submit button on a third party website. Basically, if I wanted to automatically log in to a site with a cookie filling in my username and password, for example a google account, the javascript would automatically press the submit button without me having to do it.
回答1:
I guess what you're looking for is the .click()
method that all buttons have. A more detailed explanation is below...
You can use a frameset
with your JavaScript code in one frame and the third party page in another. For example:
<!-- Main page -->
<HTML><frameset rows="0,*">
<!-- You see your script page. To see the actual login page, swap the * and the 0. -->
<frame src="http://third.party.website.com/login/page.htm" name="theirframe" />
<frame src="myscript.html" name="myframe" />
</frameset></HTML>
And if the page you want to login to is like this (cleaned up from Gmail's login page, they have a lot of oddly-placed newlines and spaces!):
<!-- Third-party page -->
<div class="email-div">
<label for="Email"><strong class="email-label">Username</strong></label>
<input type="email" spellcheck="false" name="Email" id="Email" value="">
</div>
<div class="passwd-div">
<label for="Passwd"><strong class="passwd-label">Password</strong></label>
<input type="password" name="Passwd" id="Passwd">
</div>
<input type="submit" class="g-button g-button-submit" name="signIn" id="signIn" value="Sign in">
Then your script would probably be something like this:
<!-- Your script page (myscript.html) -->
<HTML><body>
<!-- Insert custom message here... -->
Logging you in......
<script>
//parent.theirframe.document.getElementById("Email").value = getemail();
//parent.theirframe.document.getElementById("Passwd").value = getpass();
parent.theirframe.document.getElementById("signIn").click();
</script>
<body></HTML>
If your browser is telling you that "The element with ID "Email" doesn't exist" or something, the your browser probably loaded your script page before the login page. In that case, you'll have to put the login page in a named iframe
within your custom page and place your script after the iframe
in the body of the page. If you do end up doing that, this page will help you out with accessing the iframe
's content.
Hope this helps out.
来源:https://stackoverflow.com/questions/12449341/javascript-to-automatically-login-to-third-party-site