View Full Version : Fetching multiple URLs with GM_xmlhttpRequest

09-17-2011, 02:48 PM
Hello all. I am working on a userscript for a site that grabs some weather information for each NFL game. I am trying to loop GM_xmlhttpRequest(s), which has worked okay, but doesn't seem to mix well with arrays. By that I mean I am attempting to push values onto an array, but it doesn't seem to work. So basically at the bottom of the code below, I get values for wind, but not for each windArray[i]. My hunch is that has something to do with the asynchronous behavior of GM_xmlhttpRequest, but I am wondering if there is anyway around that (coding wise). I greatly appreciate any input you can provide.

//Strips all html elements from a string
String.prototype.stripTags = function() {
return this.replace(/<\/?[^>]+>|&[^;]+;|^\s+|\s+$/gi,'');

var weatherURL = new Array();
weatherURL[0] = 'http://www.nflweather.com/game/2011/week-1/bills-at-chiefs';
weatherURL[1] = 'http://www.nflweather.com/game/2011/week-1/bengals-at-browns';
weatherURL[2] = 'http://www.nflweather.com/game/2011/week-1/steelers-at-ravens';

var windArray = new Array();

function getDOC(url, callback) {
method: 'GET',
url: url,
onload: function (responseDetails) {
var dt = document.implementation.createDocumentType("html",
"-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd"),
doc = document.implementation.createDocument('', '', dt),
html = doc.createElement('html');

html.innerHTML = responseDetails.responseText;

for (var i=0; i<weatherURL.length; i++) {

getDOC(weatherURL[i], function(doc) {

var pageTds = doc.getElementsByClassName('no-line');
var wind = pageTds[9].innerHTML.stripTags();

09-17-2011, 03:03 PM
This (http://www.javascript-demos.com/Test/Weather.html) code has a setting to either continuously rotate through a list of locations or create a select list of them.

09-17-2011, 11:30 PM
Thank your for the response. This example is very useful for other things I am looking to do, but it only seems to access one website per user action. So if I choose Cleveland, it will access that one site and grab all relevant information. That is something that I have been able to do without a problem. My issue is that I need to grab a text forecast from 16 different web pages and place each piece of information in a table on another web page. Does anyone have any thoughts on how to cycle through GM_xmlhttpRequest(s) and dealing with the asynchronous behavior? Thanks.

09-26-2011, 09:03 PM
Any new ideas for this? I basically have one page where I want to put a bunch of weather forecasts from various cities, but I need to be able send multiple GM_xmlhttprequests to separate sites (for each city) at the same time to do so. Looping those types of requests doesn't seem to work as they all happen at the same time and kill the code.

If anyone can help with this, that would open a very large window for me to do much more with scripts I am writing. Thanks!

11-19-2016, 05:47 AM
I do it referring to

Passing parameters to GM_xmlhttpRequest callback function - Userscripts.org (http://userscripts-mirror.org/topics/51161)

For anybody having scope issues with GM_xmlhttpRequest - Userscripts.org (http://userscripts-mirror.org/topics/193)

11-19-2016, 05:49 AM