Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude iframe in Greasemonkey or Tampermonkey?

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?

like image 697
fwonce Avatar asked Oct 08 '09 03:10

fwonce


2 Answers

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;
like image 66
npdoty Avatar answered Nov 01 '22 20:11

npdoty


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.

like image 12
Brock Adams Avatar answered Nov 01 '22 19:11

Brock Adams