How to effectively access gzipped xml from javascript?

I need to effectively access a large gzipped xml file from javascript (actually from Greasemonkey). Unfortunately, the server does not contain the Content-Encoding header, and the Content-Type does not contain "application / x-gzip", so firefox will not (as far as I can tell) automatically inflate it. If there is a way to fake firefox, that would be ideal. In short, I need some way to effectively make inflation ... what I'm using now takes about 30 seconds to blow off a file with a 1.2 Mb file size; I would like to receive it in less than 5 seconds.

(The Greasemonkey script I work, I have no other external dependencies on the server, so proxying and presenting the Content-Encoding header is not an option.)

What I am doing now, I have secured from several places. To get binary data unchanged, I use the firefox extension XMLHTTPRequest overrideMimeType :

$.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 bloat using a slightly modified and built-in copy of https://github.com/dankogai/js-deflate/blob/master/rawinflate.js (there are a few other javascript extensions too, everything is there as I can tell from the old http: / library /www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt ). This is a terribly 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 enter it in the innerHTML property to parse it:

  xmlcontainer=$('<div>'); // remove <?xml...> prolog xmlcontainer.html(deflated.substring(45)); xmldoc=xmldoc.children(); 

(I know that the last bit can be more correctly executed using the DOMParser parseFromString, but I have not received this yet.)

+4
source share
1 answer

You simply cannot significantly improve this configuration **.

JavaScript is too slow to bloat as fast as you need, and you cannot call the binary module reliable with JS - unless AJAXing the data to your own server (which may be a local PC).

Your improvement options are as follows:

  • Get a browser to automatically inflate content. If you've already tried using overrideMimeType to set application/x-gzip , you can use GM_xmlhttpRequest instead (this is a long snapshot).

  • Convert this from a GM script in addition to Firefox. As an add-on, you can access binary files, such as 7-Zip, and even have access to the browser bloat method. You could probably hide and mimetype more easily.



** I noticed some trivial possibilities to speed up JS bloat ... things like length are checked inside for loops. Alas, given the data, he’s probably not going to buy more than a second or two.

+1
source

Source: https://habr.com/ru/post/1342538/


All Articles