简体   繁体   中英

How to insert jQuery in a firefox Add-On / extension without the Window-object?

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?

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 wrt 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM