2011-05-11 5 views
0

나는 새로운 사용자 스크립트를 크롬과 파이어 폭스 모두에서 작동하도록 작성하고 jquery를 실행하는 jquery 래퍼를 작성하려고합니다. 이전 필자의 사용자 스크립트는 Joan Piedra의 방법 (http://joanpiedra.com/jquery/greasemonkey/)을 사용했으나 Chrome에서 unsafewindow를 지원하지 않기 때문에 Chrome에서 작동하지 않습니다.

그래서 Chrome에서 jQuery를 실행하는 Erik Vold의 방법 (http://erikvold.com/blog/index.cfm/2010/6/14/using-jquery-with-a-user-script)을 찾았지만 기존 사용자 스크립트와 함께 Firefox에서는 전혀 작동하지 않습니다.

jquery에 대한 첫 번째 검색을 위해 Erik의 스크립트를 적용하려고 시도한 다음 jquery가 정의되지 않은 경우에만 스크립트 태그를 삽입하기로 결정했습니다. jquery가 있으면 jquery 스크립트 태그를 삽입하지 않고 jquery 콜백을 실행합니다 (충돌이없는 모드에서는이 태그가 scriptaculous 페이지에서도 실행되기를 기대하기 때문에).

내 문제는 먼저, 많은 수의 요구 사항이 주어진다. addJQuery의 if/else 문에있다. jquery 객체가 '정의되지 않아야'하는 명령문의 else 부분에서 jquery 객체가 여전히 존재한다. 실제로 '정의되지 않은'그래서 콜백이 실행될 때 '$'는 정의되지 않은 오류가 발생합니다.

Erik의 원본 코드는 jquery 스크립트를로드 할 때 이벤트 리스너를 연결했지만 jquery 이벤트가 이미 있지만로드되지 않은 경우이를 수행하는 방법을 알지 못합니다 (이벤트 리스너를 윈도우에 연결하려고했습니다. , document.head 및 document.body 및 아무것도 작동하지 않습니다.)

나는 또한 window.setTimeout 사용하여 시도했지만 동일한 결과 ($ 정의되지 않은 한 번 이상 반복 할 가져올 수 없습니다. .)

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); 

답변

0

:

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); 

     } 

    } 
+1

감사합니다. 그 방법을 여러 번 시도했지만 작동하지 않았습니다. 또한 unsafeWindow를 사용하여 Chrome에서 실행할 수 없습니다. –

0

나는 에릭 Vold 사용자의 스크립트에 라인 $ = jQ.noConflict(true);을 추가했습니다. 이것은 나를 위해 작동합니다!

// ==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);