【问题标题】:Why are global variables suddenly available for Greasemonkey and Tampermonkey scripts?为什么全局变量突然可用于 Greasemonkey 和 Tampermonkey 脚本?
【发布时间】:2015-01-21 11:06:15
【问题描述】:

最近在编写用户脚本时,我发现页面上下文中的变量可用。
console.log(window) 在 Tampermonkey 和 Greasemonkey 中都没有导致错误。

我很困惑。全局变量不应该只能通过unsafeWindow访问吗?

【问题讨论】:

    标签: greasemonkey tampermonkey


    【解决方案1】:

    全局变量仅在@grant none 模式下可用。 As of Greasemonkey 2.0 (June 17, 2014), @grant none mode became the default.Tampermonkey 迅速效仿,保持兼容性。

    如果您从来没有使用过@grant 的习惯,这似乎是最近的(意料之外的)变化。

    您现在应该始终使用the @grant directive,这样您就可以知道脚本在什么模式下运行。

    我也推荐至少使用@grant GM_addStyle,除非在极少数情况下@grant none 是合适的。这避免了意外的冲突和副作用,并且最接近脚本过去的工作方式以及仍有多少脚本引擎仍在运行。 (也就是说,这样的脚本更可靠,跨平台兼容性更好。)

    Greasemonkey 处理@grant 的方式现在是a leading cause of manymany problems

    【讨论】:

    • 是的,我确实使用了@grant none。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2018-05-15
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2016-03-03
    相关资源
    最近更新 更多