There's a browser based game I play where you can scan sections of a grid and download an xml file of the scan. I'm trying to automate this to speed things up.
It's easy enough to generate the urls for each section of grid to download but the xml file has to be generated.
How do I actually download each xml file in the process of looping through each coordinate?
Here's what I tried so far that only resulted in the last coordinate being process (the URL has obviously been changed):
PHP Code:
function scanSystem() {
// Starting value for scans
var xMin = '0';
var yMin = '0';
// Temporary - The highest coordinates that will be scanned
var coordMax = '5';
for (var x = xMin; x <= coordMax; x++) {
for (var y = yMin; y <= coordMax; y++) {
var url = "http://xmlurl.com/index.php?x=" + x + "&y="+ y;
window.location = url;
}
}
}
Last edited by Styles2304; 02-17-2013 at 02:51 AM..
I tried modifying it slightly by creating an iframe and changing the src with a delay but all that does now is display the last xml file. Doesn't display any others and no longer downloads it:
PHP Code:
function scanSystem() { // Starting value for scans var xMin = '0'; var yMin = '0'; // Temporary - The highest coordinates that will be scanned var coordMax = '2';
for (var x = xMin; x <= coordMax; x++) { for (var y = yMin; y <= coordMax; y++) { var url = ""http://xmlurl.com/index.php?x=" + x + "&y="+ y; document.getElementById('iframe').src = url; sleep(2000); } } }
function sleep(milliSeconds){ var startTime = new Date().getTime(); // get the current time while (new Date().getTime() < startTime + milliSeconds); // hog cpu }
Some thoughts on the first post.
You don't need to make this a function do you? Just saying this could run automatically I think, You know better then me about this.
The window.location executes every time the loop runs from x=0 y=0 to count of 5 each. I am not sure if this is not what you want.
The question is How will you control where the loops stop and retrieve the url.
Last edited by sunfighter; 02-16-2013 at 04:49 PM..
It's only a function so I can call it on click . . . it will also be customizable in the future to allow for scanning only certain sectors.
As far as the loop stopping and retrieving the URL . . . that's the trick. I want it to retrieve the url at each step. At each step it generates the url for a dynamically generated XML file that I need to download.
Like I said though, it's a grid so it needs to download the url file for every from (0,0) to (2,2). (I shortened the loop for sake of testing)
The loop generates those coordinates but it doesn't access any file accept the last one.
Ok outstanding . . . the only issue is the dlURL function is breaking the code. I commented out line by line until I found it but I don't know regEx enough to fix it. See anything odd?
Last edited by Styles2304; 02-16-2013 at 09:05 PM..
Don't know. I think there's still something wrong with the dlURL . . . the alert right after it's called never fires:
PHP Code:
// Functions to setup
function scanSystem() {
// Starting value for scans
var xMin = '0';
var yMin = '0';
// Temporary - The highest coordinates that will be scanned
var coordMax = '2';
for (var x = xMin; x <= coordMax; x++) {
for (var y = yMin; y <= coordMax; y++) {
var url = "http://xmlurl/members/scanners/list.php?cockpit&xml&x=" + x + "&y="+ y;
dlURL(url);
alert('success');
}
}
}
function dlURL(url){
download( IO(url), url.split("?").slice(-1)[0].replace(/W+/g,"_")+".xml", "text/xml" );
}
function IO(U){
var X=new XMLHttpRequest;
X.open("GET",U,false);
X.send();
return X.responseText;
}
function download(strData, strFileName, strMimeType) {
var D = document,
A = arguments,
a = D.createElement("a"),
d = A[0],
n = A[1],
t = A[2] || "text/plain";
Here's the solution given me by Joseph the Dreamer on stackoverflow. I've modified it slightly but overall, this is what's working for me:
PHP Code:
function scanSystem() {
var x,
y,
xMin = 0,
yMin = 0,
coordMax = 19,
url,
iframeContainer = document.getElementById('iframe_container');
for (x = xMin; x <= coordMax; x++) {
for (y = yMin; y <= coordMax; y++) {
url = 'http://www.xmlurl.com/members/scanners/list.php?cockpit&xml&x=' + x + '&y=' + y;
$('<iframe/>',{src : url}).appendTo(iframeContainer);
}
}
}
What it does is dynamically generate an iframe for each url and then load it in it's own time. In doing this, it downloads each file as the content in the iframe loads. Works flawlessly. iframeContainer is just a hidden div so all of this happens behind the scenes. Hopes this helps somebody down the road.