如何从javascript高效访问gzip压缩包? [英] How do I efficiently access gzipped xml from javascript?

查看:103
本文介绍了如何从javascript高效访问gzip压缩包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从javascript(实际上来自Greasemonkey)有效地访问一个大型的gzip压缩文件。
不幸的是,服务器没有提供Content-Encoding标头,而Content-Type是application / x-gzip,所以firefox不会(据我所知)自动对它进行充气。
如果有办法伪造firefox,这将是理想的。除此之外,我需要一些方法来有效地进行通货膨胀...我现在使用的东西大约需要30秒才能收缩1.2Mb的压缩文件;我希望在5秒内完成它。

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.

(我正在研究的Greasemonkey脚本不能有任何其他外部服务器依赖,所以代理和呈现Content-Encoding标头不是一个选项。)

(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.)

我现在在做什么,我从几个地方拼凑而成。要不受干扰地接收二进制数据,我正在使用 firefox XMLHTTPRequest overrideMimeType 扩展名:

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);
        ...

然后使用 https://github.com/dankogai/js-deflate/blob/master/rawinflate.js (有几个其他javascript在那里扩充库,据我所知,基于一个较旧的库 http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt )。这是一个非常缓慢的部分。

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));

然后在innerHTML属性中弹出它来解析它:

Then popping it in an innerHTML property to parse it:

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

(我知道使用DOMParser的parseFromString可以更好地完成最后一点,但我没有得到那个工作了。)

(I know the last bit could be more properly done with DOMParser's parseFromString, but I didn't get that working yet.)

推荐答案

你只是不能用这种配置做得更好**。

You're just not going to be able to do noticeably better with this configuration**.

JavaScript太慢而无法按预期快速膨胀,并且您无法从JS可靠地调用二进制文件 - 除非将数据AJAX化到您自己的服务器(可以是本地PC)。

JavaScript is too slow to inflate as fast as desired, and you can't call a binary reliably from JS -- unless AJAXing the data to your own server (which can be the local PC).

您的改进选择似乎是:


  1. 让浏览器自动对内容进行充气。如果您已尝试使用 overrideMimeType 设置 application / x-gzip ,则可以尝试使用 GM_xmlhttpRequest (这是一个很长的镜头)。

  1. Get the browser to automatically inflate the content. If you have already tried using overrideMimeType to set application/x-gzip, you might try using GM_xmlhttpRequest instead (it's a long shot).

转换这从GM脚本到Firefox附加组件。作为附加组件,您可以访问二进制文件,例如 7-Zip ,甚至可以访问浏览器的inflate方法。你也许可以更容易地欺骗mimetype。

Convert this from a GM script to a Firefox add-on. As an add-on, you can access binaries, like 7-Zip, and might even have access to the browser's inflate method. You could probably spoof the mimetype more easily too.


**我确实注意到一些琐碎的机会来加速JS的膨胀...像 length 这样的检查在循环。唉,考虑到细节,可能不会购买超过一两秒。

**I did notice some trivial opportunities to speed up that inflating JS... things like length checks inside for loops. Alas, given the particulars, probably not going to buy more than a second or two.

这篇关于如何从javascript高效访问gzip压缩包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆