greasemonkey、jquery和$未定义--与chrome兼容

greasemonkey、jquery和$未定义--与chrome兼容,jquery,firefox,google-chrome,greasemonkey,Jquery,Firefox,Google Chrome,Greasemonkey,我正在尝试编写一个jquery包装器,它将允许我正在编写的新用户脚本在chrome和firefox中与旧用户脚本一起运行,并运行jquery。我以前的用户脚本使用了Joan Piedra的方法(),但这在Chrome中不起作用,因为Chrome不支持unsafewindow 所以我找到了Erik Vold在Chrome()中运行jQuery的方法,但是它在firefox中与现有的用户脚本一起根本不起作用 我决定尝试调整Erik的脚本以首先搜索jquery,然后仅在jquery未定义时插入脚本标记

我正在尝试编写一个jquery包装器,它将允许我正在编写的新用户脚本在chrome和firefox中与旧用户脚本一起运行,并运行jquery。我以前的用户脚本使用了Joan Piedra的方法(),但这在Chrome中不起作用,因为Chrome不支持unsafewindow

所以我找到了Erik Vold在Chrome()中运行jQuery的方法,但是它在firefox中与现有的用户脚本一起根本不起作用

我决定尝试调整Erik的脚本以首先搜索jquery,然后仅在jquery未定义时插入脚本标记。如果jquery存在,它不会插入jquery脚本标记,而只是运行jquery回调(在无冲突模式下,因为我希望它也会在带有scriptaculous的页面上运行)

我的问题(许多问题中的第一个,考虑到我的需求数量,我确信)在addJQuery中的if/else语句中:在语句的else部分(jquery对象不应该是“未定义的”),jquery对象实际上仍然是“未定义的”,因此在回调运行时,我得到了“$”未定义的错误

Erik的原始代码将事件监听器附加到jquery脚本的加载中,但我不知道如何在jquery已经存在但不一定已加载的事件中执行此操作(我尝试将事件监听器附加到窗口、文档、document.head和document.body,但没有任何效果)

我也尝试过使用window.setTimeout,但我无法让它重复多次,结果相同($未定义)

我还将提到,在我知道jQuery不存在的情况下,脚本确实成功地添加了script标记,并且我已将事件侦听器附加到脚本中,但仍然会出现错误,$未定义

所以我现在完全不知所措。我无法检测jQuery,即使在我似乎已经检测到jQuery的时候,我也无法运行代码,即使在我似乎检测到jQuery的时候,它仍然会中断

function addJQuery(callback) {
if(typeof jQuery == 'undefined'){
    var libsrc = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js";
    var dochead = head = document.getElementsByTagName('head')[0];
    var jqscript = document.createElement("script");
    jqscript.setAttribute("type","text/javascript");    
    jqscript.setAttribute("src", libsrc);

    // append script tag as first element in the head 
    dochead.insertBefore(jqscript, head.childNodes[0]);

    jqscript.addEventListener('load', function(){
        var runscript = document.createElement("script");

        // force no conflict mode allowing $
        runscript.textContent = "jQuery.noConflict(); (function($) {   $(function() {" + callback.toString() +  "}); })(jQuery);";
        document.getElementsByTagName('head')[0].appendChild(runscript);
        callback();
    }, false);
}
else{
    // jquery is still undefined?!?!?!?!
    var runscript = document.createElement("script");
    runscript.setAttribute("type", "text/javascript");  

    // force no conflict mode allowing $
    runscript.textContent = "jQuery.noConflict(); (function($) {       $(function() {" + callback.toString() +  "}); })(jQuery);";
    document.getElementsByTagName('head')[0].appendChild(runscript);
    callback();

}

}

function main() {
 alert($); // check if the dollar (jquery) function works
 alert($().jquery); // check jQuery version
}

// load jQuery and execute the main function
addJQuery(main);

要解决$is undefined的问题,可以在执行所有代码之前执行以下操作:

function esperar() {

        if (typeof unsafeWindow.jQuery == 'undefined') {

            // Si aún no se ha cargado, esperar un poco más

            window.setTimeout(esperar, 100);



        } 

        else {

            $ = unsafeWindow.jQuery.noConflict(true);

        }

    }

我添加了行
$=jQ.noConflict(true)到Erik Vold的脚本。这对我有用

// ==UserScript==
// @name         jQuery For Chrome (A Cross Browser Example)
// @namespace    jQueryForChromeExample
// @include      *
// @author       Erik Vergobbi Vold & Tyler G. Hicks-Wright (modified)
// @description  This userscript is meant to be an example on how to use jQuery in a userscript on Google Chrome.
// ==/UserScript==

// loads jQuery with a callback when jQuery has loaded
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "$=jQuery.noConflict(true);(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

function main() {
    // rest of code goes here
}

// load jQuery and execute the main function
addJQuery(main);

谢谢,我已经试过好几次了,但都没有成功。另外,我不能使用unsafeWindow并让它在Chrome中运行。