简体   繁体   中英

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. What am I doing wrong?

I also put unpack true unpack tags in the install.rdf to see ny extension directory

Thanks in advanced.

initWithPath accepts only local filesystem paths. Assuming uri is a file url, you can do the conversion like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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