I need to efficently access a large gzipped xml file from javascript (actually from Greasemonkey). Unfortunately, the server doesn't provide a Content-Encoding header, and Content-Type is "application/x-gzip", so firefox will not (as far as I can tell) automatically inflate it. If there's a way to fake firefox out, that would be ideal. Short of that, I need some way to efficiently do the inflation...what I'm using now takes about 30 seconds to deflate the 1.2Mb gzipped file; I'd like to get it down under 5 seconds.
(The Greasemonkey script I'm working on can't have any other external server dependencies, so proxying and presenting a Content-Encoding header isn't an option.)
What I'm doing now, I've patched together from several places. To receive the binary data unmolested, I'm using the firefox XMLHTTPRequest overrideMimeType extension:
$.ajax(url, {
dataType:'text',
beforeSend:function(xhr){
xhr.overrideMimeType('text/plain; charset=x-user-defined')
},
success:function(data){
var blob='';
for (i=0; i<data.length; ++i)
blob += String.fromCharCode(data.charCodeAt(i) & 0xff);
...
Then inflating, using a slightly modified and inlined copy of https://github.com/dankogai/js-deflate/blob/master/rawinflate.js (there are several other javascript inflate libraries out there, all as far as I can tell based on an older library http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt). This is the horrifically slow part.
// blithely assuming the gzip header won't change,
// strip a fixed number of bytes from the front
deflated=RawDeflate.inflate(blob.substring(22,blob.length-8));
Then popping it in an innerHTML property to parse it:
xmlcontainer=$('<div>');
// remove <?xml...> prolog
xmlcontainer.html(deflated.substring(45));
xmldoc=xmldoc.children();
(I know the last bit could be more properly done with DOMParser's parseFromString, but I didn't get that working yet.)