简体   繁体   English

如何在没有Window-object的firefox附加组件/扩展中插入jQuery?

[英]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: 我尝试了几种方法,但都失败了:

  • Retrieve the window object from an active tab (Failed to send the window-element because the sendMessage() method is part of the window-object) 从活动选项卡中检索窗口对象(无法发送window-element,因为sendMessage()方法是window-object的一部分)
  • document.createElement (Failed because there is also no document-object) document.createElement(失败,因为也没有文档对象)
  • 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? 所以我的问题是,是否有人在Fi​​refox附加组件的主要方法中成功安装了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.

相关问题 javascript:window-object未定义 - javascript: window-object is undefined 如何使用附加组件生成器为firefox-extension-developlment添加jquery插件 - how to add a jquery plugi for firefox-extension-developlment using add-on builder 使用jQuery将Chrome扩展程序移植到Firefox SDK插件 - Porting Chrome extension using jQuery to Firefox SDK Add-on 如何在不打开任何窗口的情况下使用Firefox附加SDK解析html字符串 - how to parse a html string with Firefox add-on SDK without opening any window 如何使用Firefox附加组件中的jQuery和Javascript? - How can I use jQuery and Javascript from firefox add-on? 使用jQuery在Firefox附加组件中检查和取消选中菜单项 - Checking and unchecking menuitem in a Firefox add-on with jQuery 为什么我的Firefox扩展程序不使用从Firefox 4的网页中提取的jQuery 1.4.2对象添加事件处理程序 - Why does my Firefox extension not add event handlers using jQuery 1.4.2 object pulled from webpage in Firefox 4 Firefox附加组件堆积在循环中:( - Firefox add-on stacked on the loops :( 如何动态更改 Firefox 附加组件的页面工作器的 ContentURL? - How to dynamically change the ContentURL of a page-worker for Firefox Add-on? 如何在Firefox扩展中使用jQuery - How to use jQuery in Firefox Extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM