SetTimeout not working in Greasemonkey user script when JS disabled in browser?

Even though Greasemonkey JavaScript runs with elevated privileges, as Pointy said setTimeout functions are appended to the page's JavaScript space -- wrapped in a closure as needed. (In normal operation, the Greasemonkey instance is often gone by the time any timers, it has set, fire. ) So, if the page's main JavaScript is disabled, the timer will never run Possible workarounds: Use GM_xmlhttpRequest as a crude delay.

You can setup a page that deliberately draws out its response. So code like: GM_xmlhttpRequest ( { method: "GET", url: "http://YourTestServer.com/DelayService.php?Seconds=2", onload: function (response) {YourDelayedFunctionHere (); } } ) Would call a utility page that you set up to do the delay for you Use NoScript to disable all of the page's JavaScript except for the main page. For example, for page YourSite.Com/testpage.

Htm which includes scripts from, say SpamGenerator. Net Allow scripts from YourSite.Com but block them from SpamGenerator.net.

Even though Greasemonkey JavaScript runs with elevated privileges, as Pointy said, setTimeout functions are appended to the page's JavaScript space -- wrapped in a closure as needed. (In normal operation, the Greasemonkey instance is often gone by the time any timers, it has set, fire. ) So, if the page's main JavaScript is disabled, the timer will never run.

Possible workarounds: Use GM_xmlhttpRequest as a crude delay. You can setup a page that deliberately draws out its response. So code like: GM_xmlhttpRequest ( { method: "GET", url: "http://YourTestServer.com/DelayService.php?Seconds=2", onload: function (response) {YourDelayedFunctionHere (); } } ); Would call a utility page that you set up to do the delay for you.

Use NoScript to disable all of the page's JavaScript except for the main page. For example, for page, YourSite. Com/testpage.

Htm, which includes scripts from, say, *SpamGenerator.net... Allow scripts from YourSite. Com but block them from SpamGenerator.net.

NoScript as it turns out doesn't work for me, since I need to disable JavaScript from the main page as well as from any other sources that aren't my user script. The XMLhttpRequest on the other hand works beautifully (even though it's a total hack). Thanks for the insight, Brock!

– peter Jul 30 '10 at 21:46 @user360010: You're welcome. Glad to help! – Brock Adams Jul 31 '10 at 1:51.

The window reference is still the page's window, just wrapped in the sandbox wrapper thing. When you call setTimeout on it you're still setting up something to be run by the page. I suppose that it must be the case that the browser won't fire those timeout events at all (or will just ignore the events) when Javascript is disabled.

Greasemonkey gets to define its behavior, instead of the page being allowed to do this. I'm wondering if GM maybe just punts on the function call and allows the original window (ie. UnsafeWindow) to deal with it.

(Edit: In the end, my understanding is the same as yours. The call gets set up and ignored. Now I'm just wondering if there's any way around this.

Maybe some way to delay function execution outside of using window object...?) – peter Jul 28 '10 at 18:55 Well Greasemonkey might not get a say in the matter if the browser thinks that it shouldn't run any Javascript event handlers. I have not found any explicit documentation that says what happens when Javascript is disabled on a page. Obviously the "native" event handling still works - you can still submit forms by clicking "submit" buttons for example, and checkboxes and radio buttons work.

– Pointy Jul 28 '10 at 19:11.

This can be patched like this: You can say NO to NoScript + setTimeout = failed In greasemonkey. Js: find injectScripts : function..... add our GM-api..... Add this code: sandbox. SetTimeOut = function (callback, timeout, p1,p2,p3/*....*/){ var args = Array.prototype.slice.

Call(arguments,2); return sandbox.window. SetTimeout(function(){ return callback. Apply(sandbox, args); } ,timeout); } or sandbox.

SetInterval = function (callback, timeout, p1,p2,p3/*....*/){ var args = Array.prototype.slice. Call(arguments,2); return sandbox.window. SetInterval(function(){ return callback.

Apply(sandbox, args); } ,timeout); } This code is working fine, I have used it since May 2010. In user. Js you can test it like this: setTimeout(alert,1000, 'i am happy'); var loopid = setInterval(alert, 1000, 'I am happy again'); setTimeout(clearInterval, 5000, loopid); var j=300; for(;~j;j--){ //running perfectly!

SetTimeout(alert, 1000+20*j, 'I am happy' ) } Solution 2 sandbox. Kk_setTimeout = function (func, timeout, repeat_type, p1,p2,p3/*....*/){ var callback = { k100: sandbox }; var args = Array.slice. Call(arguments,3); // repeat_type: 0=once 1=repeatng, after fired stopped 2=always repeat if(repeat_type!

=2){ callback. Notify = function (timer){ func. Apply(this.

K100,args); } var timerCC = Components. Constructor("@mozilla. Org/timer;1", "nsITimer", 'initWithCallback'); var R = repeat_type?1:0; } else { callback.

Observe = function (subject, topic, data) { func. Call(this. K100); }; var timerCC = Components.

Constructor("@mozilla. Org/timer;1", "nsITimer", 'init'); var R = 2; } return new timerCC(callback, timeout, R); } // now have to test it: var test100 = kk_setTimeout(alert, 1000, 0, 'i am timer'); //running = setTimeout var test100 = kk_setTimeout(alert, 1000, 2, 'i am timer'); //running = setInterval test100.cancal() ; //clear it by cancel() method kk_setTimeout(alert, 1000+20*j, 2, 'i am happy' ); var j=300; for(;~j;j--){ kk_setTimeout(alert, 1000+20*j, 0, 'i am happy 2' ); } //bug: //this solution 2 running after about 3-8 times differently stop, why bug? I don't know.

// you will fail to use many times(over 3-8 time) kk_timeout(); or using repeat_type = 2 after fired 3-8 times timeout //or running total time max about 20-30 seconds stop //--- this maybe stop by option in about:config -- about max javascript run time china-kkmove patched edit to add… Sorry everyone, There are still a few patches to the code that I forgot to write: sandbox. Window = sandbox. _proto_; // add this line also to the solution 1# This error just came to my mind this morning.

Also, this solution is not portable -- makes it much harder to share the user's script. – Brock Adams Jun 4 at 22:05.

The problem is quite strange, if you'd care to look at my source code: www.modwebsolutions.com/test2 (warning: not optimized yet, might lock browser for a few seconds). Problem is, that sequence of setTimeouts works ok in Opera, but in other browsers only first one is executed and then execution of the script stops. Tried everything, enclosing function in quotes, looking for other syntax errors, nothing helped.

And the strange thing is as I mentioned - everything works ok in Opera.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions