【问题标题】:jQuery in Greasemonkey 1.0 conflicts with websites using jQueryGreasemonkey 1.0 中的 jQuery 与使用 jQuery 的网站冲突
【发布时间】:2012-08-22 04:54:37
【问题描述】:

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

我已经阅读了文档,但仍然不知道如何在沙盒中再次运行 GS 脚本;唯一的选择似乎是要么授予对 GS API 的访问权限,要么将其授予 none 并在没有任何安全限制的情况下运行脚本,当我设计要运行的数十个 GS 脚本时,这对我来说根本不起作用有安全限制,喜欢这样。

【问题讨论】:

  • 我将在@grant 指令中输入什么内容?这不是只适用于 Greasemonkey API 吗?我怎样才能让@require <jQuery> 在沙箱中工作?
  • 只要使用@grant GM_getValue,就可以了。即使您实际上没有调用GM_getValue()@grant 指令也有恢复沙盒的副作用;所以@require 应该恢复正常工作。
  • 啊,是的,我认为这是一种选择。只是一种解决方法,但至少它是一种选择。不过我现在仍然坚持使用 0.9,因为我有太多需要更改的脚本,我宁愿让它们保持不变,直到 GS 解决这个问题。
  • 好吧,现在我收到了来自我的脚本的几个用户的支持请求。请file a bug report with GM devs 和/或评论this bug
  • GS 1.0 于 8 月 24 日左右发布。自 8 月 27 日左右以来,他们一直被 GS 1.0 问题所淹没。看起来最终会有所作为。看起来他们也在 GS 博客上发布了一些关于此的内容。

标签: javascript jquery firefox greasemonkey tampermonkey


【解决方案1】:

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 等高级引擎兼容。

【讨论】:

    【解决方案2】:

    两年过去了,这个“功能”仍然没有被充分记录或解决。

    需要 jQuery 的脚本仍然与一些使用 jQuery 的页面发生冲突。

    可能的解决方案是:

    1. 添加@grant GM_log 或类似的沙盒脚本
    2. 通过添加此块为脚本中现有的 jQuery 对象添加别名:

      var my_jquery = jQuery;

      jQuery.noConflict(true);

      var $ = my_jquery, jQuery = my_jquery;

    3. 不要使用 jQuery

    总而言之,Greasemonkey 做出了一个糟糕的决定。

    【讨论】:

      【解决方案3】:

      遇到同样的问题。

      自 GM 版本 1.0 以来,我在同样使用 jQuery 的网站上运行的所有 @require jQuery 脚本都停止工作。

      我知道,我可以试试 $ = unsafeWindow.$ 之类的东西,但这不是重点。

      这里的重点是他们曾经工作,现在他们不会了。

      添加@grant GM_log 修复了它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多