ero*_*man 2 javascript firefox-addon firefox-addon-restartless
如何将本地(文件系统)URI转换为路径?
它可以用nsIIOService+ newURI()+ QueryInterface(Components.interfaces.nsIFileURL)+ 完成,file.path但这看起来很长.
有更短的方式吗?
这是一个示例代码:
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var url = ios.newURI(aFileURL, null, null); // url is a nsIURI
// file is a nsIFile
var file = url.QueryInterface(Components.interfaces.nsIFileURL).file;
console.log(file.path); // "C:\path-to-local-file\root.png"
Run Code Online (Sandbox Code Playgroud)
支持的方式实际上就是你已经在做的事情.如果你发现它太冗长,请给自己写一个辅助函数.当然,您可以使用各种帮助程序缩短它.
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/Services.jsm");
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var path = Services.io.newURI(aFileURL, null, null).
QueryInterface(Ci.nsIFileURL).file.path;
Run Code Online (Sandbox Code Playgroud)
要么:
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/NetUtil.jsm");
var aFileURL = 'file:///C:/path-to-local-file/root.png';
var path = NetUtil.newURI(aFileURL).QueryInterface(Ci.nsIFileURL).file.path;
Run Code Online (Sandbox Code Playgroud)