[crx] Can't for the life of me get XML via XMLHttpRequest through an XSL site (wowarmory.com)

102 views
Skip to first unread message

Mike

unread,
Apr 19, 2010, 5:41:45 PM4/19/10
to Chromium-extensions
I have a content-script extension that requests character information
from wowarmory.com (a World of Warcraft oriented website), and embeds
it into another website. wowarmory.com is using XSLT to serve its
pages.

My Chrome extension was ported through a GreaseMonkey script for
Firefox. The GM content-script uses GM_xmlhttprequest method to handle
cross-origin XHR. In Chrome I have my content-script using sendRequest
and background.html listening for onRequest, then handling
XMLHttpRequest.

The problem is these two methods return different results for unknown
reasons.

Grease Monkey script: Without sending any additional headers, the
response.responseText is an XML formatted document.
response.responseXML is null, for some reason. Unimportant, as I use
DOMParser().parseFromString() to make the responseText an XML object.

Chrome script: Without sending any additional headers, the
response.responseText is an HTML formatted document.
response.responseXML is null. This is a problem. I can't make an XML
object out of HTML text. Why did Chrome return HTML text but GM
returned XML text?

In my Chrome extension I tried setting the User-Agent header:
xhr.setResponseHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows
NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1')

However, response.responseText still returned HTML text.

An older or invalid user-agent would have wowarmory.com respond w/HTML
to ensure compatibility, whereas an XSLT compatible browser has
wowarmory.com respond w/XML. In theory I shouldn't have to set User-
Agent, the extension is running in Chrome. Spoofing the User-Agent is
only necessary when there's no browser environment (e.g. a request
made by a PHP script).

In short: I can't seem to get XML text from my Chrome extension using
XMLHttpRequest (either XML formatted text from responseText or an
actual XML document from responseXML), but my Grease Monkey script
gets XML formatted text without problem.

Any thoughts?

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

tech4computer

unread,
Apr 19, 2010, 6:19:55 PM4/19/10
to Chromium-extensions
Try
var xml = document.createElement("div");
xml.innerHTML = responseText;
.....
xml.getElementByTagName(.....)
> To post to this group, send email to chromium-extensi...@chromium.org.
> To unsubscribe from this group, send email to chromium-extensions+unsubscr...@chromium.org.

Mike

unread,
Apr 20, 2010, 12:43:01 PM4/20/10
to Chromium-extensions
That makes an easily traversable object, but the hierarchy and
contents are wrong. My Grease Monkey script works off requested XML,
which is obviously formatted differently than the XSLT rendered HTML.

wowarmory.com's XML doc looks something like this:

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl"
href="/_layout/character/sheet.xsl"?><page globalSearch="1"
lang="en_us" requestUrl="/character-sheet.xml">
<tabInfo subTab="profile" tab="character" tabGroup="character"
tabUrl="r=Blood+Furnace&amp;cn=Test&amp;gn=Despise"/>
<characterInfo>
<character battleGroup="Retaliation" charUrl="r=Blood
+Furnace&amp;cn=Test" class="Warrior" classId="1" classUrl="c=Warrior"
faction="Horde" factionId="1" gender="Male" genderId="0"
guildName="Despise" guildUrl="r=Blood+Furnace&amp;gn=Despise"
lastModified="April 19, 2010" level="80" name="Test" points="3320"
prefix="" race="Orc" raceId="2" realm="Blood Furnace" suffix=" the
Pilgrim" titleId="133">
<modelBasePath value="http://us.media.battle.net.edgesuite.net/"/
>
</character>
<characterTab>

This text is contained in responseText. I then proceed in my Grease
Monkey script with:

var xmlDoc = new DOMParser().parseFromString(xhr.responseText, 'text/
xml');

var character = xmlDoc.getElementsByTagName('character')[0];
alert(character.getAttribute('battleGroup')) // Outputs "Retaliation"

However, in Chrome I don't get XML text by the same XHR request. I get
HTML text that I can't traverse exactly as the code I've already
written to traverse XML. Obviously I could write completely separate
code for Chrome to parse the HTML response, but for my particular app
that would be more cumbersome to parse and take a lot of time. At that
point I'd rather release my extension as Firefox-only and ignore
Chrome, but I want it to work in Chrome because GM content-scripts and
Chrome content-scripts are nearly the same. I just don't understand
why Chrome's XMLHttpRequest returns HTML and Grease Monkey's
GM_xmlhttprequest returns XML. I'm still convinced some kind of header
isn't being sent properly under Chrome.

Mike

unread,
Apr 20, 2010, 1:24:22 PM4/20/10
to Chromium-extensions
Alright, with a little more research I'm pretty sure this problem is
just a quirk/shortcoming of Chrome and XSL. Using a REST client
extension under Chrome and a REST client extension under Firefox,
Chrome will always respond HTML (text/html) when requesting "http://
www.wowarmory.com"User-Agent header I use; whereas Firefox will
respond XML (application/xml) unless I spoof the User-Agent header to
something invalid/incompatible.

If anyone can get Chrome to respond XML when spoofing headers with the
following Chrome REST client extension, and using the URL "http://
www.wowarmory.com", please let me know what headers you used. =)

https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgofnpcjmb?hl=en

tech4computer

unread,
Apr 20, 2010, 8:18:17 PM4/20/10
to Chromium-extensions
Maybe you can try a different approach. Instead of using XML use JSON
format for your object. Look into parse Json and json stringify
methods. Parsing xml is cumbersome, especially for maintaining the
code. Initially it will take some effort to convert your code to use
Json but on longer run you will save from lot of headache.

On Apr 20, 10:24 am, Mike <vel...@gmail.com> wrote:
> Alright, with a little more research I'm pretty sure this problem is
> just a quirk/shortcoming of Chrome and XSL. Using a REST client
> extension under Chrome and a REST client extension under Firefox,
> Chrome will always respond HTML (text/html) when requesting "http://www.wowarmory.com"User-Agent header I use; whereas Firefox will
> respond XML (application/xml) unless I spoof the User-Agent header to
> something invalid/incompatible.
>
> If anyone can get Chrome to respond XML when spoofing headers with the
> following Chrome REST client extension, and using the URL "http://www.wowarmory.com", please let me know what headers you used. =)
>
> https://chrome.google.com/extensions/detail/fhjcajmcbmldlhcimfajhfbgo...
Reply all
Reply to author
Forward
0 new messages