当我尝试在我的扩展中包含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