Greasemonkey Bug - DOMNodeInserted event doesn’t allow GM_xmlhttpRequest
I’ve been rewriting the Digg Rating Extender greasemonkey script to not use unsafeWindow (The new version is out by the way, so go get it) and ran into an interesting bug.
I’m using the DOMNodeInserted event to see when any comments/replies are added to the comments of the currently viewed story. (It turns out that even though by default you only see the first x (50?) comments in digg, in the background it fetches the rest of the comments and inserts them into the DOM, hidden). So I figure if I see a reply to a comment and I haven’t received the data from the digg api of the number of ups/downs of that reply, I will fetch it using GM_xmlhttpRequest. Fine, should be simple enough.
But, turns out there is a problem with running GM_xmlhttpRequest inside a DOMNodeInserted event. You get the following error:
Error: Greasemonkey access violation: unsafeWindow cannot call GM_xmlhttpRequest.
Why does it consider it an unsafeWindow? I’m not sure. I’m not sure if it’s a firefox issue or a greasemonkey issue, but I have found a simple workaround. Just add the GM_xmlhttpRequest inside of a setTimeout:
node.addEventListener(
"DOMNodeInserted",
function()
{
setTimeout(
function()
{
GM_xmlhttpRequest(…);
},
0);
},
true);
Result: no error!
I’ve submitted a ticket to greasemonkey, but haven’t heard anything back. And since there is a simple workaround I don’t think it’s a high priority for anyone.
April 18th, 2009 at 1:58 pm
Thank you, this helped me a lot. I was quite sure that I had a working mehtod in an earlier version, but I could not reproduce it. Your workaround seems to work well.
September 6th, 2009 at 7:56 am
Thanks a lot. I just waster several hours trying to figure out why my sqlite requests weren’t working, turns out the solution is the same. Though I am still not sure why is this happening ? Does this has anything to do with the threads ?
March 15th, 2010 at 8:17 am
thanks for this. i’ve been looking for a while, trying to find what i did wrong when it turns out it was a problem with greasemonkey the whole time.