Bud Wieser
Bud Wieser

Reputation: 273

How to prevent execution of Greasemonkey function in subwindows?

I have a Greasemonkey function that creates a button on every website my browser visits.

I would like to prevent this from happening in all subwindows of the main window of the visited website. (Right now the button shows up on subwindows like adsense windows or input textareas in forums and I can see it several times when I am on one website).

How can I confine this function to the main window of the website?

Upvotes: 3

Views: 405

Answers (1)

Brock Adams
Brock Adams

Reputation: 93473

Those "subwindows" are <iframe>s.
See similar questions:


Greasemonkey will run on iframes, just as though they were the main page -- if the iframe matches the @include, @exclude, and @match directives of your script.

To solve the subwindow/iframe issue:

  1. Tune your @include, @exclude rules, and/or your @match directives to eliminate as many undesired iframes as you reasonably can.
    Try to avoid having scripts run on every page.

  2. Use the @noframes directive if only top-window operation is desired.

  3. To control at the function level, versus whole script, you can use code like:

    if (window.top === window.self) {
        // CODE TO RUN ON MAIN PAGE
    }
    else {
        // CODE TO RUN IN IFRAME
    }
    

Upvotes: 4

Related Questions