简体   繁体   English

Firefox扩展中的jQuery

[英]jQuery in Firefox extension

I would like to include jQuery in a Firefox extension. 我想在Firefox扩展中包含jQuery。

I add the following line of code to import the jQuery file: 我添加以下代码行来导入jQuery文件:

Components.utils.import("resource://js/jquery.js", window.content.document);

Firefox runs the file immediately after importing. Firefox导入后立即运行该文件。 The jQuery file looks like this with an anonymous closures: jQuery文件看起来像这样,带有匿名闭包:

 (function( window, undefined ) {
        ...bunch of code....
       _jQuery = window.jQuery,
 })(window);

When the extension runs there is an error "window is not defined". 当扩展程序运行时,会出现错误“未定义窗口”。 What is a way to give jQuery access to the window? 有什么方法可以让jQuery访问窗口?

将此文件写入* .xul文件以包含jQuery。

<script type="application/x-javascript" src="toolbar.js"></script>

Not tested, but on normal websites, jQuery is loaded into the context of window . 没有经过测试,但在普通网站上,jQuery被加载到window的上下文中。 Therefore, you have to use window.content as scope: 因此,您必须使用window.content作为范围:

Components.utils.import("resource://js/jquery.js", window.content);

window.content.document does not contain a property window . window.content.document不包含属性window

如果它与greasemonkey userjs相同,请尝试使用unsafeWindow ,而不是window

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM