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.