[Greasemonkey] Bug in GM_xmlhttpRequest?
Jeremy Dunck
jdunck at gmail.com
Wed May 4 12:44:30 EDT 2005
On 5/4/05, Mark Pilgrim <pilgrim at gmail.com> wrote:
> I'm calling GM_xmlhttpRequest and my onload callback function is
> getting called twice. Is this expected behavior?
No. It is possible that I'm not seeing what you're seeing, because
I'm running a version that has a patch against GM_xmlhttpRequest;
however, I expect that that patch only affected onreadystateload.
That said...
> code snippet:
I changed to this:
GM_xmlhttpRequest({
method: 'GET',
url: 'http://greaseblog.blogspot.com/atom.xml',
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
},
onload: function(responseDetails) {
GM_log('in onload');
var parser = new DOMParser();
var dom = parser.parseFromString(responseDetails.responseText,
"application/xml");
//exporting for shell debugging.
window.xmldom = dom;
var entries = dom.getElementsByTagName('entry');
GM_log('got entries:' + entries.length);
var title, p;
for (var i in entries) {
GM_log('entry[' + i + ']');
title = entries[i].getElementsByTagName('title')[0];
GM_log('got title');
p = document.createElement('p');
p.appendChild(document.createTextNode(title.textContent));
GM_log('added title textcontent');
document.body.appendChild(p);
GM_log('added container p');
}
}
});
====
Note that in the loop, you'll get
entry[0]
entry[1]
and so on.
But at the end, you'll die on:
entry[length].
The problem is that while, for native JS arrays, the length property
is not included in enumeration, it apparently is included on
HTMLCollection (which is what I got when running your sample code).
I think this could be considered a bug in Firefox, but I'm not certain.
So: to do what you want, you'll have to use crusty old for(i=0;
i<entries.length; i++).
Now, if that doesn't address the "calling two times" problem, please
let me know.
More information about the Greasemonkey
mailing list