javascript - Run a Greasemonkey script only once per page load? -
if create greasemonkey script @include *
, go site youtube, runs script 20+ times every time refresh. on firefox, not sure chrome.
there way prevent this?
first, don't want script run in iframes.
can block using the @noframes
directive works in both greasemonkey , tampermonkey of october, 2014.
for older versions, or script engines don't support @noframes
, can use code, after metadata block:
if (window.top != window.self) //don't run on frames or iframes { //optional: gm_log ('in frame'); return; }
second, can wait , fire gm code, once, on page load. wrap in main()
, call on load
event, so:
window.addeventlistener ("load", localmain, false); function localmain () { // code goes here. }
third, can exclude sites or pages adding // @exclude
directives metadata block.
overall, it's best avoid universally included gm scripts, if possible.
other methods might set flags or reload page url parameters. these tricky save them last resort.
Comments
Post a Comment