如何在Greasemonkey或Tampermonkey中排除iframe?

fwo*_*nce 14 javascript greasemonkey tampermonkey

我为网站创建了Greasemonkey脚本.脚本的作用是在页面末尾添加div.

document.body.insertBefore(myDiv, document.body.firstChild);
Run Code Online (Sandbox Code Playgroud)

但现在该网站为google-ads添加了一个iframe,结果我的div也出现在iframe中,这不是我想要的.

如何停止影响iframe的脚本?

npd*_*oty 16

我把它放在脚本的顶部,以避免在框架或iframe上运行:

if (window.top != window.self)  //don't run on frames or iframes
    return;
Run Code Online (Sandbox Code Playgroud)


Bro*_*ams 9

Greasemonkey现在支持@noframes指令(Tampermonkey和Scriptish长期支持).
使用它可以更清洁地阻止在iframe中的操作.


不幸的是,npdoty的答案现在会在Firefox的浏览器控制台中触发警告:

警告:不推荐在函数外部使用return,并且可能导致Greasemonkey的未来版本出现故障.