Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

If you create a Greasemonkey script with @include * and go to a site like youtube, it runs the script 20+ times every time you refresh. This is on Firefox, not sure about Chrome.
Is there a way to prevent this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
565 views
Welcome To Ask or Share your Answers For Others

1 Answer

First, you probably don't want the script to run in iFrames.
You can block that using the @noframes directive which now works in both Greasemonkey and Tampermonkey as of October, 2014.

For older versions, or for script engines that don't support @noframes, you can use this code, just after the metadata block:

if (window.top != window.self)  //don't run on frames or iframes
{
    //Optional: GM_log ('In frame');
    return;
}


Second, you can wait and fire your GM code, once, on page load. Wrap everything in a main() and call it on the load event, like so:

window.addEventListener ("load", LocalMain, false);

function LocalMain () {
    // Your code goes here.
}


Third, you can exclude sites or pages by adding // @exclude directives to the metadata block.

Overall, it's best to avoid universally included GM scripts, if possible.

Other methods might set flags or reload the page with URL parameters. These get tricky so save them as a last resort.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...