How to exclude iframe in Greasemonkey or Tampermonkey?

霸气de小男生 提交于 2019-11-26 07:43:54

问题


I have created a Greasemonkey script for a website. What the script does is adding a div at the end of the page.

document.body.insertBefore(myDiv, document.body.firstChild);

But now the site adds an iframe for google-ads, as a result my div appears in the iframe too, which is not what I want.

How can I stop the script affecting iframes?


回答1:


Greasemonkey now supports the @noframes directive (long supported by Tampermonkey and Scriptish).
Use that for a cleaner way to block operation in iframes.


Unfortunately, npdoty's answer will now trigger a warning in Firefox's browser console:

Warning: use of return outside of functions is deprecated and may cause failures in future versions of Greasemonkey.




回答2:


I put this at the top of my scripts to avoid running on frames or iframes:

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


来源:https://stackoverflow.com/questions/1535404/how-to-exclude-iframe-in-greasemonkey-or-tampermonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!