4

I'm having major problems when I decided to port a Chrome extension to Firefox. One of the problems is that jQuery won't install itself in the "Lib/main.js" file. The error I get is the following:

ReferenceError: window is not defined

It seems that the window object is just not defined at the main method of a Firefox Add-On.

I understand that the extension itself doesn't need a committed window object because it doesn't represent an html page. But this makes it impossible to install jQuery while I want to take advantage of the ajax method and search-algorithm in dom elements.

I've tried several methods but they all failed:

  • Retrieve the window object from an active tab (Failed to send the window-element because the sendMessage() method is part of the window-object)
  • document.createElement (Failed because there is also no document-object)
  • Some random stuff which also failed

So my question is, does anyone have successfully install jQuery in the main method of a Firefox Add-On?

Wezelkrozum
  • 831
  • 5
  • 15

1 Answers1

4

Lib/main.js is not where you have to put your application code. There you would put your initialization code. Like we did in chrome's manifest.json. Take a look at my firefox extension's main.js. It looks like this:

exports.main = function() {};

var { MatchPattern } = require("match-pattern");

var pageMod = require("page-mod");
var data = require("self").data;

pageMod.PageMod({
    include: [/.*phpminiadmin.*/, /.*phpmyadmin.*/, /.*devadmin.*/],
    contentScriptWhen: 'ready',
    contentScriptFile: [data.url('jquery-1.7.2.min.js'),data.url('jquery-ui-1.8.20.custom.min.js'),data.url('bootstrap.min.js'),data.url('querysaver.js')]

});

It is the pageMod that would allow you to load your javascript on a page's context, which is of course inside a separate world w.r.t the page's own context.

The scripts that you wish to load should reside inside ../data with respect to lib/.

Take a look at folder structure of my addon. https://github.com/juzerali/Don-t-lose-your-query/tree/master/Firefox-Addon. I don't remember correctly but you might be needing to include api-utils.

I found it helpful to use Firefox's SDK.

Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
  • I am not quiet satisfied with your answer. I'm looking for a way to execute my code in the background with a defined interval where in I can execute ajax requests but without a window. But Firefox doesn't seem to support that. – Wezelkrozum Apr 22 '13 at 18:45
  • Look at the code I've given in the answer. In the last line see how I am passing an array of data urls. I am loading jquery, bootstrap and my custom `querysaver.js`. All these files reside in data folder. I suppose you can access window object from there, coz if ain't there jquery will never load. Whether you are allowed to make an `XMLHttpRequest` from there is another matter. – Juzer Ali Apr 24 '13 at 10:16
  • Sorry, I've said it wrong. The difference with Firefox and Chrome is that a Chrome extension has a background page. Thats the location I let the extension poll for new messages. Now for me to read those messages I need to be able to use jQuery. But I need a window object in that background page to be able to load DOM elements into jQuery. What you show me now is how to add a script into a tab when it meets the required pattern. Ofcourse there you will have a window object. So, that already works for me so that is not the problem. – Wezelkrozum Apr 27 '13 at 11:37