【发布时间】:2015-01-21 11:06:15
【问题描述】:
最近在编写用户脚本时,我发现页面上下文中的变量可用。console.log(window) 在 Tampermonkey 和 Greasemonkey 中都没有导致错误。
我很困惑。全局变量不应该只能通过unsafeWindow访问吗?
【问题讨论】:
最近在编写用户脚本时,我发现页面上下文中的变量可用。console.log(window) 在 Tampermonkey 和 Greasemonkey 中都没有导致错误。
我很困惑。全局变量不应该只能通过unsafeWindow访问吗?
【问题讨论】:
全局变量仅在@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 many、many problems。
【讨论】:
@grant none。谢谢!