Jor*_*ren 22 javascript greasemonkey tampermonkey
如果您创建Greasemonkey脚本@include *
并转到像youtube这样的站点,则每次刷新时它都会运行脚本20次以上.这是在Firefox上,不确定Chrome.
有办法防止这种情况吗?
Bro*_*ams 36
首先,您可能不希望脚本在iFrame中运行.
您可以使用现在在Greasemonkey和Tampermonkey 中使用的@noframes
指令来阻止它,截至2014年10月.
对于旧版本或不支持的脚本引擎@noframes
,您可以在元数据块之后使用此代码:
if (window.top != window.self) //don't run on frames or iframes
{
//Optional: GM_log ('In frame');
return;
}
Run Code Online (Sandbox Code Playgroud)
其次,您可以在页面加载时等待并触发GM代码一次.将所有内容包装main()
在一起并在load
事件中调用它,如下所示:
window.addEventListener ("load", LocalMain, false);
function LocalMain () {
// Your code goes here.
}
Run Code Online (Sandbox Code Playgroud)
第三,您可以通过向// @exclude
元数据块添加指令来排除网站或页面.
总的来说,如果可能的话,最好避免普遍包含的GM脚本.
其他方法可能设置标志或使用URL参数重新加载页面.这些变得棘手,所以将它们作为最后的手段来保存.