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?

4

1 回答 1

4

Lib/main.js不是您必须放置应用程序代码的地方。在那里你会放你的初始化代码。就像我们在 chrome 的manifest.json. 看看我的 Firefox 扩展main.js。它看起来像这样:

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')]

});

pageMod允许您在页面的上下文中加载您的 javascript,这当然是在页面自己的上下文中的一个单独的世界中。

../data您希望加载的脚本应位于lib/.

看看我的插件的文件夹结构。https://github.com/juzerali/Don-t-lose-your-query/tree/master/Firefox-Addon。我记错了,但您可能需要包含 api-utils。

我发现使用 Firefox 的 SDK 很有帮助。

于 2013-02-25T14:54:13.770 回答