如何遍历Mozilla Firefox 4.0及更高版本中的目录?

时间:2012-01-21 04:55:13

标签: firefox firefox-addon directory traversal

习惯这样做:

// Firefox 3.6 and before; Mozilla 1.9.2 and before  
var ext = this.Cc["@mozilla.org/extensions/manager;1"]
              .getService(this.Ci.nsIExtensionManager)
              .getInstallLocation(id)
              .getItemLocation(id); 

// list all XML files in the installation folder:
var entries = ext.directoryEntries;
var files = [];

while(entries.hasMoreElements())
{ )

如何立即获取ext变量?我已经达到了以下目的:

Components.utils.import("resource://gre/modules/AddonManager.jsm");  
AddonManager.getAddonByID(id, function(addon) {
  ext = addon.getResourceURI("");  }

但不确定如何实际获取目录信息来遍历它......

1 个答案:

答案 0 :(得分:0)

可能没有要遍历的目录 - 从Firefox 4开始,安装时不再解压缩扩展,因此资源URI将指向XPI文件(通过jar:协议方案)。然后,在某些情况下,扩展将在安装时解压缩,然后您将获得file:/// URI。所以你应该这样做(未经测试):

var uri = addon.getResourceURI("");
if (uri instanceof Components.interfaces.nsIJARURI)
{
  var xpiFile = uri.JARFile.QueryInterface(Components.interfaces.nsIFileURL).file;
  var reader = Components.classes["@mozilla.org/libjar/zip-reader;1"]
                         .createInstance(Components.interfaces.nsIZipReader);
  reader.init(xpiFile);
  var enumerator = reader.findEntries(null);
  while (enumerator.hasMoreElements())
  {
    var entry = enumerator.getNext().QueryInterface(Components.interfaces.nsIZipEntry);
    alert(entry.name);
  }
  reader.close();
}
else if (uri instanceof Components.interfaces.nsIFileURL)
{
  var dir = uri.file;
  var enumerator = dir.directoryEntries;
  while (enumerator.hasMoreElements())
  {
    var entry = enumerator.getNext().QueryInterface(Components.interfaces.nsIFile);
    alert(entry.path);
  }
}
else
  throw new Error("Unexpected install location");