您的位置:首页 > 其它

如何编写Firefox扩展

2008-09-22 12:31 423 查看
如何编写Firefox扩展(1)-技术准备

使用Firefox很长时间了,很喜欢这只"小狐狸",特别是它提供的丰富多彩的扩展功能。一直都想学习一下关于编写Firefox的扩展的知识,现在终于可以开始了,我将边学边把自己所掌握的知识书写出来,以方便大家学习.

收集了一些编写Firefox扩展的相关资料,如果英文够好,请直接查看这些资料,完全忽略我的文章

Extension development

Getting started with extension development

How to write Firefox extensions using BugMeNot as an example

How to create Firefox extensions

Writing an Extension for Firefox

Firefox Extension Tutorial

Building an Extension

下面是编写Firefox扩展所需掌握的相关技术

开发Firefox扩展的过程中,将会接触到以下技术,您不需要精通这些东西,但最好能对他们都比较熟悉.

XUL (XML User-Interface Language).基于xml的UI界面定义技术

JavaScript. 这个不用多讲了,大多数扩展使用它来完成.

DOM (Document Object Model). HTML文档结构模型.

XPCOM/XPConnect. 用来连接Mozilla 提供的XPCOM (Cross-Platform Component Object Model) 功能组件包, 可以用来访问 preferences 配置数据库, filesystem文件系统 以及其他Mozilla提供的功能(可以使用 JavaScript, C++, 甚至是 Python PyXPCOM来编写这个扩展).

CSS (Cascading Style Sheets).

XBL (XML Binding Language). 用来扩展XUL, 允许创建新的风格的UI界面.

RDF (Resource Description Framework). 在扩展中,用来描述某些数据的存储格式。.

如何编写Firefox扩展(2)-配置开发环境

想要快速方便的开发Firefox扩展,配置一下开发环境,做些必要的准备是必须的。

设置Firefox配置

为了避免开发中的扩展平时使用的Firefox的性能,我们需要重新创建一个配置,并且将其命名为"dev",然后用以下的命令行来启动这个配置为开发环境的Firefox

start "" "%ProgramFiles%\Mozilla Firefox\firefox.exe" -no-remote -P dev

想要运行默认的配置,使用 "firefox" 或 "firefox -P default".

您还可以同时运行稳定版和开发中的Firefox版本,来检查扩展的兼容性y (Installing Firefox 3 or Minefield while keeping Firefox 2).

开发环境配置

下面的这些配置可以使开发调试扩展更加方便。查看 Editing Configuration Files 了解更多信息。 下面的这些选项,默认是不会在about:config中列出来的,所以需要手工添加它们。方法是,在你的firefox的配置目录下找到user.js ,如果文件不存在,就手工创建一个,然后添加以下几行

user_pref("nglayout.debug.disable_xul_cache",true);

user_pref("browser.dom.window.dump.enabled",true);

提示:firefox3.0中user.js已经被prefs.js代替了

下面是一些可用的配置选项、

javascript.options.showInConsole = true. 记录错误日志到 Error Console.

nglayout.debug.disable_xul_cache = true. 禁止XUL缓存,这样当修改界面元素时,就不需要重新启动firefox了。此选项只有当你使用了目录而不是jar的格式安装扩展时有效,并且修改XUL的行为时仍然需要重新启动。

browser.dom.window.dump.enabled = true. 允许使用dump() 命令输出信息到标准控制台 See window.dump for more info. You can also use nsIConsoleService from privileged script.

javascript.options.strict = true.  Enables strict JavaScript warnings in the Error Console. Note that since many people have this setting turned off when developing, you will see lots of warnings for problems with their code in addition to warnings for your own extension. You can filter those with Console2.

extensions.logging.enabled = true.  This will send more detailed information about installation and update problems to the Error Console.

用来辅助进行Firefox扩展开发的一些Firefox扩展

名字有点拗口,但绝对都是好东西

DOM Inspector, an option of a custom installation.

Venkman, a JavaScript 的调试工具.

Extension Developer's Extension 扩展开发工具,可以用来生成xpi文件,设置开发选项,等等,强烈推荐。

Console2 

Chrome List 

Firebug javascript,http调试工具,功能强大,强烈推荐

Execute JS 

XPCOMViewer, an XPCOM inspector

自定义代码位置

为了使每次修改代码后,不需要重复的进行扩展的安装,你可以将你的源码放到Firefox的配置目录下,Firefox会自动检测并加载你的扩展

在 install.rdf 中找到扩展编号,如 em:id="{46D1B3C0-DB7A-4b1a-863A-6EE6F77ECB58}"

在your_profile_directory/extensions/ 目录下创建一个目录,目录名为刚刚找到的编号(eg. `your_profile_directory/extensions/{46D1B3C0-DB7A-4b1a-863A-6EE6F77ECB58}`) (Find your profile directory)

将你的开发文件,复制到这个目录下

重新启动Firefox.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: