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

    Inserting IMG Links

    So my goal was to insert a mini ICO image (of an R) and have it be a link to an external news page for each individual player on a fantasy team. The code below gets the proper link to each player and inserts the image and corresponding link one at a time though a couple different functions. I have inserted the IMG successfully (see attached), but I am having issues with the link. There are times when the images load accurately with the correct link and others when some of the 'href' elements say undefined. And it isn't consistent either, as to imply that it is a specific coding error. It is completely random and sometimes they are even all correct. Could this have something to do with the asynchronicity of the GM_xmlhttprequests and the loop? If so, is there some way to get around that?

    This is the bottom of the code which gets fed an array teamURL with all the proper URLs. All of the variables and arrays have the correct information, as they have been checked and double checked. It is almost as if I need a way to slow down the loop to let it do its thing... if that makes any sense.

    Code:
    var count = 0;
    var item = 0;
    
    for (var k=0; k< myPlayersArray.length; k++) 
    {	   
    	GM_xmlhttpRequest({
    					method:"GET",
    					url:teamURL[k],
    					headers:{"User-Agent":"Mozilla/5.0","Accept":"text/xml"},
    					onload:function(response){findInfo(response);}
    					}) ;	
    }
    
    var allElements = document.evaluate("//*[contains(@href, 'http://sports.yahoo.com/nfl/players/')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
    
    var baseRotoworldURL = 'http://www.rotoworld.com';
    
    function findInfo( response )
    {
    	var doc = document.createElement( "div" ) ;
    	doc.innerHTML = response.responseText ;
    	
    	//Get appropriate td data on the rankings page
    	var teamPageList = document.evaluate("//a[contains(@href, 'player')]",doc,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
    		
    	for (var m=0; m<teamPageList.snapshotLength; m++)
    	{				
    		if ((teamPageList.snapshotItem(m).innerHTML.toString()) == (myPlayersArray[count].toString()))
    		{
    			var playerHTML = teamPageList.snapshotItem(m).parentNode.innerHTML;
    			var match = playerHTML.match(/\/nfl\/\d{1,10}\/[a-z]+\-[a-z]+/);
    
    			var playerURL = baseRotoworldURL +"/recent" + match
    		
    				break;  //loop until you get a hit
    		}					
    	}
    	 	
    	addGameLogIcon(allElements, playerURL);
    	count++;
    	
    }
    
    function addGameLogIcon(allElements, playerURL) {
    
    	var thisElement;
    	var rotoworldLink;	
    		
    	var rotoworldLinkGraphic = "data:image/gif;base64,"+"AAABAAEADw8AAAEAGAA0AwAAFgAAACgAAAAPAAAAHgAAAAEAGAAAAAAAAAAAAEgAAABIAAAAAAAAAAAAAADR09TR09TR0tPR0tPQ0tPQ0tPQ0tPQ0dTQ0tPQ0tPQ0tPQ0dPQ0tPR09TR09S9AADR09TR09TP0dOvssuan8acocfFx9HQ0tPGyNGdocean8edocfDxdDR09TR09S9AADR09TR09TGydFHRsEsKNsrJ9SGicLP0dNsb74rJ9csKNsrJ8iTlcPR09TR09S9AADR09TR09TEx9BFRcYvKu0sKOOChcK3ucwuKsgvKu0uKedUVMDIydHR09TR09S9AADR09TR09TEx9BERMQuKOcsJ9+ChcFnaLstJ+IuKOc0McuqrMnQ0tTR09TR09S9AADR09TR09TEx9BERMItKOEsJ9lfYbYuKsQtKOEsJ9hxc7/P0NPQ0tPR09TR09S9AADR09TR09TEx9BERMAsJ9srJ9gqJsgrJ9ksJ9sqJsdub7vJytHQ0tPR09TR09S9AADR09TR09TEx9BEQ70sJ9QsJ9UsJtQsJ9QsJtUsJtQrJspzdb3Q0dPR09TR09S9AADR09TR09TEx9BEQ7stJc4sJco8OrZJSLkxLbotJc0tJs49Ori/wM7R09TR09S9AADR09TR09TEx9BDQ7ksJ8gsJsN6fL3Bw855e7wrJcIsJ8c5Nrm7vs3R09TR09S9AADR09TR09TEx9BEQ7YtJ8IsJ780MLE+O7QuKrYsJ8EtJ8I/PLTBws/R09TR09S9AADR09TR09TEx9BERLUtKMEtKMEtJ8EtJ8EtJ8AsJ8EtKLh7fL3P0NTR09TR09S9AADR09TR09THytFMTrQpJrUpJrUpJrUpJrUvLrVGRLSKisDLzNLQ0tTR09TR09S9AADR09TR09TR09TDxdC7vc67vc67vc67vc6+wc/HytHR09TR09TR09TR09TR09S9AADR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09S9AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
    		
    	thisElement = allElements.snapshotItem(item);		
    	rotoworldLink = document.createElement('a');
    	rotoworldLink.setAttribute("href", playerURL);
    	rotoworldLink.setAttribute("target","_blank");
    	rotoworldLink.innerHTML = "<img border='0' src='" + rotoworldLinkGraphic +"'>" ;
    	thisElement.parentNode.appendChild(rotoworldLink, thisElement.nextSibling);
    	item=item+3;	
    }
    Attached Thumbnails Attached Thumbnails Inserting IMG Links-cell.jpeg  
    Last edited by bszen27; 09-29-2011 at 09:35 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    Could this have something to do with the asynchronicity of the GM_xmlhttprequests and the loop?
    Yes, it is the case. What I have understood from the given code is that, you are invoking multiple AJAX requests, which not always respond in the same sequence they have been invoked.

    The way around for this is to make synchronous call. This can be done by setting the third parameter of xmlHttpObject.open() method to false.

    Try that out.. and may be your problem might get resolved.

    Thanks & Regards,
    Niral Soni

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I cannot use that function, because it is only restricted to the current domain. I need to use "GET" to go outside the domain, so I must use GM_xmlhttprequest, which I don't believe has a synchronous option.

    Please help. Thank you.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It is not a wise thing to use DOM methods altogether with innerHTML in the same sequence. Why don't you use createElement() and setAttribute() to create and insert your IMG element as well?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    30
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor View Post
    It is not a wise thing to use DOM methods altogether with innerHTML in the same sequence. Why don't you use createElement() and setAttribute() to create and insert your IMG element as well?
    I believe I did use those elements at the end of the code to insert the images. No?

    I find that when writing a userscript, it is very difficult to avoid using DOM and innerHTML together in the type of situation I am trying to conquer. When you loop through requests like that, it makes the entire script one big sequence. Or so it seems.


  •  

    Posting Permissions

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