我试图在我的扩展目录中创建一个文件,我有这个代码:
AddonManager.getAddonByID(" extension id here ", function(addon)
{
var uri = addon.getResourceURI("hello.txt");
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);
try{
file.initWithPath(stringUri);
} catch(e) {
alert(e);
}
alert(addon.hasResource("hello.txt"));
});
但由于某种原因,最后一个警告显示始终为false且文件不存在。我做错了什么?
我还在install.rdf中放入了unpack true unpack标签,以查看ny扩展目录
先谢谢。
答案 0 :(得分:0)
initWithPath
仅接受本地文件系统路径。假设uri
是文件网址,您可以像这样进行转换
var path = uri.QueryInterface(Components.interfaces.nsIFileURL).file.path