【问题标题】:Abstracting the chrome packaged app filesystem for use with node-webkit抽象 chrome 打包的应用程序文件系统以与 node-webkit 一起使用
【发布时间】:2015-05-04 22:56:34
【问题描述】:

我目前有一个 chrome 打包应用程序,我们也将它移植到 iPad,但我想使用 node-webkit (nw.js) 使其可安装,我需要抽象 chrome 打包应用程序 API 以与 chrome 一起使用。文件系统。我目前用来保存的代码如下。

 var downloadFile = function (readUrl, next) {
     var xhr = new XMLHttpRequest();
    xhr.open('GET', readUrl);
     xhr.responseType = 'arraybuffer';

     xhr.onload = function (e) {
         if (this.status == 200) {
             var response = this.response;
            var params = {
                 type : 'saveFile',
                suggestedName : fileNameNoExtension,
                         //my code will inject the extension but in this case i just put in txt
                         accepts : [{
                         extensions : ['.txt']
                     }
                 ]
             }
            chrome.fileSystem.chooseEntry(params, function (writableFileEntry) {
                 debugger;
                writableFileEntry.createWriter(function (writer) {
                     debugger;
                    writer.onwriteend = function (e) {
                         return next(null)
                     };
                    writer.onerror = function (e) {};
                     writer.write(new Blob([response], {
                             type : 'application/octet-stream'
                         }));
                 });
             });

         } else {
             //alert
         }
     };
    xhr.onprogress = function (evt) {
         if (evt.lengthComputable) {
             console.log('progress: ' + Math.round(evt.loaded * 100 / evt.total));
         }
     }
    xhr.addEventListener("error", function () {
         return next('error')
     }, false);
    xhr.addEventListener("abort", function () {
         return next('abort')
     }, false);
    xhr.send();
 }

【问题讨论】:

    标签: javascript html google-chrome-extension node-webkit nw.js


    【解决方案1】:

    我创建了一个名为 interop.js 的文件,我将此脚本加载到我的 index.html 中,如果它是一个 nw.js 项目,它将处理对 fileStorage 的所有 chrome 打包应用程序 API 调用。如果它是一个 chrome 打包的应用程序,那么 chrome 将处理它自己的 API。

     //if process is undefined then it is not an nw.js project and we can ignore the rest of the code;
     if (typeof process == 'undefined') {}
     //this is a nw.js project, spoof the chrome packaged app API
     else {
         var fs = require('fs')
             chrome = new function () {
            var fileSystem = {
                 //callback return readableFileEntry, which has a
                chooseEntry : function (params, callback) {
                     if (params.type == 'openFile') {
                         //open file choose
                        chooseFile(params, function (files) {
                             //this is technically an html5 "filelist"  we need to turn it into an array if there is more
                            //than one, and just return the single file if there isn't
                             if (!files) {
                                 return callback(null)
                             }
                            async.map(files, function (file, cb) {
                                 //normally chrome provides a 'readablefileentry' that will only give you the file
                                //asynchronously using the file() function
                                 file.file = function (next) {
                                     return next(this);
                                 }
                                cb(null, file)
                             }, function (err, files) {
                                 if (files.length > 1) {
                                     return callback(files);
                                 } else {
                                     return callback(files[0]);
                                 }
                             })
                         })
                     } else if (params.type == 'saveFile') {
                         chooseFile(params, function (files) {
                             var file = files[0];
                            debugger;
                             file.createWriter = function (next) {
                                 var writer = {
                                     write : function (blob) {
                                         debugger;
                                        var reader = new FileReader()
                                             reader.readAsArrayBuffer(blob)
                                            reader.addEventListener('loadend', function (e) {
                                                 var binary = new Uint8Array(reader.result)
                                                     debugger;
                                                 fs.writeFile(file.path, new Buffer(binary), function (err) {
                                                     //if the on error and writeend has been defined then callback, otherwise throw the error and log success
                                                    if (err && writer.onerror) {
                                                         writer.onerror(err)
                                                     } else if (err) {
                                                         throw err
                                                     } else if (writer.onwriteend) {
                                                         writer.onwriteend()
                                                     } else {
                                                         console.log('file was written but no callback was defined')
                                                     }
                                                 })
                                             });
                                     }
                                 }
                                return next(writer)
                             }
                            return callback(file)
                         })
                     }
                    function chooseFile(params, next) {
                         var fileHtml = '<input type="file"'
                             debugger;
                         if (params.acceptsMultiple)
                             fileHtml += ' multiple';
                         if (params.accepts && params.accepts.length > 0 && params.accepts[0].extensions) {
                             fileHtml += ' accept="'
                            for (var i = 0; i < params.accepts[0].extensions.length; i++) {
                                 if (i != 0)
                                     fileHtml += ','
                                    fileHtml += '.' + params.accepts[0].extensions[i]
                             }
                            fileHtml += '"'
                         }
                        if (params.suggestedName) {
                             fileHtml += ' nwsaveas="' + params.suggestedName + '"'
                         }
    
                         fileHtml += '>'
    
                         var chooser = $(fileHtml);
                        chooser.change(function (evt) {
                             debugger;
                            return next(chooser[0].files)
                         });
    
                         chooser.click();
                     }
                 }
             }
    
             return {
                 fileSystem : fileSystem,
             }
         };
     }
    

    【讨论】:

      猜你喜欢
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 2013-04-24
      • 1970-01-01
      相关资源
      最近更新 更多