Turning xmlhttprequest response into objects.

Subscribe to Turning xmlhttprequest response into objects. 6 posts, 2 voices

 
Buggy Menot Scriptwright

I'm using xmlhttprequest to fetch data from an offsite webpage, but I've run into a bit of a snag. The response is just pure html, not an object I can search. The data I'm looking for is in a table and the text length isn't always the same (they're names and rankings), so I can't do a substring effectively. Each field with name, rank, and other info is enclosed in its own <tr> tags within a table, so once I had it in object format it'd be pretty easy to parse, hopefully. Any ideas are appreciated.</tr>

 
alien_scum Scriptwright

This thread might help but sometime it is easier just to use a regular expression eg

results.match(/<tr[^>]*class="rank"[^>]*>([^<]*)/)[1]

 
Buggy Menot Scriptwright

Thanks, that worked out great. Another question if you don't mind: I'm planning on using two different functions. One will get the HTML and parse it to pass on to another function, is there a way to make the function fetching the data wait until it's done before continuing? Right now I keep getting undefined as a result, though it works fine if I keep it all as one function.

 
alien_scum Scriptwright

I'm not sure exactly what you want to do, but the easiest way would be to call the second function from the callback function of the XHR. if that is not an option you would have to have to use something like this wait function.

This should illustrate what you want

//code here evaluates first
GM_xmlhttpRequest({
  method: 'get',
  url: 'http://www.google.com/search?q='+query,
  onload:function(result) {
    //code in here evaluates third
    parse(result.responseText);
    doStuff()
  }
});
//code down here evaluates second
function doSuff(){
  //this evaluates fourth
}

 
Buggy Menot Scriptwright

Well what I have is two functions. There is a page formatted like so:
name | location | type | last played
name | location | type | last played
name | location | type | last played
name | location | type | last played

function getinfo(ranknum,gettype) {
xmlhttprequest {
onload: function(result) {
if (gettype == name) return parsed name
if (gettype == location) return parsed location
etc
}
}

Then I have another function to actually create a div object and write all the parsed data. If I was only doing it in one function I could just have it all encapsulated in one onload, but if I'm doing multiple executions of the function that gets and parses the data I figure I'm better off compressing it this way if I can only figure out how to make it stop that one function until the html request is finished.

 
alien_scum Scriptwright

This thread might help then as you want to wait for lots to finish, also note that return probably isn't going to work the way you expect here, you need a global variable (probably an array).