如何在Firefox扩展中使用jQuery

57 javascript firefox jquery firefox-addon

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

<script type="application/x-javascript" src="chrome://myExtension/content/jquery.js"> </script>
Run Code Online (Sandbox Code Playgroud)

但是在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);
Run Code Online (Sandbox Code Playgroud)

但仍然没有工作,有没有人遇到过这个问题?

小智 28

我使用以下内容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>
Run Code Online (Sandbox Code Playgroud)

这是一个 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);

})();
Run Code Online (Sandbox Code Playgroud)

  • 这种方法是绝对不合适的,因为当您从溢出加载jquery时,然后为整个窗口定义了2个对象jQuery和$,并可能导致与其他扩展冲突.虽然您可以使用jquery.noconflict和函数作用域来隐藏它,但可以异步加载叠加层.在我可以使用jquery.noconflict之前,还有可能已经定义了另一个firefox的扩展并在窗口上使用了jQuery和$ (3认同)

Dav*_*vid 10

以下解决方案使得在contentScriptFile中使用jQuery成为可能(Targetting 1.5 Addon-sdk)

在你的main.js中:

exports.main = function() {
    var pageMod = require("page-mod");

    pageMod.PageMod({
          include: "*",
          contentScriptWhen: 'end',
          contentScriptFile: [data.url("jquery-1.7.1-min.js") , data.url("notifier.js") ,   data.url("message.js")],
          onAttach: function onAttach(worker) {
             //show the message
             worker.postMessage("Hello World");
          }
    });

};
Run Code Online (Sandbox Code Playgroud)

在你的message.js中:

self.on("message", function(message){
    if(message !== "undefined"){
       Notifier.info(message); 
    }
});
Run Code Online (Sandbox Code Playgroud)

您需要注意的一些陷阱:

  • contentScriptFile数组的顺序.如果首先放置message.js:jQuery将不会被重新调整.
  • 不要在data.url中放置http:// url (这不起作用)!
  • 您的所有javascript文件都应该在数据文件夹中.(只有main.js应该在lib文件夹中)