这是我试图修改的页面,我想绕过倒数计时器,我该怎么写脚本?
有没有办法可以使用Greasemonkey将变量document.licenseform.btnSubmit.disabled
更改为yes?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>dsfsdf</title>
</head>
<body>
<form name="licenseform" method="post" action="">
<input name="btnSubmit" type="button" value="我同意">
</form>
<SCRIPT language=javascript type=text/javascript>
<!--
var secs = 9;
var wait = secs * 1000;
document.licenseform.btnSubmit.value = "我同意 [" + secs + "]";
document.licenseform.btnSubmit.disabled = true;
for(i = 1; i <= secs; i++)
{
window.setTimeout("Update(" + i + ")", i * 1000);
//这一句很关键,记得参数写法为("update("+i+")",i*1000)
}
window.setTimeout("Timer()", wait);
function Update(num)
{
if(num != secs)
{
printnr = (wait / 1000) - num;
document.licenseform.btnSubmit.value = "我同意 [" + printnr + "]";
}
}
function Timer()
{
document.licenseform.btnSubmit.disabled = false;
document.licenseform.btnSubmit.value = " 我同意 ";
}
-->
</SCRIPT>
</td>
<!--网页中部中栏代码结束-->
</body>
</html>
答案 0 :(得分:4)
使用unsafeWindow
的更安全的替代方法是将代码注入文档。您注入的代码将在与页面代码相同的上下文中运行,因此它可以直接访问那里的所有变量。但它无法访问用户脚本代码的其他部分中的变量或函数。
注入代码的另一个好处是,以这种方式编写的用户脚本可以在Chrome和Firefox中使用。 Chrome根本不支持unsafeWindow
。
我最喜欢的注入代码的方法是编写一个函数,然后使用这个可重用的代码来获取函数的源代码:
// Inject function so that in will run in the same context as other
// scripts on the page.
function inject(func) {
var source = func.toString();
var script = document.createElement('script');
// Put parenthesis after source so that it will be invoked.
script.innerHTML = "("+ source +")()";
document.body.appendChild(script);
}
要切换btnSubmit
,您可以编写如下脚本:
function enableBtnSubmit() {
document.licenseform.btnSubmit.disabled = false;
document.licenseform.btnSubmit.value = " 我同意 ";
// Or just invoke Timer()
}
function inject(func) {
var source = func.toString();
var script = document.createElement('script');
script.innerHTML = "("+ source +")()";
document.body.appendChild(script);
}
inject(enableBtnSubmit);
请记住,当您以这种方式使用函数的序列化形式时,正常的闭包范围将不起作用。您注入的函数将无法访问脚本中的变量,除非它们是在该函数内定义的。
答案 1 :(得分:0)
尝试调用Timer()函数,因为它无论如何都要发生:
unsafeWindow.Timer();
当你在它时,更改更新功能不做任何事情:
unsafeWindow.update = function(){}
答案 2 :(得分:0)
这是可能的。简短的回答是你可以使用对象unsafeWindow
,例如
unsafeWindow.document.licenseform.btnSubmit.disabled = true;
然而,这并不是建议这样做,因为它是不安全的。有关这方面的更多信息: http://wiki.greasespot.net/UnsafeWindow
答案 3 :(得分:0)
忽略任何关于“不安全”的说法,因为script-&gt;文档写入操作非常安全。
unsafeWindow.document.licenseform.btnSubmit.disabled = false;
(使用mkoryak的方法来抑制超时回调) 给定的表单只包含超时,因此您可能希望完全绕过它:
// this example is INSECURE
unsafeWindow.document.licenseform.submit();
请参阅?