开发者

How can I change a JavaScript variable using Greasemonkey?

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.licensefor开发者_开发技巧m.btnSubmit.value = " 我同意 ";
                     }
                     -->
                     </SCRIPT>
    </td>
    <!--网页中部中栏代码结束-->
</body>
</html>


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

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

See?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜