如何使用 Greasemonkey 更改 JavaScript 变量?

发布于 2024-11-02 08:27:55 字数 1797 浏览 0 评论 0原文

这是我正在尝试修改的页面,我想绕过倒计时器,我应该如何编写脚本? 有没有办法可以使用 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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

银河中√捞星星 2024-11-09 08:27:55

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

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

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.

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

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.

魔法唧唧 2024-11-09 08:27:55

尝试调用 Timer() 函数,因为无论如何它都是您想要发生的:

unsafeWindow.Timer();

当您使用它时,将 Update 函数更改为不执行任何操作:

unsafeWindow.update = function(){}

try calling the Timer() function since its what you want to happen anyway:

unsafeWindow.Timer();

while you are at it, change the Update function to do nothing:

unsafeWindow.update = function(){}
简单 2024-11-09 08:27:55

这是可能的。简而言之,您可以使用对象 unsafeWindow,例如

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

,但是不建议这样做,因为它不安全。有关此的更多信息请参见此处:
http://wiki.greasespot.net/UnsafeWindow

This is possible. The short answer is you can use the object unsafeWindow, for instance

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

However it is not recomemended to do so, because it is unsecure. More information about this here:
http://wiki.greasespot.net/UnsafeWindow

黑白记忆 2024-11-09 08:27:55

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

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

(使用mkoryak的方法抑制超时回调)
该给定的表单只包含超时,因此您可能想完全绕过它:

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

看到了吗?

Disregard anything said about "insecure", because script->document write operation IS perfectly secure.

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

(Use mkoryak's method to suppress timeout callback)
That given form contains nothing but timeout, so you might want to bypass it completely:

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

See?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文