有没有办法退出Greasemonkey脚本? [英] Is there a way to exit a Greasemonkey script?
问题描述
我知道您可以使用 return;
从Greasemonkey脚本返回,但前提是您不在另一个函数中。例如,这不起作用:
I know that you can use return;
to return from a Greasemonkey script, but only if you aren't in another function. For example, this won't work:
// Begin greasemonkey script
function a(){
return; // Only returns from the function, not the script
}
// End greasemonkey script
是否有内置的Greasemonkey函数可以让我从脚本的任何地方停止执行脚本?
Is there a built in Greasemonkey function that would allow me to halt execution of the script, from anywhere in the script?
谢谢,
推荐答案
是的,你可能会做类似的事情:
Yeah, you can probably do something like:
(function loop(){
setTimeout(function(){
if(parameter === "abort") {
throw new Error("Stopped JavaScript.");
}
loop();
}, 1000);
})(parameter);
您可以通过将变量参数的值设置为中止来简单地中止脚本,这可以是一个常规变量或Greasemonkey变量。如果它是Greasemonkey变量,那么您可以使用Firefox中的about:config直接通过浏览器修改它。
You can simply abort your script by setting the value of variable parameter to abort, this can either be a regular variable or a Greasemonkey variable. If it's a Greasemonkey variable, then you can modify it directly through the browser using about:config in Firefox.
这篇关于有没有办法退出Greasemonkey脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!