在Firefox扩展中使用相对路径

时间:2012-04-09 10:08:04

标签: firefox firefox-addon

我使用捆绑的可执行文件开发Firefox扩展,该文件应该在浏览器启动时运行。

要运行进程,我需要获取指向可执行文件的nsIFile或nsILocalFile实例。 我知道如何使用目录服务获得它的一个解决方案:

var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
file.append("extensions");
file.append("<extension id>");
file.append("<relative path>");

但是这个解决方案有两个缺点:

  1. 它不能在开发模式下工作,而不是安装扩展程序我只有具有真实扩展路径的文本文件

  2. 我不确定它是否适用于所有Firefox配置,因为硬编码的扩展程序&#34;路径的一部分

  3. 那么有没有更好的方法来运行Firefox扩展附带的可执行文件?

    感谢。

2 个答案:

答案 0 :(得分:3)

你对Firefox配置文件的目录结构做了太多假设 - 不要。 Add-on Manager API允许您获取扩展内文件的路径,您应该使用它:

Components.utils.import("resource://gre/modules/AddonManager.jsm");

AddonManager.getAddonByID("<extension id>", function(addon)
{
  var uri = addon.getResourceURI("<relative path>");
  var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
  ...
});

答案 1 :(得分:2)

无重启插件的startup函数(在bootstrap.js文件中)将作为其第一个参数接收安装插件的路径。然后,您可以播放各种技巧来读取.jar文件中的文件(如果有):请参阅https://github.com/protz/GMail-Conversation-View/blob/master/bootstrap.js#L55作为示例。

在一个没有重新启动的情况下,我必须承认我没有太多想法:)。