这是教程最后一部分, 我们将把所有的web所有的组件成分打包成可安装到其他应用中的形式 - the library itself, the type library, the header file, and the user interface resources. The first section, Component Installation Overview, describes the general installation process in Mozilla. The following sections describe the steps you can take to organize the WebLock component for distribution and installation.
请注意: 这个教程主要是关注组件开发本身, 所以这部分描述有关打包和安装到Gecko的过程是很简单的. 如果你希望了解详细的打包和安装组件到基于Gecko应用的信息,应该参考https://www.mozilla.org/projects/xpinstall.
Component Installation Overview
XPInstall是一组JavaScript APIs用来建立安装脚本. 使用XPInstall,你可以为装载到Gecko-based应用,Mozilla extensions,或者individual components的组件建立web-based安装脚本. WebLock component安装脚本也可以用来注册组件到browser(see Registration Methods in XPCOM for more information on registration).
下面的例子安装脚本使用了Mozilla XPInstall技术来操作安装并且以高层次Javascript对象的方式来跟Mozilla's chrome registry 交互。
Like the Windows registry, the chrome registry is a database of information about applications, skins, and other extensions that have been installed in a Gecko application. Since Mozilla and other Gecko-based applications are cross-platform, this database is abstracted above the operating system or any particular platform's registry.
The chrome registry lives in a series of RDF/XML files in the application directory of Mozilla and other Gecko-based browsers, where new installs, user configurable data, skins, and other information are related to one another and the application itself.
XPInstall中的JavaScript APIs Install
对象下载包含了安装文件的JAR并且调用注册方法来告诉 Mozilla 新的组件和用来调用WebLock组件的UI. WebLock Installation Script 是完整的
trigger installation script
, 可以从网页触发. 文件被存储在JAR file weblock.jar
, 这是一个简单的ZIP文件,以XPI结尾,有时候也可能包含一个内部的安装脚本install.js
.
一旦你把组件和Weblock相关资源正确打包(see the following section, Archiving Resources), WebLock安装脚本是一个简单的事情(see The WebLock Installation Script).
Archiving Resources
Once you have compiled all the resources that make up the WebLock component and the files that make up the user interface that will be added to the browser, you can place these within a subdirectory called weblock
.
Place the entire subdirectory into a ZIP archive and name the archive weblock.xpi
. The archive, its subdirectory structure, and its contents should look like this:
weblock.xpi
Archive Viewed in WinZIP
Note that the top level of the archive holds the install.js
installation file, an RDF manifest for the package as a whole, and the component files (weblock.xpt
and weblock4.dll
). The component files are copied to the components directory of the Gecko application, and the weblock subdirectory gets copied over into the chrome subdirectory, where its UI resources can be added dynamically to the XUL-based Gecko application.
The next section shows how this process of downloading, copying and registering the necessary files from the XPI can be achieved with an XPInstall installation script.
The WebLock Installation Script
安装脚本是一个存储在XPI中的JavaScript文件. 他必须在包的根目录 (i.e., weblock.xpi
) itself. 一旦触发 (see The WebLock Trigger Script), 安章脚本将:
- downloads the WebLock component and places it in the
components
directory - copies the
weblock
subdirectory in the Mozilla chrome application subdirectory - registers both the component and the UI
The XPInstall API提供了一些核心方法[essential-methods]例如 initInstall
, registerChrome
, addFile
, and others.
// initialize the installation var err = initInstall("WebLock", "weblock", 1.0); var componentsDir = getFolder("Components"); var cf = getFolder("Chrome"); // add the DLL and say where it'll go addFile("weblock.dll", 1.0, "weblock.dll", componentsDir, ""); // add the typelib also addFile("weblock.xpt", "1.0", "weblock.xpt", componentsDir, ""); // add the weblock subdirectory of the XPI and specify that // it be installed in the chrome application directory err = addDirectory("weblock", "1.0", "", chromeDir, ""); // ? have to register component here or with regxpcom? // register the new UI with the mozilla chrome registry registerChrome(CONTENT, getFolder(cf,"weblock.xpi"),"weblock"); registerChrome(SKIN, getFolder(cf, "weblock.xpi"),"weblock"); // perform the installation if there are no errors if (err==SUCCESS) performInstall(); else cancelInstall(err);
The WebLock Trigger Script
The trigger script
is the script placed on a web page that actually initiates an XPInstall installation and calls the installation script that appears in the XPI. The following HTML specifies a complete webpage in which the trigger script is defined as a JavaScript function, installWebLock
, that gets called when the user clicks the hyperlink.
<html> <title>WebLock Installation</title> <script type="text/javascript"> /* * Trigger function that downloads the XPI so the * install.js file inside can be read and executed */ function installWebLock() { weblock_xpi = {'WebLock Extension': 'weblock.xpi'}; InstallTrigger.install(weblock_xpi); } </script> <h1>Install WebLock</h1> <p><a href="#" onclick="installWebLock();">install weblock</a></p> </html>
Distributing Your Component
Once you have the component packaged properly and the necessary installation and trigger scripts, you are ready to distribute your component so others can install it in their Gecko applications.
In Mozilla and Netscape browsers, XPInstall makes this process especially easy by providing the file format (XPI) and the necessary installation scripts for doing a web-based installation. As WebLock Installation Script demonstrates, XPInstall uses special keywords to refer to common installation directories such as components
in a generalized, cross-platform way.
If you are installing WebLock in an Gecko-based application for which XPInstall is not available, then you will have to devise a separate installation scheme. We leave this as an exercise for the reader.
- Note: install-object-methodsThe methods are available on the main
Install
object, which is implied in the script below in the same way that thewindow
object is implied in JavaScript manipulation of the DOM of a web page. In other words, the fragmentinitInstall()
from the script is equivalent toInstall.initInstall()
.
Copyright (c) 2003 by Doug Turner and Ian Oeschger. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.02 or later. Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder. Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.