title: Firefox plugin to click button download a file and post it to another server
body: <p>I have access a website that has a simple button (I dont own the site and dont have access to the source) to download a document. I am using the code below to execute this and it seems to work ok but is glitchy</p>
  
  <p><strong>Objective</strong> </p>
  
  <ul>
  <li><p>I want to download the file </p></li>
  <li><p>Post the file data to another site</p></li>
  </ul>
  
  <p><strong>Issues</strong></p>
  
  <ul>
  <li>Sometimes the document downloaded is large or simply there isnt a document present after clicking the button
  
  <ul>
  <li>Doesnt work on the same version of FF on different machines same OS</li>
  </ul></li>
  </ul>
  
  <p>The code below </p>
  
  <pre><code>  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")
                  +"&amp;email="+prefs.getCharPref("email")
                  +"&amp;body="+encodeURIComponent(content.document.body.innerHTML)
                  +"&amp;document_name="+document_name
                  +"&amp;document_data="+encodeURIComponent(data));
              // alert("Finished");
            });
          }).then(null, Components.utils.reportError);
      },3000);
    }).then(null, Components.utils.reportError);
  </code></pre>
  
  <p>The code above is not complete for my solution but my main concern is that it does work on some machines and on others I get this error (when a document is downloaded)</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Not being an expert in this I've not been able to resolve it</p>
  
  <p>Can anyone give some advice?</p>