gpt4 book ai didi

javascript - 如何在 Firefox 扩展中使用 jQuery

转载 作者:IT王子 更新时间:2023-10-29 02:56:25 28 4
gpt4 key购买 nike

我想在 firefox 扩展中使用 jQuery,我像这样在 xul 文件中导入库:

<script type="application/x-javascript" src="chrome://myExtension/content/jquery.js"> </script>

但是 $() 函数在 xul 文件中无法识别,jQuery() 也无法识别。

我用谷歌搜索了这个问题并找到了一些解决方案,但没有人与我合作: 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 函数,如下所示:

$('img',content.document);

但还是不行,以前有人遇到过这个问题吗?

最佳答案

我使用以下 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);

})();

关于javascript - 如何在 Firefox 扩展中使用 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/491490/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com