[英]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. 当我决定将Chrome扩展程序移植到Firefox时,我遇到了重大问题。 One of the problems is that jQuery won't install itself in the "Lib/main.js" file. 其中一个问题是jQuery不会自己安装在“Lib / main.js”文件中。 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. 似乎没有在Firefox附加组件的主方法中定义窗口对象。
I understand that the extension itself doesn't need a committed window object because it doesn't represent an html page. 我理解扩展本身不需要提交的窗口对象,因为它不代表html页面。 But this makes it impossible to install jQuery while I want to take advantage of the ajax method and search-algorithm in dom elements. 但这使我无法安装jQuery,而我想利用dom元素中的ajax方法和搜索算法。
I've tried several methods but they all failed: 我尝试了几种方法,但都失败了:
So my question is, does anyone have successfully install jQuery in the main method of a Firefox Add-On? 所以我的问题是,是否有人在Firefox附加组件的主要方法中成功安装了jQuery?
Lib/main.js
is not where you have to put your application code. Lib/main.js
不是您放置应用程序代码的地方。 There you would put your initialization code. 你会在那里放置初始化代码。 Like we did in chrome's manifest.json
. 就像我们在chrome的manifest.json
所做的那样。 Take a look at my firefox extension's main.js
. 看看我的firefox扩展的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. pageMod
允许你在页面的上下文中加载你的javascript,这当然是在页面自己的上下文中的一个单独的世界里。
The scripts that you wish to load should reside inside ../data
with respect to lib/
. 您希望加载的脚本应驻留在与lib/
../data
。
Take a look at folder structure of my addon. 看看我的插件的文件夹结构。 https://github.com/juzerali/Don-t-lose-your-query/tree/master/Firefox-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. 我记不清楚了,但你可能需要包含api-utils。
I found it helpful to use Firefox's SDK. 我发现使用Firefox的SDK很有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.