Is it possible to use jQuery to manipulate XUL elements?

前端 未结 6 853
一向
一向 2020-12-08 05:30

I know its possible to integrate jQuery within Firefox addons, but are we able to manipulate (animate, move, adjust transparency, etc) XUL elements themselves?

From

相关标签:
6条回答
  • 2020-12-08 06:07

    Unfortunately, jQuery fails to initialize in XUL because of the HTML-specific functions like document.body that exist for HTML pages only. However there is a special branch called jQuery-xul that it optimized for use in FireFox extensions. I have checked, it works fine.

    0 讨论(0)
  • 2020-12-08 06:08

    XUL and HTML actually deal with opacity the exact same way, and it is jQuery that incorrectly detects what the browser can do. jQuery thinks it's in a different browser when it's living inside XUL, and so opacity effects are handled differently - by jQuery. Since it IS in Firefox, and it should deal with opacity normally, you can override this like so:

    jQuery.support.opacity = true

    Do this right after jQuery is loaded.

    There is probably a whole category of similar fixes that can be post-applied to jQuery to make it behave better, but I haven't looked in to it.

    0 讨论(0)
  • 2020-12-08 06:12

    My explanation refers to jQuery 1.7.1.

    My goal was to perform a fade animation within the XUL context, but errors were being thrown because jQuery wrongly assumes it is dealing with IE if jQuery.support.opacity is falsy.

    The logic that populates the support Object was prematurely returning an empty Object due to the inability to interact with a DOM element created via document.createElement( "div" ). My problem was solved by using document.createElementNS:

    createElementNS("http://www.w3.org/1999/xhtml", "div");
    

    I searched the jQuery bug tracker after coming up with this solution and found the following related ticket: http://bugs.jquery.com/ticket/5206

    jQuery isn't advertised as working within the XUL context and therefore the team has no plans of addressing the issue, though they should address the issue of the IE false positive. I am content with manually making this one line change in the source code for my application.

    Cheers, Andy

    0 讨论(0)
  • 2020-12-08 06:18

    I was wondering this myself recently. Obviously the DHTML doesn't make sense, but the basic syntactic sugar and things are rumored to work.

    • There is this jQuery on XUL discussion from the jQuery group which indicates that it loads up with some exceptions.
    • Also see this slightly more recent blog post about jQuery and DHTML in XUL. That person is going after the HTML within the XUL which is not exactly what you need but he does have good information.
    0 讨论(0)
  • 2020-12-08 06:20

    Short answer: Yes

    Long Answer:

    I experimented with it myself. I could only use it in a limited way. Manipulation of XUL elements is possible. But I am having a hard time observing events, since I am pretty new to jQuery - I don't know how to tweak it to observe events at Firefox/XUL level - I don't even know whether tweaking is required :D

    Example:

    overlay.xul

    <?xml version="1.0"?>    
    <?xml-stylesheet href="chrome://testaddon/content/overlay.css" type="text/css"?>
    
    <overlay id="testaddon_overlay"
            xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
            xmlns:html="http://www.w3.org/1999/xhtml">
    
        <script type="application/x-javascript" src="chrome://testaddon/content/jquery-1.4.2.js" />
        <script type="application/x-javascript" src="chrome://testaddon/content/overlay.js" />
    
        <toolbox id="navigator-toolbox">
            <toolbar toolbarname="TestAddonToolbar" class="chromeclass-toolbar">
                <toolbaritem>
                    <toolbarbutton id="btnHide" label="HideMe" onclick="hideMe();" />
                </toolbaritem>            
            </toolbar>
        </toolbox>
    
    </overlay>
    

    overlay.js

    function hideMe()
    {
        $('#btnHide').hide();
    }
    

    Above code will hide the button when you click on it - basic XUL manipulation with jQuery!

    But as I said, try to observe document load and other such events and it gets complicated very quickly (or I don't know much :D).

    Update: I tried some effects. Effects.fadeIn() works - but since the transparency properties are set differently in XUL when compared to HTML, the button stays there and in the end, it abruptly disappears. Now it is becoming clear to what extent we can (can't) use jQuery to manipulate XUL.

    0 讨论(0)
  • 2020-12-08 06:22

    The answer right now is "mostly".

    Basic stuff works, including selecting, deleting and finding. I use 1.5.2 pretty heavily both inside an extension and on content pages from the extension.

    $.ready() also doesn't work because jQuery assumes and waits for a body. This is the commit that broke it.

    Some stuff doesn't work because, as @Daniel describes, jQuery's doesn't properly detect what it can and can't do in XUL (more background below). From stepping through jQuery code I have have found you need at least the following lines in your own code.

    // Workarounds for jQuery not properly testing support in XUL environment
    
    // These are done at jQuery init
    // This is actually critical, otherwise elements are not properly removed from the cache and you get a cache[id] is undefined
    crowdmash.$.support.deleteExpando = true;
    
    // These are done at ready(), but jQuery never fires ready in XUL
    // XUL doesn't seem to support offsetWidth and offsetHeight (without this :hidden is broken which breaks fadeIn)
    crowdmash.$.support.reliableHiddenOffsets = false;
    crowdmash.$.support.opacity = true;
    

    If you want to use jQuery on a content page from an extension, see my tips.

    jquery-xul modifies 1.4.4 to better work with XUL. I have not tried it yet, but from looking at its changes it does look like fixes the ready issue. I don't know if it fixes the support issue .

    As background on the $.support issue. The problem is that jQuery's feature detection creates a and then populates it using innerHTML. XUL divs do not support innerHTML, so the support code fails marking nothing as supported. There is actually a ticket on this that is open and being discussed for 1.7 - http://bugs.jquery.com/ticket/5206.

    0 讨论(0)
提交回复
热议问题