...

View Full Version : Inserting IMG Links



bszen27
09-29-2011, 09:11 PM
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.


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,"+"AAABAAEADw8AAAEAGAA0AwAAFgAAACgAAAAPAAAAHgAAAAEAGAAAAAAAAAAAAEgAAABIAAAAAAAAAAAAAADR09TR09TR0tPR0tPQ 0tPQ0tPQ0tPQ0dTQ0tPQ0tPQ0tPQ0dPQ0tPR09TR09S9AADR09TR09TP0dOvssuan8acocfFx9HQ0tPGyNGdocean8edocfDxdDR 09TR09S9AADR09TR09TGydFHRsEsKNsrJ9SGicLP0dNsb74rJ9csKNsrJ8iTlcPR09TR09S9AADR09TR09TEx9BFRcYvKu0sKOOC hcK3ucwuKsgvKu0uKedUVMDIydHR09TR09S9AADR09TR09TEx9BERMQuKOcsJ9+ChcFnaLstJ+IuKOc0McuqrMnQ0tTR09TR09S9 AADR09TR09TEx9BERMItKOEsJ9lfYbYuKsQtKOEsJ9hxc7/P0NPQ0tPR09TR09S9AADR09TR09TEx9BERMAsJ9srJ9gqJsgrJ9ksJ9sqJsdub7vJytHQ0tPR09TR09S9AADR09TR09TEx9BEQ70 sJ9QsJ9UsJtQsJ9QsJtUsJtQrJspzdb3Q0dPR09TR09S9AADR09TR09TEx9BEQ7stJc4sJco8OrZJSLkxLbotJc0tJs49Ori/wM7R09TR09S9AADR09TR09TEx9BDQ7ksJ8gsJsN6fL3Bw855e7wrJcIsJ8c5Nrm7vs3R09TR09S9AADR09TR09TEx9BEQ7YtJ8Is J780MLE+O7QuKrYsJ8EtJ8I/PLTBws/R09TR09S9AADR09TR09TEx9BERLUtKMEtKMEtJ8EtJ8EtJ8AsJ8EtKLh7fL3P0NTR09TR09S9AADR09TR09THytFMTrQpJrUpJrU pJrUpJrUvLrVGRLSKisDLzNLQ0tTR09TR09S9AADR09TR09TR09TDxdC7vc67vc67vc67vc6+wc/HytHR09TR09TR09TR09TR09S9AADR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09TR09S9AAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";

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;
}

niralsoni
09-30-2011, 06:15 PM
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

bszen27
10-01-2011, 01:43 PM
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.

Kor
10-01-2011, 02:46 PM
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?

bszen27
10-01-2011, 03:52 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum