![]() ![]() |
How do I make a script terminate in greasemonkey? I googled it but couldn't find any answers. |
![]() ![]() |
You mean interfere page's original script or prevent your userscript from running? |
![]() ![]() |
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 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) |
![]() ![]() |
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);
|
![]() ![]() |
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 |
![]() ![]() |
thanks cletus, thanks lazyttrick. Lazyttrick, correct me if I am wrong but it looks to me that
I went ahead and added the following to my script http://userscripts.org/scripts/show/125638 :
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
|
![]() ![]() |
|
![]() ![]() |
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?
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) |
![]() ![]() |
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. |
![]() ![]() |
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. |
![]() ![]() |
Thanks for the info thus far people.
|
![]() ![]() |
taltamir wrote:You could try hooking into the unload event that runs when the browser leaves a page: https://developer.mozilla.org/en/DOM/window.onu... |


