Issue with Greasemonkey and XML objects

112 views
Skip to first unread message

Mister Fanwank

unread,
Feb 19, 2012, 10:59:12 PM2/19/12
to greasemonkey-users
0 down vote favorite
share [g+] share [fb] share [tw]


I'm working on a Greasemonkey script that will download an XML file
via GM_xmlhttpRequest, do some Regex work on it, then convert is back
into an XML object and post it back to the website, again, with
GM_xmlhttpRequest.

I have successfully been able to download the XML file, but I can't
convert it to an XML object.

downloadedXML.toString();
downloadedXML = downloadedXML.replace(/<\?xml.+\?>/, "");
downloadedXML = downloadedXML.replace(/sometag>/g, "anothertag>");
downloadedXML=new XML(downloadedXML);

This code works fine when it's in a webpage and I copy/paste the XML
into a text box manually and copy the text box contents into the
downloadedXML variable, but in Greasemonkey it throws a fit, saying:

Error: uncaught exception: ReferenceError: xml is not defined

Or, if I switch new XML(downloadedXML); to
downloadedXML.toXmlString(); it gives me:

Error: uncaught exception: TypeError: downloadedXML.toXmlString is not
a function

I'm not sure what's happening or how to fix this. Googling around
doesn't give me any results of people with a similar problem.

Thanks.

Anthony Lieuallen

unread,
Feb 19, 2012, 11:21:56 PM2/19/12
to greasemon...@googlegroups.com
On Sun, Feb 19, 2012 at 10:59 PM, Mister Fanwank <mister...@gmail.com> wrote:
...

I have successfully been able to download the XML file, but I can't
convert it to an XML object.

   downloadedXML.toString(); 
...

I don't know how you're getting "downloadedXML" but as documented [1] GM_xmlhttpRequest only returns .responseText, and getting an XML object can be done as like:
      responseXML = new DOMParser()
        .parseFromString(response.responseText, "text/xml");
The same applies for turning any (valid) XML string into a DOM object.

[1] http://wiki.greasespot.net/GM_xmlhttpRequest
Reply all
Reply to author
Forward
0 new messages