Terminating a script

in Script development
Subscribe to Terminating a script 12 posts, 5 voices



taltamir Scriptwright
ChromeWindows

How do I make a script terminate in greasemonkey? I googled it but couldn't find any answers.

 
dindog Scriptwright
FirefoxWindows

You mean interfere page's original script or prevent your userscript from running?

 
taltamir Scriptwright
ChromeWindows

I mean stop my own userscript from running once it has already performed its intended task.

Lets say I have a script that listens for a keypress. When a keypress occurs it checks for the existance of a certain button/checkboxes/radio boxes and checks/submits them.
I noticed that on repeated use I got horrible and ever increasing lag which is eliminated when I restart the browser. It leads me to believe that something is still running.

I want to basically do:

window.addEventListener("keyup", key_press, false);

function key_press(event)
	if (event.keyCode==68) 	//keypress d
		if(document.forms.namedItem("attempt"))
                {
			location.assign('javascript:attempt.submit()');
                        (Terminate script somehow)
                }

Currently I have something similar (somewhat more complex) than the above but without any (terminate script somehow) code. And as a result I get significant and massive slowdown on repeated use (as in, it goes from under a second to several seconds lagtime)

 
Cletus Scriptwright
FirefoxWindows

Rather than attempting to terminate the entire script, just remove the listener after you are done with it. The browser should automatically free up any memory associated with that listener/function once it is no longer used. This should allow you the ability of expansion without the need to work around an unintended termination.

element.removeEventListener(type, listener, useCapture)

window.removeEventListener("keyup", key_press, false);

 
lazyttrick Scriptwright
FirefoxWindows

the only way out here is to remove the listener...

there are other situations though, where you can choose not to run the script's code at all, so at the beginning of the script you can put a a condition like if(skipCode){ return; }

 
taltamir Scriptwright
ChromeWindows

thanks cletus, thanks lazyttrick.

Lazyttrick, correct me if I am wrong but it looks to me that
if(skipCode){ return; }
is saying basically that {return; } terminates a script.

I went ahead and added the following to my script http://userscripts.org/scripts/show/125638 :
function terminate()
{
window.removeEventListener("keyup", key_press, false);
return;
}

And it works great. it takes slightly longer to execute, but doesn't slowdown with use, eliminating the need to restart the browser.

I can't help but wonder if it is redundant though. Do I actually need to remove the event listener if I do
return;

 
Cletus Scriptwright
FirefoxWindows

return will only exit the key_press function, but the listener will still be there and the function will continue to run as many times as you press the key. You might want to try putting the removeEventListenerbefore your location.assign.

 
taltamir Scriptwright
ChromeWindows

thanks cletus.

If return; does what you say then I might as well get rid of it since it isn't helping. The whole thing seems like a bug in chrome rather then correct script behavior. I am gonna try setting up an example page + script and report that.

Also, what is the benefit of putting the removeEventListener before my location.assign?
Currently I hit the button as fast as I can and even on a slow laptop I only observe a single click being registered, the rest are ignored since the listener is removed fast enough after the first click, and doesn't reappear until the page finishes reloading. Which accidentally solves another issue i was having problem with where clicking things before the page finishes loading will trigger a "you are playing too fast, slow down and relog-in to the game" error page.

Anyways, logically I would see the desire to remove it as soon as possible to avoid multiple clicks registering. But on the other hand I also want to submit the page as soon as possible to maximize the speed of the function. Then again, if the time it takes to remove to execute is so little that I can never "catch" it with spamming the button then it wouldn't introduce human noticeable delay and might help for someone playing on an extremely slow machine (say, a smart phone)

 
Cletus Scriptwright
FirefoxWindows

Just a suspicion really, you'd probably have to do some heavy benchmarking to see it if it is even worth moving it around--it shouldn't make THAT huge of a difference if even makes a difference at all. I'd probably just keep it the way you have it as long as it's working.

I don't know the exact execution order regarding multiple listeners running, but with the speed of the current JS engines, it shouldn't ever run twice even when spamming.

 
dindog Scriptwright
FirefoxWindows

in fact, with the event can pile up like keydown, mousedown and some DOM mutation event like beforescriptexecute and DOMNodeInserted, the script will run more than you thought if you don't remove the listener first place.

 
taltamir Scriptwright
ChromeWindows

Thanks for the info thus far people.
Is there any way to terminate a script / remove listener on page navigation? Every time I navigate to another page I add another running instance.

 
Jefferson Scher Scriptwright
FirefoxWindows

taltamir wrote:
Is there any way to terminate a script / remove listener on page navigation? Every time I navigate to another page I add another running instance.
You could try hooking into the unload event that runs when the browser leaves a page: https://developer.mozilla.org/en/DOM/window.onu...