Firefox 插件点击按钮下载文件 post 到另一台服务器

Firefox plugin to click button download a file and post it to another server

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

Objective

问题

下面的代码

  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 的网络应用程序,以帮助测试网络应用程序。它允许很多自动化,并且可以以混合方式使用,您可以只保留在临时文件夹中查找文件的代码部分,然后 posts/uploads 在其他地方查找文件。

firefox 插件还允许在 FF 中进行更多控制IDE

  function download_and_post(){

      //simple version

      //button click or whatever that starts the download
      window.content.location.href = "javascript:void download_document()";



      var dJsm = Components.utils.import("resource://gre/modules/Downloads.jsm").Downloads;
      var tJsm = Components.utils.import("resource://gre/modules/Task.jsm").Task;
      var fuJsm = Components.utils.import("resource://gre/modules/FileUtils.jsm").FileUtils;
      var nsiPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);


        var view = {
           onDownloadChanged: function (download) {
              if (download.succeeded) {
               var file = fuJsm.File(download.target.path);
               NetUtil.asyncFetch(file, function(inputStream, status) {
                 if (!Components.isSuccessCode(status)) {
                  return;
                 }
                  var data =  NetUtil.readInputStreamToString(inputStream, inputStream.available());
                  btoa_data = window.btoa(data);

                  var xmlhttp = new XMLHttpRequest(); 
                  xmlhttp.open("POST","http://someserver.com",true);
                  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                  xmlhttp.send(
                      "file_data="+encodeURIComponent(btoa_data)
                      +"&file_name="+ get_file_for_os(download.target.path)
                  );
              });
                tJsm.spawn(function () {
                   let list = yield dJsm.getList(Downloads.ALL);
                   list.removeView(view);
                }).then(null, Components.utils.reportError); 
              }
           }
        };

        tJsm.spawn(function () {
           let list = yield dJsm.getList(Downloads.ALL);
           list.addView(view);
        }).then(null, Components.utils.reportError);
  }


  function get_file_for_os(file){
      var osString = Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime).OS; 
      if (osString == "Linux"){
          var document_name_no_path = file.split('/').pop();
      }else{
          var document_name_no_path= file.split("\").pop();
      }
      return document_name_no_path;
  }

这适用于所有平台并发送文档名称和数据 ;-)