5

At first I made a function that received a parameter and returned jQuery such as:

function getjQuery(window)
{
   /*jquery code*/(window);
   return window.jQuery;
}

But then I got an email form the review and they told me I have to use jQuery file with the original file name and completely unmodified.

I started to search for an alternative and found this solution, but there is no way it work.

jQuery object is created, but I can't find any elements. $("#id").length is always 0. With the previous method it was always found.

My current code (which doesn't work)

AddonNameSpace.jQueryAux = jQuery;

AddonNameSpace.$ = function(selector,context) { 
    return                                                 // correct window
        new AddonNameSpace.jQueryAux.fn.init(selector,context||contentWindow); 
};
AddonNameSpace.$.fn =
    AddonNameSpace.$.prototype = AddonNameSpace.jQueryAux.fn;
AddonNameSpace.jQuery = AddonNameSpace.$;

The jQuery file is loading on my browser.xul overlay:

<script type="text/javascript" src="chrome://addon/content/bin/jquery-1.5.2.min.js" />

Am I loading in the right place?

How can I use jQuery to modify the content on a page (HTML) with the original jQuery file, is it even possible?

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • possible duplicate of [How do I use jQuery in firefox addon?](http://stackoverflow.com/questions/5014105/how-do-i-use-jquery-in-firefox-addon) – Tyler May 03 '11 at 00:05
  • @Matrix Did you read the question? – BrunoLM May 03 '11 at 00:08
  • how are you loading jQuery (in your current code how do you get the jQuery variable)? Also, are you trying to use jQuery for XUL or for html pages loaded in the browser? – skabbes May 03 '11 at 00:48
  • @skabbes I am adding jQuery in `browser.xul` overlay. I want to use on HTML pages `gBrowser.addEventListener("DOMContentLoaded", function (e) { e.originalTarget.defaultView; /* this window */ }, false)` – BrunoLM May 03 '11 at 00:50

2 Answers2

3

You need pass the e.originalTarget.defaultView on the second parameter on jquery.. If you don't jquery will use window.document, which is the window.document from the xul.

Use

gBrowser.addEventListener("DOMContentLoaded", function (e) {
    $("#id", e.originalTarget.defaultView).length
}, true);

instead of

$("#id").length;

And, for avoid conflicts with other extensions don't use script in the xul page, use MozIJSSubScriptLoader.

Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader)
.loadSubScript("chrome://youraddon/content/jquery-1.5.2.min.js"); 

If you use this method, you load jquery only when you need, avoiding memory leak.

StiveKnx
  • 320
  • 4
  • 14
  • Nearly perfect. But for some reason I can't append elements. `$("
  • ").append("#pageNav")`, nor sending the second parameter (context). This is really annoying... Although I can set the `html` of the elements.
  • – BrunoLM May 03 '11 at 02:51