如何使用Greasemonkey更改JavaScript变量?

How can I change a JavaScript variable using Greasemonkey?

本文关键字:变量 JavaScript 更改 何使用 Greasemonkey      更新时间:2023-09-26

这是我试图修改的页面,我想绕过倒计时定时器,我应该如何写脚本?是否有一种方法,我可以改变变量document.licenseform.btnSubmit.disabled是使用Greasemonkey?


<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>

使用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);

请记住,当您以这种方式使用函数的序列化形式时,正常的闭包作用域将不起作用。你注入的函数将不能访问脚本中的变量,除非这些变量是在该函数中定义的。

尝试调用Timer()函数,因为它是你想要发生的事情:

unsafeWindow.Timer();

当你在它的时候,改变Update函数为什么都不做:

unsafeWindow.update = function(){}

这是可能的。简单地说,您可以使用对象unsafeWindow,例如

unsafeWindow.document.licenseform.btnSubmit.disabled = true;

但是不建议这样做,因为这是不安全的。更多信息请点击这里:http://wiki.greasespot.net/UnsafeWindow

忽略任何关于"不安全"的说法,因为脚本->文档写入操作是完全安全的。

unsafeWindow.document.licenseform.btnSubmit.disabled = false;

(使用mkoryak的方法来抑制超时回调)给定的表单只包含timeout,所以您可能希望完全绕过它:

// this example is INSECURE 
unsafeWindow.document.licenseform.submit();
看到了

?