當我嘗試在我的擴展中包含jQuery庫時,JS引擎給我一個錯誤。這是代碼:Chrome擴展 - 爲什麼JS會抱怨,當我包含jQuery庫?
chrome.tabs.executeScript(null,{code:"
if (typeof jQuery == 'undefined') {
var script = document.createElement('script');
script.setAttribute('type') = 'text/javascript';
script.setAttribute('src') = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
};
"});
和這個結果:
Uncaught ReferenceError: Invalid left-hand side in assignment (anonymous function)
這段代碼被寫入popup.js文件,包括在由popup.html。
甚至當我做這樣的事情:
chrome.tabs.executeScript(null, {file: "browser.js", allFrames: true}, window.close());
,並在browser.js我指定以上(其中包括我的jQuery庫)編寫的代碼,它發生同樣的東西。最奇怪的是jQuery看起來並沒有被定義,甚至當代碼執行的選項卡已經刪除它時(例如Flickr)。事實上,typeof jQuery總是被設置爲'undefined'。我很困惑...
是的,我在第一時間就這樣做了。但是我有一些瘋狂的錯誤,比如「jQuery沒有定義」,所以我決定通過經典的JS sintax - 事實上這些錯誤再也沒有出現過。事實上,我無法在我的擴展中包含jQuery! – Gianluca 2012-07-27 16:39:45
http://stackoverflow.com/q/4947510/139010或http://stackoverflow.com/q/8745463/139010和[更多](http://stackoverflow.com/search?q=chrome+extension+ jQuery) – 2012-07-27 16:48:53
我很抱歉爲這樣一個愚蠢的問題困擾你。我只是不知道「content_scripts」參數的存在。非常感謝你,你非常有幫助。 – Gianluca 2012-07-27 20:03:40