0

I am attempting to export some data to an excel file manually using javascript and a loop around a GM.xmlHttpRequest using Tampermonkey. The loop is happening at a different time and the request is using a number outside the loop.

I have tried changing to an ajax request but i get CORS problems. I have changed from an object to an array and moving the loop, also tried making sychronous true with no success.

var mydata = [];
var loop;
var NewJson;
var changeToExcelData;

var url = "json/DashboardQuestionnaireForm.php";

function changeURL () {
    for(loop = 0; loop < 4; loop++){
    url = "json/DashboardQuestionnaireForm.php";

     console.log("loop inside function = " + loop);
    }

                      }



function HTTPRequest () {

  GM.xmlHttpRequest({
    method: "GET",
    url: url,
     context:  loop,
        headers:    {
        "Content-Type": "application/json"
                },
      onload: function(exportData) {
    //

          changeURL();


          console.log("Download excel ajax working . . . loop = " + loop);

    NewJson = JSON.parse(exportData.responseText);

        console.log("this is newjson "+NewJson[0].results[0].Lecturer);


          mydata[loop] = JSON.stringify(NewJson[0].results[0].Lecturer);
          console.log(mydata[loop]);


                                     } //end exportData function

                      }); //end ajax call



} //end function



console.log("starting export . . .");


function downloadData () {

var downloadLink = document.createElement("a");


console.log("mydata = "+mydata);
     var blob = new Blob([mydata],{ type: 'text/csv;charset=utf-8;' });
     var url2 = URL.createObjectURL(blob);
     downloadLink.href = url2;
     downloadLink.download = "exportData.csv";
     downloadLink.innerHTML = "downloadCSVnew";
     document.body.appendChild(downloadLink);
     downloadLink.click();

}

HTTPRequest (); downloadData ();

So i would expect the loop to start and hit the request first but instead it seems to go through the loop and then insert '5' during the call which would be outside the loop? The reason i am doing it this way is that i need to take data from a lot of differen webpages.

Bangorsteve
  • 19
  • 1
  • 6
  • You need to either wrap the `GM.xmlHttpRequest` call in a closure or pass (and use) [the `context` parameter](https://www.tampermonkey.net/documentation.php#GM_xmlhttpRequest). – Brock Adams Jul 18 '19 at 20:20
  • Thanks for your prompt reply Brock. I really appreciate it. So im pretty much a beginner but i can see that ur putting the request as a function inside a loop, is this correct? I also see the context is a property which will be added to the response object. Could u explain some more about this please? – Bangorsteve Jul 18 '19 at 20:35
  • See [this Q&A](https://stackoverflow.com/questions/5191850/how-can-i-handle-multiple-ajax-results-in-a-userscript). You would pass `context: loop,`. Then, in your `onload: function`, you would use: `var loop = exportData.context;`. – Brock Adams Jul 18 '19 at 20:45
  • Also, don't even *try* something like `synchronous: true,`. That way lies wracke and ruine... – Brock Adams Jul 18 '19 at 20:46
  • Brilliant, I will try your suggestion and I will also take the synchronous tip on board. Thanks again – Bangorsteve Jul 18 '19 at 20:49

0 Answers0