17

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?

2 Answers 2

22

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;
0
18

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
  • That warning is not a problem, you just need to wrap your script in an anonymous function: (function(){ ... })();, that's a good practice anyway. Thanks for the @noframes, upvoted.
    – user
    Commented Jan 12, 2015 at 18:12
  • 1
    Changed my acceptance. Thanks for providing a more elegant way.
    – fwonce
    Commented Jan 29, 2015 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.