xul - Firefox 插件单击按钮下载文件并将其发布到另一台服务器

标签 xul nsfilemanager

我可以访问一个网站,该网站有一个简单的按钮(我不拥有该网站,也无权访问源代码)来下载文档。我正在使用下面的代码来执行它,它似乎工作正常但有问题

目标

  • 我要下载文件
  • 将文件数据发布到另一个站点

  • 问题
  • 有时下载的文档很大或者单击按钮后根本没有文档
  • 在相同操作系统的不同机器上的相同版本的 FF 上不起作用

  • 下面的代码
      Components.utils.import("resource://gre/modules/Downloads.jsm");
      Components.utils.import("resource://gre/modules/Task.jsm");
    
      window.content.location.href = "javascript:void download_document()";
    
      Task.spawn(function () {
        let list = yield Downloads.getList(Downloads.ALL);
        let downloads = yield list.getAll();
        setTimeout(function(d_before){
            Task.spawn(function(d_before) {
              let list = yield Downloads.getList(Downloads.ALL);
              let downloads = yield list.getAll();
              var file =  downloads[downloads.length-1];
              var parts = file.target.path.split('/');
              var document_name = parts[parts.length-1];
    
              // alert(document_name);
              var file = FileUtils.getFile("DfltDwnld", [document_name]);
              Components.utils.import("resource://gre/modules/NetUtil.jsm");
              NetUtil.asyncFetch(file, function(inputStream, status) {
    
                // alert("Fetching file");
                if (!Components.isSuccessCode(status)) {
                  return;
                }
                var data =  NetUtil.readInputStreamToString(inputStream, inputStream.available());
    
                // alert("Reading file data");
                data = window.btoa(data);
    
                // alert("File data read");
                // alert(prefs.getCharPref("server_ip"));
                xmlhttp.open("POST",ht_server+"/import_document",true);
                xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlhttp.send("authentication_token="+prefs.getCharPref("api_key")
                    +"&email="+prefs.getCharPref("email")
                    +"&body="+encodeURIComponent(content.document.body.innerHTML)
                    +"&document_name="+document_name
                    +"&document_data="+encodeURIComponent(data));
                // alert("Finished");
              });
            }).then(null, Components.utils.reportError);
        },3000);
      }).then(null, Components.utils.reportError);
    

    上面的代码对于我的解决方案并不完整,但我主要担心它在某些机器上可以工作,而在其他机器上我收到此错误(下载文档时)
    NS_ERROR_NOT_AVAILABLE: Async version must be used nsHelperAppDlg.js:209:0
    [Exception... "Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsIFile.append]"  nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)"  location: "JS frame :: resource://gre/modules/FileUtils.jsm :: FileUtils_getFile :: line 43"  data: no] Promise-backend.js:873:0
    NS_ERROR_NOT_AVAILABLE: Async version must be used nsHelperAppDlg.js:209:0
    [Exception... "Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsIFile.append]"  nsresult: "0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH)"  location: "JS frame :: resource://gre/modules/FileUtils.jsm :: FileUtils_getFile :: line 43"  data: no] Promise-backend.js:873:0
    A promise chain failed to handle a rejection. Did you forget to '.catch', or did you forget to 'return'?
    See https://developer.mozilla.org/Mozilla/JavaScript_code_modules/Promise.jsm/Promise
    

    不是这方面的专家,我无法解决它

    任何人都可以提供一些建议吗?

    最佳答案

    我建议检查 selenium,这是一个基于 Java 的 Web 应用程序来帮助测试 Web 应用程序。它允许很多自动化,并且可以以混合方式使用,您可以只保留在临时文件夹中查找文件的代码部分,然后将文件发布/上传到其他地方。

    firefox 插件还允许在 FF IDE 中进行大量控制

    关于xul - Firefox 插件单击按钮下载文件并将其发布到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29531206/

    相关文章:

    iphone - 检查 bundle 中是否存在图像 - iPhone

    iphone - NSFileManager removeItemAtPath 锁定主线程

    ios - 如何使用 FileManager 在 iOS 中写入文件?

    css - Firefox 的 URL 建议弹出窗口的 CSS 是什么?

    javascript - Firefox Addon 添加了一个 div/iframe 但无法设置 innerHTML/src

    javascript - 如何使标签在屏幕上居中?

    ios - 有没有办法强制ios系统清除部分(或全部)应用程序的缓存?

    objective-c - 如何获取 cocoa 中目录的大小和可用空间?

    javascript - Mozilla XUL - 对话框按钮的 ID

    java - 在XUL中设置java类(jar文件)的类路径,通过javascript访问