自几天前发布新的Greasemonkey 1.0以来,每个具有jQuery的站点以及我在Greasemonkey脚本中使用jQuery的站点均无法正常运行我的脚本。我的GS脚本中的jQuery(使用@require元数据)与页面的jQuery冲突。这是由于新的 @grant 代码。

我已经阅读了文档,但仍然不知道如何再次在沙箱中运行GS脚本。唯一的选择似乎是要么授予对GS API的访问权限,要么不授予它访问权限,然后在没有任何安全限制的情况下运行脚本,当我设计了数十个GS脚本以运行时,这对我根本不起作用具有安全性限制,并且喜欢这种方式。

最佳答案

Greasemonkey 1.0, radically changed the way the sandbox works,破坏了数千个脚本。这是一个巨大的问题,我希望您能与我一起表达您对the principle bug report for this issue的意见/经验。

The Greasemonkey blog claims that you can workaround the issue with the following:

this.$ = this.jQuery = jQuery.noConflict(true);

...我不确定在所有情况下都可以使用。从避免副作用的原子编码哲学DRY-principle出发,这是完全错误的方法。我认为,最好的策略是还原沙箱。

通过指定@grant值(none除外)重新激活沙箱。编辑您的 Metadata Block以以下行结尾:

// @grant       GM_addStyle
// @grant       GM.getValue
// ==/UserScript==
/*- The @grant directive is needed to work around a design flaws introduced in GM 1.0
    and again in GM 4.0.
    It restores the sandbox.
*/

沙箱将被还原,所有冲突都将得到解决。
这些脚本将与Tampermonkey和Violentmonkey等高级引擎兼容。

关于javascript - Greasemonkey 1.0中的jQuery与使用jQuery的网站冲突,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12146445/

10-12 00:21
查看更多