如何在 Firefox 扩展中使用 jQuery [英] How to use jQuery in Firefox Extension
问题描述
我想在 firefox 扩展中使用 jQuery,我在 xul 文件中导入了这个库,如下所示:
I want to use jQuery inside a firefox extension, I imported the library in the xul file like this:
<script type="application/x-javascript" src="chrome://myExtension/content/jquery.js"> </script>
但是 $() 函数在 xul 文件中无法识别,jQuery() 也无法识别.
but the $() function is not recognized in the xul file neither do the jQuery().
我用谷歌搜索了这个问题并找到了一些解决方案,但没有人与我合作:http://gluei.com/blog/view/using-jquery-inside-your-firefox-extensionhttp://forums.mozillazine.org/viewtopic.php?f=19&t=989465
I googled about the problem and found some solutions but no one did work with me: http://gluei.com/blog/view/using-jquery-inside-your-firefox-extension http://forums.mozillazine.org/viewtopic.php?f=19&t=989465
我还尝试将content.document"对象(引用document"对象)作为上下文参数传递给 jQuery 函数,如下所示:
I've also tried to pass the 'content.document' object(which refrences the 'document' object) as the context parameter to the jQuery function like this:
$('img',content.document);
但还是不行,有没有人遇到过这个问题?
but still not working, does any one came across this problem before?
推荐答案
我使用以下 example.xul
:
<?xml version="1.0"?>
<overlay id="example" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head></head>
<script type="application/x-javascript" src="jquery.js"></script>
<script type="application/x-javascript" src="example.js"></script>
</overlay>
这里是一个 example.js
(function() {
jQuery.noConflict();
$ = function(selector,context) {
return new jQuery.fn.init(selector,context||example.doc);
};
$.fn = $.prototype = jQuery.fn;
example = new function(){};
example.log = function() {
Firebug.Console.logFormatted(arguments,null,"log");
};
example.run = function(doc,aEvent) {
// Check for website
if (!doc.location.href.match(/^http://(.*.)?stackoverflow.com(/.*)?$/i))
return;
// Check if already loaded
if (doc.getElementById("plugin-example")) return;
// Setup
this.win = aEvent.target.defaultView.wrappedJSObject;
this.doc = doc;
// Hello World
this.main = main = $('<div id="plugin-example">').appendTo(doc.body).html('Example Loaded!');
main.css({
background:'#FFF',color:'#000',position:'absolute',top:0,left:0,padding:8
});
main.html(main.html() + ' - jQuery <b>' + $.fn.jquery + '</b>');
};
// Bind Plugin
var delay = function(aEvent) {
var doc = aEvent.originalTarget; setTimeout(function() {
example.run(doc,aEvent);
}, 1);
};
var load = function() {
gBrowser.addEventListener("DOMContentLoaded", delay, true);
};
window.addEventListener("pageshow", load, false);
})();
这篇关于如何在 Firefox 扩展中使用 jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!