2011-12-27 91 views
3

對於我正在編寫的腳本,我需要使用包含3個JavaScript文件的第三方JavaScript庫。由於@require在Chrome上不起作用,我如何將多個外部JavaScript庫添加到用戶腳本中?我正在考慮所有可能性,然後再選擇一個。如何在腳本中集成第三方JavaScript庫

我知道你可以使用this方法添加jQuery。我親自使用過。是否有可能使用這種解決方法添加其他庫?

回答

0
function requireFiles(jsLibs, callback) 
{ 
    //array to hold the external libabry paths 
    var jsLibs = new Array(); 
    jsLibs[0] = "http://code.jquery.com/jquery-1.7.1.min.js" 
    jsLibs[1] = "https://raw.github.com/gildas-lormeau/zip.js/master/WebContent/zip.js" 
    jsLibs[2] = "https://raw.github.com/gildas-lormeau/zip.js/master/WebContent/deflate.js" 
    jsLibs[3] = "https://raw.github.com/gildas-lormeau/zip.js/master/WebContent/inflate.js" 

    var index = 0; 
    var requireNext = function() 
    { 
     var script = document.createElement("script"); 
     if (index < jsLibs.length) 
     { 
      script.addEventListener("load", requireNext, false); 
      script.setAttribute("src", jsLibs[index++]); 
     } 
     else 
     { 
      script.textContent = "(" + callback.toString() + ")()"; 
     } 
     document.body.appendChild(script); 
    } 
    requireNext(); 
} 


function otherCode() 
{ 
    //rest of the script 
} 

requireFiles(otherCode); 
3

嘗試將按照您的userscript功能,

/** 
* Dynamically loading javascript files. 
* 
* @param filename url of the file 
* @param callback callback function, called when file is downloaded and ready 
*/ 
function loadjscssfile(filename, callback) { 
    var fileref = document.createElement('script') 
    fileref.setAttribute("type", "text/javascript") 
    fileref.setAttribute("src", filename) 
    if (fileref.readyState) { 
     fileref.onreadystatechange = function() { /*IE*/ 
      if (fileref.readyState == "loaded" || fileref.readyState == "complete") { 
       fileref.onreadystatechange = null; 
       callback(); 
      } 
     } 
    } else { 
     fileref.onload = function() { /*Other browsers*/ 
      callback(); 
     } 
    } 

    // Try to find the head, otherwise default to the documentElement 
    if (typeof fileref != "undefined") 
     (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(fileref) 
} 

由於文件異步加載和文件的加載順序是沒有保證的,比 多個外部文件,調用這個函數在chined功能,例如

loadjscssfile("http://code.jquery.com/jquery-1.6.3.min.js", function() { 
    loadjscssfile("http://www.abc.org./script/otherFile.js", function() { 
     // call your function that depends on the external libriries here. 
    }); 
}); 

根據需要鏈接儘可能多的exeternal文件,加載文件的順序將被正確保存。希望這可以幫助,所有最好的

+0

非常感謝您發佈解決方案!對此,我真的非常感激。 :)我根據自己的情況修改了你的代碼。由於它有點冗長,我在[這裏]發佈了它(http://paste2.org/p/1851096)。你可以請通過代碼,讓我知道如果我做錯了什麼?你能解釋一下這一行代碼嗎?因爲我是JS新手,所以我沒有完全掌握所有內容。'if(typeof fileref!=「undefined」)'再次感謝。 – Isuru

+1

嗨,我瀏覽了一下你的代碼。我實際上用一個額外的括號粘貼了代碼。行 'if(typeof fileref!=「undefined」) (document.getElementsByTagName(「head」)[0] || document.documentElement).appendChild(fileref)' 屬於loadjscssfile函數。實際上,我修改了原始功能並粘貼了只下載了javascript的部分。最初,該函數還下載了外部CSS,這是typeof filref的來源, 它檢查javascript對象是否存在。你不需要這個檢查,因爲fileref將永遠是一個腳本元素對象, 最好 –

+0

哦,我現在明白了。非常感謝您的幫助。高度欣賞它! :) – Isuru

相關問題