最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I change a JavaScript variable using Greasemonkey? - Stack Overflow

programmeradmin0浏览0评论

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>

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>
Share Improve this question edited Apr 19, 2011 at 23:00 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Apr 19, 2011 at 20:10 JoeJoe 211 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

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.

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(){}

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 reemended to do so, because it is unsecure. More information about this here: http://wiki.greasespot/UnsafeWindow

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

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

See?

发布评论

评论列表(0)

  1. 暂无评论