Firefox 扩展 .xpi 文件结构:描述、内容、创建和安装 [英] Firefox extension .xpi file structure: description, contents, creation, and installation

本文介绍了Firefox 扩展 .xpi 文件结构:描述、内容、创建和安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种简单的方法来开发 Firefox 扩展时投入了很多东西,但我无法创建一个扩展.请告诉我 Firefox 扩展的文件结构和安装扩展的简单方法.

I put a lot of stuff in searching an easy way to develop a Firefox extension, but I am unable to create an extension. Kindly tell me the file structure of Firefox extensions and an easy way to install the extension.

推荐答案

.xpi 文件格式(扩展打包)

用作 Mozilla(Firefox、Thunderbird 等)扩展的容器的 .xpi 文件只是将文件扩展名更改为 .xpi 使用deflate"将文件添加到存档中压缩或未压缩.如果您使用除deflate"或store"之外的任何其他类型的压缩,(未压缩),您将收到如下错误:

.xpi file format (Extension Packaging)

The .xpi files that are used as containers for Mozilla (Firefox, Thunderbird, etc.) extensions are merely zip archives that have had the file extension changed to .xpi with the files added to the archive using either "deflate" compression, or uncompressed. If you use any other type of compression, other than "deflate", or "store" (uncompressed), you will get an error like:

This add-on cannot be installed because it appears to be corrupted

文件从 zip 压缩存档的根目录开始(即,没有一个空的第一级目录然后包含文件).

The files start in the root directory of the zip compressed archive (i.e. there is not an empty first level directory which then contains the files).

存档的内容可以是几个文件到任意数量的文件.必须包含的文件取决于您要打包的附加组件的类型.如果您打算使用 Add-on SDK,那么您可能不需要知道这些文件的格式,因为其中大部分是使用 jpm 工具抽象出来的.如果您不知道我在说什么,您可能需要阅读 不同类型 Firefox 附加组件 (WebExtensions, 附加 SDKBootstrap/RestartlessOverlay/Legacy/XUL).

The contents of the archive could be only a few files to any number of files. The files that must be included depend on the type of add-on which you are packaging. If you are planning on using the Add-on SDK, then you probably don't need to know the format for these files, as much of it is abstracted by using the jpm tool. If you have no idea what I am talking about, you may want to read up on the different types of add-ons for Firefox (WebExtensions, Add-on SDK, Bootstrap/Restartless, and Overlay/Legacy/XUL).

至少,您将拥有一个描述扩展的 manifest.json 文件.几乎可以肯定,您将拥有其他文件.chrome.manifestinstall.rdfpackage.json 文件用于其他类型的附加组件未使用 在 WebExtension 附加组件中.您不应该拥有这些文件.

At a minimum, you will have a manifest.json file which describes the extension. You will, almost certainly, have additional files. The chrome.manifest, install.rdf, and package.json files used in other types of add-ons are not used in WebExtension add-ons. You should not have those files.

Firefox 附加 SDK 扩展的 .xpi 文件应通过执行 jpm xpi.附加 SDK 扩展在 package.json 文件中描述.当您运行 jpm xpi 时,您的附加组件被转换为 Bootstrap/Restartless 附加组件.这是通过将 package.json 文件转换为 install.rdf、创建一个 chrome.manifest 文件并向 JavaScript 添加一些包装器来完成的.您不应该尝试自己执行此过程,除非您的附加组件需要这样做(这种情况很少见).

The .xpi file for a Firefox Add-on SDK extension should be created by executing jpm xpi. Add-on SDK extensions are described in a package.json file. When you run jpm xpi your add-on is translated to being a Bootstrap/Restartless add-on. This is done by translating the package.json file into a install.rdf, creating a chrome.manifest file and adding some wrappers to the JavaScript. You should not try to perform this process yourself, unless doing so is necessary for your add-on to function (which would be quite rare).

至少,您有 install.rdfchrome.manifest 文件.Bootstrap/Restartless 附加组件也将有一个 bootstrap.js 文件.几乎总是会有额外的文件.这些类型的附加组件不使用 package.json,也不使用 manifest.json.

At a minimum, you have install.rdf, and chrome.manifest files. Bootstrap/Restartless add-ons will also have a bootstrap.js file. There will almost always be additional files. These types of add-ons do not use a package.json, nor a manifest.json.

我非常简单的 Bootstrap/Restartless 扩展,Print Button is Print(将打印按钮改为打印而不是打印预览),结构如下:

My very simple Bootstrap/Restartless extension, Print Button is Print (changes the print button to print instead of print preview), has the following structure:

Archive contains:
  bootstrap.js
  chrome/
  chrome/content/
  chrome/content/options.xul
  chrome/skin/
  chrome/skin/printer-typeC128.png
  chrome/skin/printer-typeC32.png
  chrome/skin/printer-typeC48.png
  chrome/skin/printer-typeC64.png
  chrome.manifest
  install.rdf
  license.txt
Total 12 entries (42360 bytes)

  • 有必需的 install.rdfchrome.manifest 文件.
  • 文件bootstrap.jsBootstrap/Restartless 扩展所必需的.它包含在安装、删除、启用、禁用扩展或 Firefox 启动或关闭时运行的代码.这个扩展非常简单,所有的 JavaScript 代码都包含在 bootstrap.js 中.
  • 有一个文件 chrome/content/options.xul,它是 选项对话框.
  • license.txt 只是说明该扩展是在 Mozilla Public 下发布的许可证,v2.0.
  • .png 文件是此扩展程序在各种分辨率下的图标.
    • There are the required install.rdf and chrome.manifest files.
    • The file bootstrap.js is required for Bootstrap/Restartless extensions. It contains the code that is run when the extension is installed, removed, enabled, disabled, or upon Firefox startup or shutdown. This extension is simple enough such that all the JavaScript code is contained in bootstrap.js.
    • There is a file chrome/content/options.xul which is a XUL definition of the options dialog.
    • The license.txt just explains that the extension was relased under the Mozilla Public License, v2.0.
    • The .png files are the icon for this extension at various resolutions.
    • 您可以使用任何您想要的方法来创建 .zip 文件,该文件被重命名为 .xpi.请记住,支持的唯一压缩方法是deflate",但文件也可以添加到未压缩的存档中.您的顶级文件(例如,您拥有的 manifest.json(WebExtensions)或其他所有文件:chrome.manifestinstall.rdf) 应该在存档的根目录中,而不是在子目录中.

      You can use whatever method you desire to create the .zip file, which is renamed to .xpi. Keep in mind the requirement that the only compression method that is supported is "deflate", but files can also be added to the archive uncompressed. Your top level files (e.g. which ever you have of manifest.json (WebExtensions), or everything else: chrome.manifest, and install.rdf) should be in the root directory of the archive, not in a subdirectory.

      为了创建 .xpi 文件,我使用了一个批处理文件,它结合了 DOS 和 Unix/Linux(实际上是 Cygwin) 命令:

      To create the .xpi file I use a batch file, which uses a combination of DOS and Unix/Linux (actually Cygwin) commands:

      mkxpi.bat:

      rm -f PrintButtonIsPrint@makyen.foo.xpi
      zip -1 -r PrintButtonIsPrint@makyen.foo.xpi * -x@xpi.ignore
      pause
      

      这会删除 .xpi 文件的所有旧版本.然后它使用 -1 最小压缩(访问速度比节省空间更重要)创建一个新的 .xpi 文件,这强制仅存储未压缩或使用放气".新的 .xpi 将包含所有文件和子目录 *,但忽略 xpi.ignore 文本文件 (-x@xpi.ignore).使用忽略文件是因为我在目录中还有其他内容(例如 .git 目录、从编辑器自动创建的 .bak 文件等).创建 .xpi 文件后,脚本将执行 pause 以便我可以验证包含哪些文件,没有错误等,而不仅仅是让窗口消失并假设一切都很好.

      This removes any old version of the .xpi file. It then creates a new .xpi file using, -1, minimal compression (speed of access is more important than saving space), which forces only storing uncompressed or using "deflate". The new .xpi will contain all files and subdirectories *, but ignoring all the files in the xpi.ignore text file (-x@xpi.ignore). Ignoring files is used because I have other things in the directory (e.g. .git directory, .bak files auto-created from editor, etc.). Once the .xpi file is created the script executes pause so I can verify which files were included, that there were no errors, etc., instead of just having the window disappear and assuming that everything is fine.

      我的 xpi.ignore 文件有点长,因为它积累了来自各种项目的数据并且很少被清除:

      My xpi.ignore file is a bit long, as it accumulates cruft from various projects and is rarely cleaned out:

      *.com
      *.class
      *.dll
      *.exe
      *.o
      *.so
      *.7z
      *.dmg
      *.gz
      *.iso
      *.jar
      *.rar
      *.tar
      *.zip
      *.log
      *.sql
      *.sqlite
      *.svg
      */.DS_Store
      */.DS_Store?
      */._*
      ._*
      */.Spotlight-V100
      .Spotlight-V100
      */.Trashes
      .Trashes
      */ehthumbs.db
      */Thumbs.db
      *.ORIG
      *.bak
      *OLD*
      OLD/*
      */OLD/*
      *.OLD
      *.OLD[0-9]
      */OLD/*
      */OLD[0-9]/*
      *.unknown
      *.unknown[0-9]
      *.updated
      *.updated[0-9]
      */Copy *
      */OLD
      */OLD*
      */OLD[0-9]
      */OLD[0-9][0-9]
      */test/*
      */not in xpi/*
      */tmp
      *.tmp
      */foo
      *.foo
      *checkpoint
      .git
      */.git
      .gitignore
      */.gitignore
      xpi.ignore
      mkclean.bat
      mkclean.bat.DONTRUN
      mkxpi.bat
      *.xpi
      */devtools-toolbox-window.ico
      */devtools-webconsole.ico
      */JSConsoleWindow.ico
      */main-window.ico
      */places.ico
      */viewSource.ico
      

      安装扩展

      作为普通扩展:
      为了将扩展作为普通附加组件安装到 Firefox 的品牌发行版或 Beta 版中,它必须是 由 Mozilla 签名.这是通过将其提交给 AMO 来完成的.您可以将未签名的扩展作为普通附加组件安装到其他版本的 Firefox(例如 Firefox Developer版本Firefox Nightly无品牌测试版或无品牌发布),方法是将 xpinstall.signatures.required 设置为 falseabout:config 中.

      Installing extensions

      As normal extensions:
      In order to install an extension as a normal add-on into a branded Release or Beta version of Firefox it must be signed by Mozilla. This is done by submitting it to AMO. You can install unsigned extensions as normal add-ons into other versions of Firefox (e.g. Firefox Developer Edition, Firefox Nightly, Unbranded Beta, or Unbranded Release) by setting xpinstall.signatures.required to false in about:config.

      如果您选择,在特定的 Firefox 安装中,您可以完全禁用附加签名要求.有关更多信息,您可以查看我的回答:如何禁用 Firefox 附加组件的签名检查?

      If you choose, in a particular installation of Firefox, you can completely disable the add-on signing requirement. For more information, you can see my answer: How can I disable signature checking for Firefox add-ons?

      安装扩展(即.xpi 文件)可以很简单,只需将其拖放到运行您希望安装它的配置文件的 Firefox 窗口中即可.对于开发/测试,您可以将扩展放在本地驱动器的目录中通过使用 Firefox 扩展代理文件(创建一个名为作为扩展的 <em:id>(在 install.rdf 中用于 Bootstrap/Restartless 和 Overlay/Legacy)在配置文件的 extensions 目录中包含一行包含包含扩展文件的目录的完整路径).根据您的目标(一个配置文件、所有配置文件、所有用户、哪个操作系统等),还有其他关于如何安装扩展程序.

      Installing an extension (i.e. the .xpi file) can be a simple matter of dragging and dropping it onto a Firefox window running the profile in which you desire it installed. For development/testing, you can have the extension be in a directory on your local drive by using a Firefox extension proxy file (create a file named as the extension's <em:id> (in install.rdf for Bootstrap/Restartless and Overlay/Legacy) in the profile's extensions directory containing one line with the complete path to the directory containing the extension's files). Depending on what your goal is (one profile, all profiles, all users, which OS, etc.), there are other options as to how to install extensions.

      作为 临时附加组件:
      唯一不能作为临时插件安装的扩展类型是覆盖/传统.此类扩展需要在安装后重新启动浏览器才能运行.因此,它们不能是暂时的.

      As temporary add-ons:
      The only type of extension which can not be installed as a temporary add-on is Overlay/Legacy. Such extensions require the browser to be restarted after the install prior to being functional. As such, they can not be temporary.

      要将扩展安装为临时扩展,请导航至 about:debugging.在该页面上,单击加载临时加载项",然后将弹出窗口导航到相应的文件夹并选择 .xpi 文件或目录中的任何文件.如果您选择的文件不是 .xpi 文件,则假定该目录包含将自动识别的解压附加文件.

      To install an extension as a temporary, navigate to about:debugging. From that page, click on Load Temporary Add-on, then navigate popup to the appropriate folder and select either an .xpi file, or any file in the directory. If you select a file other than an .xpi file, it is assumed that the directory contains unpacked add-on files which will be automatically identified.

      这篇关于Firefox 扩展 .xpi 文件结构:描述、内容、创建和安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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