Turning xmlhttprequest response into objects.
|
|
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> |
|
|
This thread might help but sometime it is easier just to use a regular expression eg
results.match(/<tr[^>]*class="rank"[^>]*>([^<]*)/)[1] |
|
|
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. |
|
|
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
}
|
|
|
Well what I have is two functions. There is a page formatted like so:
function getinfo(ranknum,gettype) {
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. |
|
|
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). |
