2011-03-22 71 views
1

我有以下JS函數,它是mozilla thunderbird擴展的第一個原型。 目標是連接到服務器並下載示例文件,然後解壓縮並將內容存儲在thunderbird配置文件文件夾中。 現在,這一切都正常工作,除了在文件系統上創建zip文件後,函數的執行停止。所以我必須重新啓動這個函數,以便獲得從zip文件中提取user.js文件的第二部分函數。 任何想法可能是什麼問題?爲什麼JavaScript函數會停止執行?

function downloadFile(httpLoc) { 
    // get profile directory 
    var file = Components.classes["@mozilla.org/file/directory_service;1"]. 
     getService(Components.interfaces.nsIProperties). 
     get("ProfD", Components.interfaces.nsIFile); 

    var profilePath = file.path; 

    // change profile directory to native style 
    profilePath = profilePath.replace(/\\/gi , "\\\\"); 
    profilePath = profilePath.toLowerCase(); 

    // download the zip file 
    try { 
     //new obj_URI object 
     var obj_URI = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI(httpLoc, null, null); 

     //new file object 
     var obj_TargetFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 

     //set to download the zip file into the profil direct 
     obj_TargetFile.initWithPath(profilePath + "\/" + "test.zip"); 

     //if file the zip file doesn't exist, create it 
     if(!obj_TargetFile.exists()) { 

      alert("zip file wird erstellt"); 

      obj_TargetFile.create(0x00,0644); 

     } 

     //new persitence object 
     var obj_Persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"].createInstance(Components.interfaces.nsIWebBrowserPersist); 


     // with persist flags if desired ?? 
     const nsIWBP = Components.interfaces.nsIWebBrowserPersist; 
     const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES; 
     obj_Persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE; 

     //save file to target 
     obj_Persist.saveURI(obj_URI,null,null,null,null,obj_TargetFile); 
    } catch (e) { 
     alert(e); 
    } finally { 
     // unzip the user.js file to the profile direc 

     // creat a zipReader, open the zip file 
     var zipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"] 
      .createInstance(Components.interfaces.nsIZipReader); 

     zipReader.open(obj_TargetFile); 

     //new file object, thats where the user.js will be extracted 
     var obj_UnzipTarget = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 

     //set path for the user.js 
     obj_UnzipTarget.initWithPath(profilePath + "\/" + "user.js"); 

     // if user.js doesn't exist, create it 
     if(!obj_UnzipTarget.exists()) { 
      alert("user.js wird erstellt"); 

      obj_UnzipTarget.create(0x00,0644); 
     } 

     // extract the user.js out of the zip file, to the specified path 
     zipReader.extract("user.js", obj_UnzipTarget); 

     zipReader.close(); 
    } 
} 

var hello = { 
    click: function() { 
     downloadFile("http://pse2.iam.unibe.ch/profiles/profile.zip"); 
    }, 
}; 
+0

對於它的價值,你不是唯一的這個問題。這個人通過設置一些隨機變量來「解決」它看起來像? http://forums.mozillazine.org/viewtopic.php?f=27&t=236477 – 2011-03-22 01:14:32

+0

爲了吸引更多的幫助,您應該將結果包含在javascript錯誤控制檯中(在菜單Tools-> Error Console中)。你也應該編輯整理這個問題,看起來很整潔,人們可以閱讀它。在兩個可滾動代碼窗口和額外純文本之間拆分時,腳本很難讀取... – Paul 2011-03-22 01:22:31

+0

以下是錯誤消息...錯誤:未捕獲的異常:[異常...]組件返回的失敗代碼:0x80004005(NS_ERROR_FAILURE) [nsIZipReader.open]「nsresult:」0x80004005(NS_ERROR_FAILURE)「位置:」JS frame :: chrome://pse1/content/test.js; version = 1.7 :: downloadFile :: line 44「data:no] – Patrick 2011-03-22 08:26:39

回答