GM_openInTab (or any other GM_ function) is not defined?

后端 未结 1 821
栀梦
栀梦 2021-01-20 20:26

When my GM script does this:

var curTab = GM_openInTab(url);

it results in a \'GM_openInTab is not defined\' JavaScript error

1条回答
  •  一整个雨季
    2021-01-20 21:24

    In order to use any of the GM_ functions, you must set a matching @grant directiveDoc (As of Greasemonkey version 2.0Release notes)

    For example:

    // ==UserScript==
    // @name     _YOUR_SCRIPT_NAME
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @grant    GM_openInTab
    // ==/UserScript==
    
    var curTab  = GM_openInTab ("http://pwnthemall.com/");
    

    Note that this has the side effect of switching Greasemonkey's sandbox back on. See also:

    • Error: Permission denied to access property 'handler'
    • How to access `window` (Target page) objects when @grant values are set?


    Tampermonkey emulates most of this behavior as of version 3.9Release notes. But the current version (3.9.202) still attempts to guess appropriate values if @grant is not specified, so you won't necessarily see an error (yet).
    Always use @grant anyway, for maximum compatibility and to future-proof your code.

    0 讨论(0)
提交回复
热议问题