简体   繁体   English

Web Worker 中的 DOM 操作

[英]DOM manipulation inside web worker

I know that workers can't manipulate the document directly, but how about the DOM API methods?我知道工作人员不能直接操作文档,但是 DOM API 方法呢? Where did they go?!他们去哪儿了?!

For example if I make a request that receives a HTML fragment, what I'm supposed to do if need just to parse it in order to retrieve some data from a specific node?!例如,如果我发出一个接收 HTML 片段的请求,如果只需要解析它以便从特定节点检索一些数据,我应该怎么做?!

There's absolutely no way to work with virtual DOM on web workers?!绝对没有办法在网络工作者上使用虚拟 DOM?!

Support in browsers浏览器支持

DOMParser or document.implementation are usually used to parse HTML to DOM in browsers. DOMParserdocument.implementation通常用于在浏览器中将 HTML 解析为 DOM。 Neither is available in worker context.在工作环境中两者都不可用。

In Firefox, this is not possible because someone decided there will be only one DOM parser instance for all threads.在 Firefox 中,这是不可能的,因为有人决定所有线程只有一个 DOM 解析器实例。 See this bug: https://bugzilla.mozilla.org/show_bug.cgi?id=677123看到这个错误: https : //bugzilla.mozilla.org/show_bug.cgi?id=677123

In google chrome it doesn't work either.在谷歌浏览器中它也不起作用。

Workaround - external library解决方法 - 外部库

That's right, since browser developers didn't realize DOM and XML parsing will be one of main uses of WebWorkers, we'll have to fall back to external library.没错,由于浏览器开发人员没有意识到 DOM 和 XML 解析将成为 WebWorkers 的主要用途之一,我们将不得不退回到外部库。 The best bet seems to be JSDOM , but you'll need to figure out how to browserify it .最好的选择似乎是JSDOM ,但您需要弄清楚如何浏览它


Here's my failed attempt with DOMParser , I keep it for future experiments on this topic: https://jsfiddle.net/svaqb2wn/2/这是我对DOMParser的失败尝试,我保留它以备将来关于此主题的实验: https : //jsfiddle.net/svaqb2wn/2/

You can use one of the DOM implementations running in a Web Worker:您可以使用在 Web Worker 中运行的 DOM 实现之一:

I just made a library that solves this exact problem. 我刚刚建立了一个解决这个问题的库。 It is based on the cycle architecture, it is still WIP but you can find the source here. 它基于循环架构,它仍然是WIP,但您可以在此处找到源。

http://github.com/aronallen/-cycle-sandbox/ http://github.com/aronallen/-cycle-sandbox/

At first you might think the proper way to go about parsing XML is XMLHttpRequest and in 9 out of 10 cases you would be right.起初,您可能认为解析 XML 的正确方法是XMLHttpRequest并且在十分之九的情况下您是正确的。 However, in the current web worker spec we don't have access to XMLHttpRequest .但是,在当前的 Web Worker 规范中,我们无权访问XMLHttpRequest An alternative is to use one of a few popular XML parsing libraries:另一种方法是使用几个流行的 XML 解析库之一:

https://github.com/isaacs/sax-js https://github.com/isaacs/sax-js

http://xmljs.sourceforge.net/index.html or https://www.npmjs.com/package/xmljs http://xmljs.sourceforge.net/index.htmlhttps://www.npmjs.com/package/xmljs

HTML is just XML, so you can parse your data in this manner. HTML 只是 XML,因此您可以以这种方式解析您的数据。 An example using xmljs from npmjs above:使用上面 npmjs 中的 xmljs 的示例:

var XmlParser = require("xmljs");
var fs = require("fs");


var p = new XmlParser({ strict: true });
var xml = fs.readFileSync("./SOAP1.xml"); // XML in the examples direct 
var xmlNode = p.parseString(xml, function(err, xmlNode) {
    if(err) {
        console.error(err);
        return;
    }
    var nodes = xmlNode.path(["Envelope", "Body", "GetstockpriceResponse", "Price"], true);
    console.log(nodes.map(function(n) { return n.text; }));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM