简体   繁体   English

使用扩展名Firefox创建文件

[英]Creating File using extension firefox

Iam trying to create a file in my extension directory and i have this code: 我试图在我的扩展目录中创建一个文件,我有以下代码:

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"));
});

but for some reason the last alert shows always false and file doesn't exist. 但由于某种原因,最后一个警报始终显示为false且文件不存在。 What am I doing wrong? 我究竟做错了什么?

I also put unpack true unpack tags in the install.rdf to see ny extension directory 我还将unpack真正的unpack标记放入install.rdf中,以查看ny扩展目录

Thanks in advanced. 提前致谢。

initWithPath accepts only local filesystem paths. initWithPath仅接受本地文件系统路径。 Assuming uri is a file url, you can do the conversion like this 假设uri是一个文件网址,您可以像这样进行转换

var path = uri.QueryInterface(Components.interfaces.nsIFileURL).file.path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM