我正在尝试编写一个脚本来在我玩的一款名为 Evony 的游戏上创建帐户。
在创建帐户的页面上,我找到了电子邮件和密码文本框的元素 ID,并尝试将文本输入其中,但我的代码由于某种原因无法正常工作。
创建帐户页面的链接位于:http://user.evony.com/index.do?PageModule=ForgetPassword
这是我尝试过的:
<?php
?>
<html>
<meta http-equiv="refresh" content="0; URL=http://user.evony.com/index.do?PageModule=ForgetPassword">//loads webpage
<script>
var email = email;
var password = password;
var accnumber = 0;
function createaccount(){
// Click the button to bring up create acc tab
document.getElementById("tab2").click()
// If the clicking above doesn't work,
// this should manually bring up the tab
// to create the acc
document.getElementById('tab_login_c').style.display='none';
// Part of above
document.getElementById('tab_reg_c').style.display='';
this.className='on'" class="on">
// This is to input the email into the email box
document.getElementById('username').value = email + accnumber + "@yahoo.com";
}
// Execute the code
window.onload = function () { createaccount() }
</script>
</html>
就输入密码而言,我的代码尚未完全完成,并且我没有循环来增加 x
——我正在尝试让这部分首先工作。
为了运行此程序,我使用 XAMPP 并进入 Chrome 和 127.0.0.1:5555
。
它加载网页,但不运行任何 JavaScript。任何帮助都会很棒!
最佳答案
不幸的是,你的方法基本上是不可能的。
当您指示 HTML 重定向并加载用户帐户页面时,您的网络浏览器将完全离开您编写的网页,从而丢失您编写的所有 JavaScript 代码。确实没有一种干净的方法可以让一个页面加载另一个网页并尝试将 JavaScript 注入(inject)其中。
事实上,几乎现有的每个网络浏览器都会尝试并主动阻止一个网页冒充另一个网页并注入(inject)任意 JavaScript。这是因为这样做可能会导致各种安全漏洞 - 请参阅 cross-site scripting如果您有兴趣了解更多信息。
为了使其正常工作,您需要执行以下操作之一:
- 直接向 Evony 的服务器发送请求,而不是填写文本框
- 使用类似 Greasemonkey 的内容编写将运行您的 JavaScript 代码的用户脚本。
- 使用网络自动化工具编写脚本,例如 Selenium或更通用的自动化工具,如 AutoHotKey .
我还会仔细查看 Evony 的服务条款 - 我怀疑他们是否会批准自动创建大量帐户,如果您使用这些帐户来滥用或利用这些帐户,您很可能会遇到麻烦游戏。
关于javascript - 重定向到不同页面时无法使 JS 脚本工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31376159/