XMLHttpRequest issue

A

Axel Bock

Hello all,

I am new to the whole JavaScript stuff, but I would likt to get in
with a little AJAX. XMLHttpRequest to be precise, for a Greasemonkey
script.

My idea: To enhance EVERY Google search result with maybe the digg
count for the story.

But it does not work - my main problem is that the callback function
only applies to the LAST element in the search results list, which is
kind of weird. So only the very last google search result on the page
is modified.

Can anyone help me maybe? The script code is below.

thanks in advance for any answers!
Axel.


<pre>
var godren = {
stories : "http://services.digg.com/stories",
appkey : "http://the-me.de/digg",

GetLinksWrapper : function(link, parent_element) {
rq = new XMLHttpRequest();
ln = link;
pe = parent_element;
cb = function() {
if (rq.readyState == 4) {
GM_log("response: " + rq.responseText);
GM_log("url: " + url);
GM_log("request status: " + rq.status)
pe.appendChild(document.createTextNode(" yuddi"));
}
};
var url = godren.stories
+ "?appkey=" + escape(godren.appkey)
+ "&link=" + escape(link)
+ "&type=json";
rq.open("GET", url, true);
rq.setRequestHeader("User-Agent", "God-Ren/1.0");
rq.onreadystatechange = cb;
rq.send();
},

get_link_for : function(result) {
return result.childNodes[0].toString();
},

find_google_results : function() {
// google result links are tag "h3" with class "r"
elements = document.getElementsByTagName("h3");
results = new Array();
for (var i=0; i<elements.length; i++) {
element = elements;
if (element.className=="r") results.push(element)
}
return results;
}
}

// main loop

results = godren.find_google_results();
for (var i=0; i<results.length; i++) {
result = results;
// prevent multiple insertions - maybe pagerization? :)
if (result.childNodes.length == 1) {
link = godren.get_link_for(result);
new godren.GetLinksWrapper(link, result);
}
}
</pre>
 
G

Gregor Kofler

Axel Bock meinte:
Hello all,

I am new to the whole JavaScript stuff, but I would likt to get in
with a little AJAX. XMLHttpRequest to be precise, for a Greasemonkey
script.

My idea: To enhance EVERY Google search result with maybe the digg
count for the story.

But it does not work - my main problem is that the callback function
only applies to the LAST element in the search results list, which is
kind of weird. So only the very last google search result on the page
is modified.

Can anyone help me maybe? The script code is below.

thanks in advance for any answers!
Axel.


<pre>

Won't help on usenet - which is pure text.
var godren = {
stories : "http://services.digg.com/stories",
appkey : "http://the-me.de/digg",

GetLinksWrapper : function(link, parent_element) {
rq = new XMLHttpRequest();
ln = link;
pe = parent_element;
cb = function() {

Is there a reason, why you use global variables? They get overwritten
with each instancing of GetLinksWrapper() - I suspect that's the problem.


Gregor
 
A

Axel Bock

Is there a reason, why you use global variables? They get overwritten
with each instancing of GetLinksWrapper() - I suspect that's the problem.

Actually, no. I just don't get this scope-stuff in my head.
I have changed it now to something less global (I hope), but I have
another problem, which simply refuses to be solved:

IN short: "this" in the anonymous callback function is NOT the same
"this" as just before. I get in the "marked line" the error: "this.rq
s undefined".

Ideas? Right now I am pretty annoyed, because I don't really seem to
understand a thing here :)


Thanks!
Axel.



GetLinksWrapper : function(link, parent_element) {
this.rq = new XMLHttpRequest();
this.pe = parent_element;
GM_log("parent this: " + this); // -> "object"
this.cb = function() {
GM_log("this: " + this); // -> "[XPCNativeWrapper [object
XMLHttpRequest]]" (WTF??)
if (this.rq.readyState == 4) { /* MARKED LINE */
GM_log("response: " + rq.responseText);
GM_log("url: " + url);
GM_log("request status: " + this.rq.status)
this.pe.appendChild(document.createTextNode("
yuddi"));
}
};
this.url = godren.stories
+ "?appkey=" + escape(godren.appkey)
+ "&link=" + escape(link)
+ "&type=json";
this.rq.open("GET", this.url, true);
this.rq.setRequestHeader("User-Agent", "God-Ren/1.0");
this.rq.onreadystatechange = this.cb;
this.rq.send();
},
 
G

Gregor Kofler

Axel Bock meinte:
Actually, no. I just don't get this scope-stuff in my head.
I have changed it now to something less global (I hope), but I have
another problem, which simply refuses to be solved:

IN short: "this" in the anonymous callback function is NOT the same
"this" as just before. I get in the "marked line" the error: "this.rq
s undefined".
Yes.

Ideas? Right now I am pretty annoyed, because I don't really seem to
understand a thing here :)

"this" refers to the XMLHttpRequest object in the callback.

You need a closure:
GetLinksWrapper : function(link, parent_element) {
this.rq = new XMLHttpRequest();
this.pe = parent_element;
GM_log("parent this: " + this); // -> "object"

var that = this;
this.cb = function() {
GM_log("this: " + this); // -> "[XPCNativeWrapper [object
XMLHttpRequest]]" (WTF??)
if (this.rq.readyState == 4) { /* MARKED LINE */
if(that.rq.readyState == 4) {
....
GM_log("response: " + rq.responseText);
GM_log("url: " + url);
GM_log("request status: " + this.rq.status)
this.pe.appendChild(document.createTextNode("

same here.
yuddi"));
}
};
this.url = godren.stories
+ "?appkey=" + escape(godren.appkey)
+ "&link=" + escape(link)
+ "&type=json";
this.rq.open("GET", this.url, true);
this.rq.setRequestHeader("User-Agent", "God-Ren/1.0");
this.rq.onreadystatechange = this.cb;
this.rq.send();
},

Gregor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top