2012-04-05 39 views
5

我需要从JavaScript获取扩展安装目录的路径。如何确定扩展的目录

我的目标是从Firefox扩展中写入扩展目录中的JSON文件。为了做到这一点,我需要确定在Firefox配置文件中安装扩展的目录。

我用这个代码:

function writeToFile() 
{ 
    var id = "[email protected]";// The extension's id from install.rdf(i.e. <em:id>) 
    var ext = Components.classes["@mozilla.org/extensions/manager;1"] 
         .getService(Components.interfaces.nsIExtensionManager) 
         .getInstallLocation(id) 
         .getItemLocation(id); 
    var file = Components.classes["@mozilla.org/file/local;1"] 
         .createInstance(Components.interfaces.nsILocalFile); 
    file.initWithPath(ext.path); 
    file.append("config.json");  
    var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"] 
          .createInstance(Components.interfaces.nsIFileOutputStream); 
    foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
    var data = '[ {"id" : "2"} ]'; 
    foStream.write(data, data.length); 
    foStream.close(); 

它引发以下错误:

TypeError:Components.classes['@mozilla.org/extensions/manager;1'] is undefined. 

我基本上需要从JavaScript自动获得扩展的路径。 我再次检查了我的扩展的ID,我也尝试从其他扩展名的文件写入没有运气。


非常感谢您的回复。它不让我立即解决我的问题,但它迫使我阅读Mozilla文档。我终于得到了它如何工作的窍门。再次感谢。

用于解决上面的问题:

Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
    Components.utils.import("resource://gre/modules/FileUtils.jsm"); 
    AddonManager.getAddonByID("plugin_id", function(addon) { 

    var uri = addon.getResourceURI("config.json"); 
    var file = Components.classes["@mozilla.org/file/local;1"] 
         .createInstance(Components.interfaces.nsILocalFile); 
    var stringUri = uri.asciiSpec; 
    stringUri = stringUri.replace(new RegExp(/\//g), '\\'); 
    stringUri = stringUri.slice(8); 
    alert(stringUri); 
    file.initWithPath(stringUri); 
    alert(addon.hasResource("config.json")); 

    var stream = FileUtils.openFileOutputStream(file, 
               FileUtils.MODE_WRONLY 
               | FileUtils.MODE_CREATE 
               | FileUtils.MODE_TRUNCATE); 
    stream.write(dataToWrite, dataToWrite.length); 
    stream.close(); 
+1

我想你需要确保你的扩展,以写入文件的安装目录解压。 – erikvold 2012-04-05 18:20:03

+0

,因此您可能希望将该文件保存在用户的配置文件目录中。 – erikvold 2012-04-05 18:20:22

回答

2

与Firefox 4开始,你应该使用Add-on Manager API

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

AddonManager.getAddonByID(id, function(addon) 
{ 
    var uri = addon.getResourceURI("config.json"); 
    if (uri instanceof Components.interfaces.nsIFileURL) 
    writeToFile(uri.file); 
}); 

注意的是,新的API是异步的,获取有关扩展的数据可能需要一段时间。另外,你需要在你的install.rdf中指定<em:unpack>true</em:unpack> flag,否则你的扩展在安装时不会被解压缩(出于性能方面的考虑),并且在磁盘上将没有对应于config.json的文件(它将是压缩XPI文件中的一个位置) 。写入扩展目录还有另一个问题:扩展名更新时,所有文件将被替换。一个更好的想法可能是写入配置文件目录中的文件,那么您将不需要牺牲性能。

写入可以使用FileUtils.jsm简化文件:

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

var stream = FileOutputStream.openFileOutputStream(file, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE); 
stream.write(data, data.length); 
stream.close(); 
0

你可以找到与用户的配置文件目录:

Components.utils.import("resource://gre/modules/Services.jsm"); 
Services.dirsvc.get("ProfD", Ci.nsILocalFile) 

这可能是一个更好的主意,这里保存文件,但我不不了解上下文。