【问题标题】:Greasemonkey Jquery interference? [duplicate]Greasemonkey Jquery 干扰? [复制]
【发布时间】:2012-10-19 22:15:46
【问题描述】:

可能重复:
jQuery in Greasemonkey 1.0 conflicts with websites using jQuery

看来,如果我的任何greasemonkey 脚本有像下面这样的行,我会在包含jquery 的网站上得到错误。我不知道为什么,但我确实注意到一些 javascript 事件完全没有触发。 (例如,我无法添加评论)。

我可以在脚本中排除 SO,但有更好的解决方案吗?也许我应该在测试 jquery 是否存在后动态添加 jquery。如何解决这个问题以及如何在不使用 jquery 的情况下将 jquery 脚本标签添加到正文?

// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js

【问题讨论】:

标签: javascript jquery greasemonkey


【解决方案1】:

Greasemonkey 1.0, radically changed the way the sandbox works,破坏了数千个脚本。另请参阅jQuery in Greasemonkey 1.0 conflicts with websites using jQuery

这是一个巨大的问题,Greasemonkey 的首席开发人员表示完全不愿意以明智的方式解决它。

要解决此问题,请将沙盒恢复到您的脚本,并解决 $ 冲突,方法是编辑您的 Metadata Block 以以以下行结尾

// @grant       GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a major design change introduced in GM 1.0,
    It restores the sandbox.
*/


指定 @grant 值(none 除外)会重新激活沙箱。


您也可以考虑切换到 Scriptish,它在过去提供了卓越的功能和性能,并且不受这种新沙盒行为的影响。

Scriptish,到目前为止,还没有发布破坏其大部分安装基础的更新。

【讨论】:

    【解决方案2】:

    只有当 jQuery 尚未包含时,您才需要包含它。所以你需要进行第一行检查。如果包含它,则运行脚本,如果它不包含,则添加类似的行以包含它,然后循环检查功能。像这样的:

    var wrote = false,
        runScript = function(){
        if(typeof(jQuery) === 'undefined'){
            if(!wrote){
                wrote = true;
                document.write('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>');
            }
            setTimeout(runScript, 50);
        }else{
            //your script goes here
        }
    };
    
    runScript();
    

    唯一的问题是如果 google 的 jquery 没有启动。

    【讨论】:

      猜你喜欢
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 2013-04-27
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多