代码之家  ›  专栏  ›  技术社区  ›  Greg K

Greasemonkey@require jQuery不工作“组件不可用”

  •  10
  • Greg K  · 技术社区  · 16 年前

    我见过 the other question on here 关于在Greasemonkey中加载jQuery。尝试过这个方法后,在我的 ==UserScript== 标签:

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

    Error: Component is not available
    Source File: file:///Users/greg/Library/Application%20Support/
           Firefox/Profiles/xo9xhovo.default/gm_scripts/myscript/jquerymin.js
    Line: 36
    

    这会阻止我的greasemonkey代码运行。我已经确定我包括了 @require 对于jQuery,请在安装之前保存我的js文件,因为只在安装时加载所需的文件。

    代码:

    // ==UserScript==
    // @name           My Script
    // @namespace      http://www.google.com
    // @description    My test script
    // @include        http://www.google.com
    // @require        http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
    // ==/UserScript==
    
    GM_log("Hello");
    

    我的 config.xml

    <UserScriptConfig>
    <Script filename="myscript.user.js" name="My Script" 
     namespace="http://www.google.com" description="My test script" enabled="true" 
     basedir="myscript">
        <Include>http://www.google.com</Include>
        <Require filename="jquerymin.js"/>
    </Script>
    

    我可以看到jquerymin.js坐在 gm_scripts/myscript/ 目录

    Error: not well-formed
    Source File: file:///Users/Greg/Documents/myscript.user.js
    Line: 1, Column: 1
    Source Code:
       // ==UserScript==
    
    8 回复  |  直到 9 年前
        1
  •  6
  •   Anders    16 年前

    好的,所以我更深入地研究了这个问题。我完全使用了您的脚本,但使用了我们的JQuery版本,使其看起来像这样:

    // ==UserScript==
    // @name           My Script
    // @namespace      http://www.google.com
    // @description    My test script
    // @include        http://www.google.se/*
    // @include        http://www.dn.se/*
    // @require        http://myserver/jquery-1.3.2.js
    // ==/UserScript==
    
    GM_log("Hello");
    

    这对我来说很好,我猜,google api上的JQuery缺少一些函数。因为上面的代码工作得很好。还要注意 /*

    尝试另一个JQuery并更改URL,它应该可以正常运行。

        2
  •  7
  •   Lam Chau    15 年前

    我找到了一种将它与jQuery1.4.1结合使用的非理想方法——这似乎可以解决这个问题。这是最重要的 new browser sniffing 这似乎“打破”了它。

    jquery-1.4.1.min.js:

      [old]  36: var o=r.createElement("div");n="on"+n;var m=n in o;
      [new]  36: var o=r.createElement("div");n="on"+n;var m=true;
    

    jquery-1.4.1.js

      [old] 934: var isSupported = (eventName in el);
      [new] 934: var isSupported = true;
    
        3
  •  6
  •   oeuftete    16 年前

    http://forum.jquery.com/topic/importing-jquery-1-4-1-into-greasemonkey-scripts-generates-an-error

    在我看来,这似乎是这个问题的最终答案,以及如何解决这个问题。这个变通办法对我很有效。

        4
  •  3
  •   Mat    13 年前

    上面的补丁允许在Greasemonkey脚本中运行1.5.2之前的jQuery版本,但幸运的是,如果使用当前的jQuery 1.5.2版本,则不再需要该补丁。

    我检查了它的代码,注意到eventSupported函数代码在jQuery中

    var eventSupported = function(eventName) { ... }
    

        5
  •  2
  •   Tom    15 年前

    jquery-1.4.3.min.js的补丁程序

    [旧]第41行 u、 createElement(“div”);s=“on”+s;变量 B=v中的s;
    [新]第41行 B=正确;

        6
  •  0
  •   Nick Craver    16 年前

    @require属性在Greasemonkey和jQuery中无法正常工作……FireBug中也可能出现同样的错误。

    Here's how to do that .

        7
  •  0
  •   Johan    16 年前

    因此,恢复到1.3.2确实可以做到这一点,但我更愿意找到一个可以使用1.4的解决方案。

    顺便说一句,我用这个,有点不同:

    // @require http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js
    
        8
  •  0
  •   yah.avatar    15 年前