2008-09-19 33 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" 
相關問題