Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Fetching multiple URLs with GM_xmlhttpRequest

    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.

    Code:
    //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) {
        GM_xmlhttpRequest({
            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;
              doc.appendChild(html);
              callback(doc);
            }
        });
    }
    
    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();
    	//alert(wind);
    	windArray.push(wind);
    	//alert(windArray[i]);
    	});
    }

  • #2
    Regular Coder
    Join Date
    Nov 2009
    Posts
    247
    Thanks
    4
    Thanked 22 Times in 22 Posts
    This code has a setting to either continuously rotate through a list of locations or create a select list of them.

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •