如何使用Greasemonkey更改JavaScript变量? [英] How can I change a JavaScript variable using Greasemonkey?

查看:41
本文介绍了如何使用Greasemonkey更改JavaScript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要修改的页面,我想绕过倒数计时器,我该如何编写脚本? 是否可以使用Greasemonkey将变量document.licenseform.btnSubmit.disabled更改为yes?

This is the page that I am trying to modify, I want to bypass the countdown timer, how should I write the script? Is there a way that I can change the variable document.licenseform.btnSubmit.disabled to yes using 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的更安全的替代方法是将代码注入文档中.您注入的代码将在与页面代码相同的上下文中运行,因此可以直接访问其中的所有变量.但是它将无法访问用户脚本代码其他部分中的变量或函数.

A more secure alternative to using unsafeWindow is to inject code into the document. The code that you inject will run in the same context as the page code, so it will have direct access to all of the variables there. But it will not have access to variables or functions in other parts of your user script code.

注入代码的另一个好处是,以这种方式编写的用户脚本可以在Chrome和Firefox中使用. Chrome完全不支持unsafeWindow.

Another benefit of injecting code is that a user script written that way will work in Chrome as well as in Firefox. Chrome does not support unsafeWindow at all.

我最喜欢的注入代码的方法是编写一个函数,然后使用此可重用代码获取该函数的源代码:

My favorite way to inject code is to write a function, then to use this reusable code to get back the source code for the function:

// 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,您可以编写如下脚本:

To toggle btnSubmit you could write a script like this:

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

请记住,当您以这种方式使用函数的序列化形式时,正常的关闭范围将不起作用.除非在函数内部定义变量,否则您注入的函数将无权访问脚本中的变量.

Remember that when you use the serialized form of a function in this way normal closure scope will not work. The function that you inject will not have access to variables in your script unless they are defined inside that function.

这篇关于如何使用Greasemonkey更改JavaScript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆