如何在Firefox扩展中使用jQuery

编程入门 行业动态 更新时间:2024-10-07 02:26:26
本文介绍了如何在Firefox扩展中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

< script type =application / x-javascriptsrc =chrome://myExtension/content/jquery.js> < /脚本>

但是在xul文件中无法识别$()函数,jQuery() / p>

我搜索了这个问题,发现了一些解决方案,但没有人和我合作: gluei/blog/view/using-jquery-inside-your-firefox-extension forums.mozillazine/viewtopic.php?f= 19& t = 989465

我也尝试将'content.document'对象(它引用'document'对象)作为上下文参数的jQuery功能是这样的:

$('img',content.document);

但是仍然无效,是否有人遇到过这个问题?

解决方案

我使用下面的 example.xul :

<?xml version =1.0?> < overlay id =examplexmlns =www.mozilla/keymaster/gatekeeper/there.is.only.xul> < head>< / head> < script type =application / x-javascriptsrc =jquery.js>< / script> < script type =application / x-javascriptsrc =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){ //检查网站 if(!doc.location.href.match(/ ^ http:\ / \ / * \。)?stackoverflow\(\ /.*)?$/ i)) return; //检查是否已经加载 if( doc.getElementById(plugin-example))return; //安装 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>'); }; //绑定插件 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); })();

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>

but the $() function is not recognized in the xul file neither do the jQuery().

I googled about the problem and found some solutions but no one did work with me: gluei/blog/view/using-jquery-inside-your-firefox-extension forums.mozillazine/viewtopic.php?f=19&t=989465

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?

解决方案

I use the following example.xul:

<?xml version="1.0"?> <overlay id="example" xmlns="www.mozilla/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>

And here is an 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\(\/.*)?$/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

本文发布于:2023-11-28 06:23:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Firefox   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!