3

I am porting an extension from Google Chrome into Firefox's Addon SDK (v1.9). Since it uses jQuery on the background page (main.js on Firefox), I would like to use that library as well on the Firefox version. However, since the window object is not accessible from the addon code itself (and jQuery is widely based on that), I was wondering if there was a better way of implementing the jQuery library from the addon code. Maybe there is a way to import jQuery as a module using "require('jquery')".

Just to clarify, I am aware of how to implement jQuery on content scripts. What I am trying to do is use jQuery on the addon code itself, such as "main.js" (or whatever name you give the background "main" module).

5
  • 1
    There is no "background window", the modules execute in a sandboxed context without any windows. Unless I am missing somebody jQuery would be absolutely useless in that context even if you managed to import it. Commented Aug 23, 2012 at 6:25
  • I suppose 1 slight use-case in chrome would be cross-domain requests. In the Add-on SDK you would instead use the request module for that: addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/… Commented Aug 23, 2012 at 19:17
  • I just had some simple functions such as $.each() and $.ajax() that were being used in the Google Chrome version of the addon. I guess I will have to implement them without jQuery. Commented Aug 24, 2012 at 15:56
  • For $.ajax, use the request module. For $.each or similar, you can use the Array object's forEach iterator pattern, or a for loop, or... Commented Aug 25, 2012 at 15:58
  • Unless I misunderstand (or this feature has been added since the question was posted :) ) why not just use pageMod with contentScriptFile ? That will let you inject jquery into the addon's content script code which has access to the page content. Then you can use port messaging to communicate between the addon and the contents script. Commented Jan 17, 2016 at 1:41

2 Answers 2

2

The conclusion is that it is not possible (or very hard) to include.

However, as mentioned, it is pointless in most cases (since we dont have access to the window object from the background page).

Sign up to request clarification or add additional context in comments.

Comments

1

I'm also porting an extension from Chrome to Firefox, and managed to come up with this:

var {Cc, Ci} = require("chrome");
_window = Cc["@mozilla.org/appshell/appShellService;1"]
    .getService(Ci.nsIAppShellService).‌​hiddenDOMWindow;

$ = require('jquery')(_window);

There's a little more detail here.

Nowadays there are lots of uses for jQuery in a non-DOM context, especially Deferreds and ajax stuff, as well as utilities like extend. It would be frustrating to have to rewrite cross-browser jQuery code to be Firefox-specific, e.g. the request module.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.