Firefox扩展在main.js文件中包含脚本 [英] Firefox extension include script in main.js file

查看:228
本文介绍了Firefox扩展在main.js文件中包含脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个Firefox的扩展,并有一个问题,包括脚本到main.js(背景)文件...

在我的情况下,我想包括例如jquery.js和config.js,但我看不出如何正确地做到这一点。

在我的Chrome扩展中,我只是在清单文件上做到这一点,但



感谢您的帮助,
jdmry

解决方案

要导入使用 Components.utils的文件.import



您将需要一个从中导入文件的URL。通常,这是一个资源://或chrome:// URL。

Mozilla提供的一些库例子:

  Components.utils.import(resource://gre/modules/FileUtils.jsm); // File Utilities 
Components.utils.import(resource://gre/modules/NetUtil.jsm); //网络工具
Components.utils.import(resource://gre/modules/Services.jsm); //服务

从您自己的扩展中加载的一些示例:

  Components.utils.import(chrome://myExtensionModule/content/common.jsm); // Common 
Components.utils.import(chrome://myExtensionModule/content/classes.jsm); //类

为了从您自己的扩展中加载,您需要将文件已被映射到chrome.manifest中的chrome://或resource:// URL的文件夹。例如:

  content myExtensionModule modules / 

这不需要与通常为内容映射的文件夹分开,但通常将JavaScript模块保存在单独的文件夹中,仅供组织使用。您不需要让他们使用.jsm扩展名,再次只是一个约定。如果你有其他的基于.js扩展名的自动配置工具(例如编辑器),使用.js就可以了。



可以在MDN的文档中找到:



请注意,在Firefox代码中,通常需要映射到较短的变量才能访问某些常用的Firefox属性。具体而言,常常使用以下内容:

  const Cc = Components.classes; 
const Ci = Components.interfaces;
const Cu = Components.utils;

通常这些是以更简化的方式定义的。他们在上面明确指出,以使映射更清晰。

I am writing an extension for firefox and having a problem including scripts into the main.js (background) file ...

In my case I would like to include for example jquery.js and config.js but I can't see how to do it properly.

In my chrome extension I just do that on the manifest file but I would like the equivalent in firefox.

Thank you for your help, jdmry

解决方案

To import files you use Components.utils.import.

You will need a URL from which to import the file. Usually, this is either a resource:// or chrome:// URL.

Some examples of Mozilla supplied libraries:

Components.utils.import("resource://gre/modules/FileUtils.jsm"); //File Utilities
Components.utils.import("resource://gre/modules/NetUtil.jsm");   //Network utilities
Components.utils.import("resource://gre/modules/Services.jsm");  //Services

Some examples of loading from your own extension:

Components.utils.import("chrome://myExtensionModule/content/common.jsm");          //Common
Components.utils.import("chrome://myExtensionModule/content/classes.jsm");         //Classes

In order to load from your own extension, you will need to have the files in a folder that has been mapped to a chrome:// or resource:// URL within your chrome.manifest. For example with the line:

content       myExtensionModule                             modules/

This does not need to be separate from the folder which you normally map for content, but it is common to keep JavaScript modules in a separate folder, just for organization. You do not need to have them use a .jsm extension, again just a convention. Using .js is just fine and may be desirable if you have other tools which auto-config based on a .js extension (e.g. an editor).

Significantly more information can be found in the documentation on MDN:

Note that in Firefox code it is common for there to be a mapping to shorter variables to access some commonly used Firefox properties. Specifically, the following are often used:

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Usually these are defined in a much more abbreviated manner. They are explicitly stated above to make the mapping more clear.

这篇关于Firefox扩展在main.js文件中包含脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆