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:

Code (javascript)

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.

This entry was posted on Friday, May 9th, 2008 at 2:31 pm and is filed under greasemonkey. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No Responses to “Greasemonkey Bug - DOMNodeInserted event doesn’t allow GM_xmlhttpRequest”

No comments yet

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>