简体   繁体   English

为什么此JavaScript函数停止执行?

[英]why does this JavaScript function stop execution?

I have the following JS function which serves as a first prototype for a mozilla thunderbird extension. 我有以下JS函数,可作为mozilla雷鸟扩展的第一个原型。 The goal is to connect to a server and download a sample file, then unzipping it and storing the contents in the thunderbird profile folder. 目标是连接到服务器并下载示例文件,然后将其解压缩并将内容存储在雷鸟配置文件文件夹中。 Now this all works fine, except that the execution of the function stops after creating the zip file on the file system. 现在,一切正常,除了在文件系统上创建zip文件后停止执行功能。 So i have to restart the function again, in order to get the second part of the function executed which extracts the user.js file from the zip file. 因此,我必须再次重新启动该功能,以便执行该功能的第二部分,该第二部分将从zip文件中提取user.js文件。 Any ideas what the problem could be? 任何想法可能是什么问题?

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");
    },
};

saveURI is asynchronous, so you need to set the progress listener on the persist object to know when it has finished. saveURI是异步的,因此您需要在persist对象上设置进度侦听器 ,以了解其何时完成。 Here's an example of setting a progress listener and later on there's a check to see whether the transfer has finished . 这是设置进度侦听器的示例,稍后检查传输是否已完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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