【发布时间】:2011-07-07 21:28:12
【问题描述】:
我知道 Greasemonkey 脚本自动包装在匿名函数中以某种方式隔离,以防止它们与页面中的脚本发生冲突。
Chrome 用户脚本也会发生同样的情况吗?
【问题讨论】:
标签: javascript google-chrome greasemonkey anonymous-function userscripts
我知道 Greasemonkey 脚本自动包装在匿名函数中以某种方式隔离,以防止它们与页面中的脚本发生冲突。
Chrome 用户脚本也会发生同样的情况吗?
【问题讨论】:
标签: javascript google-chrome greasemonkey anonymous-function userscripts
是的,Greasemonkey 脚本是 normally wrapped in an anonymous function。还有,Chrome userscripts apparently are too。
但是,更重要的是,Greasemonkey 脚本通常1包裹在 XPCNativeWrapper 沙箱中,而 Google Chrome 将用户脚本转换为扩展程序,而 they operate in an arena that Google calls an "isolated world" 2.
因此,出于安全目的,您无需将脚本代码包装在匿名函数中,它们已经受到保护。
请注意:
<script> 标签),那么该代码可以被页面的 JS 看到。unsafeWindow,则该页面理论上可以跟随它并获得略微提升的权限。风险非常低,我无法在野外找到任何记录在案的漏洞利用。
~~~
底线是,脚本在两种浏览器中的隔离程度不同。 (而不仅仅是被包裹在匿名函数中。)
Greasemonkey 在 Firefox 中提供 a nice set of privileged features。虽然 Chrome 中的用户脚本受到更多限制。
不过,GM 的大部分功能都通过Tampermonkey extension 恢复到 Chrome。
1 截至Greasemonkey version 1.0(2012 年 8 月 24 日),沙盒由the @grant directive 控制。如果脚本以(或默认为)@grant none 运行,则不使用沙箱。该脚本仅在私有范围内运行,而普通的GM_,API 函数将不起作用。
2 这听起来不是比一些讨厌的沙盒更大/更好吗? (^_^)
.
【讨论】: