2008-09-19 17 views
3

简而言之:有什么方法可以找到xul应用程序的当前目录完整路径吗?xul:在xul浏览器中打开一个与“myapp.xul”相关的本地html文件

长解释:

我想在XUL浏览器应用程序中打开某些HTML文件。应该从xul应用程序以编程方式设置html文件的路径。这些html文件位于我的xul应用程序的文件夹之外,但处于同一级别。 (用户将从SVN签出这两个文件夹,没有可用于xul应用程序的安装)

它打开文件就好了,如果我设置完整路径,如“file:/// c:\ temp \ processing-sample \ index .html“

我想要做的就是打开相对于我的xul应用程序的文件。

我发现我可以打开用户的配置文件路径:

var DIR_SERVICE = new Components.Constructor("@mozilla.org/file/directory_service;1", "nsIProperties"); 
var path = (new DIR_SERVICE()).get("UChrm", Components.interfaces.nsIFile).path; 
var appletPath; 

// find directory separator type 
if (path.search(/\\/) != -1) 
{ 
appletPath = path + "\\myApp\\content\\applet.html" 
} 
else 
{ 
appletPath = path + "/myApp/content/applet.html" 
} 

// Cargar el applet en el iframe 
var appletFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 
appletFile.initWithPath(appletPath); 
var appletURL = Components.classes["@mozilla.org/network/protocol;1?name=file"].createInstance(Components.interfaces.nsIFileProtocolHandler).getURLSpecFromFile(appletFile); 
var appletFrame = document.getElementById("appletFrame"); 
appletFrame.setAttribute("src", appletURL); 

有没有办法找到一个XUL应用程序的当前目录的完整路径?

回答

2

我找到了一个解决方法:http://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO我不能用一个相对路径“../../index.html”完全打开一个文件,但我可以得到应用程序目录并使用它。

var DIR_SERVICE = new Components.Constructor("@mozilla.org/file/directory_service;1", "nsIProperties"); 
var path = (new DIR_SERVICE()).get(resource:app, Components.interfaces.nsIFile).path; 
var appletPath; 
+0

其实,你可以设置浏览器`src`与文件的路径属性,但你需要使用的“Chrome://” sintax,像@bizzy回答。 – 2010-11-19 13:32:35

0

在xul应用程序中,您可以使用chrome url访问应用程序的chrome文件夹。

我对元素的使用经验相对较少,所以我不确定这是否会为您准确地工作。事情是这样的,你有你的XUL文件中的JavaScript源文件:

<script src="chrome://includes/content/XSLTemplate.js" type="application/x-javascript"/> 

所以,也许如果文件驻留在C:\ applicationFolder \铬\内容\ index.html的,你可以访问它:

chrome://content/index.html 

以某种方式。

您也可以查看jslib,这是一个简化很多事情的库,包括文件I/O。 IIRC,但我很难使用'../'进入相对路径。

2

是的,您的扩展中的html文件可以使用chrome URI寻址。 从我的扩展一个例子:

content.document.location.href = "chrome://{appname}/content/logManager/index.html" 
相关问题