最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DOM manipulation inside web worker - Stack Overflow

programmeradmin4浏览0评论

I know that workers can't manipulate the document directly, but how about the DOM API methods? 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?!

There's absolutely no way to work with virtual DOM on web workers?!

I know that workers can't manipulate the document directly, but how about the DOM API methods? 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?!

There's absolutely no way to work with virtual DOM on web workers?!

Share Improve this question edited Nov 21, 2017 at 10:33 cvsguimaraes asked Nov 10, 2015 at 23:28 cvsguimaraescvsguimaraes 13.2k9 gold badges51 silver badges74 bronze badges 5
  • 1 Perhaps some nodejs library with this purpose exists?! – cvsguimaraes Commented Nov 10, 2015 at 23:28
  • Can you change the request so that it doesn't receive a HTML fragment, but a usable data structure like JSON? Are you doing this in-browser, or in node? – CodingIntrigue Commented Nov 11, 2015 at 10:21
  • @RGraham in-browser but I may use npm modules if needed. About the response format in HTML, that was just a hypothetical use case for this... – cvsguimaraes Commented Nov 11, 2015 at 15:49
  • 1 You could check out github./jindw/xmldom or github./blowsie/Pure-JavaScript-HTML5-Parser – CodingIntrigue Commented Nov 11, 2015 at 15:57
  • @RGraham Thank you, I'll check which one works better – cvsguimaraes Commented Nov 11, 2015 at 16:04
Add a ment  | 

3 Answers 3

Reset to default 8

Support in browsers

DOMParser or document.implementation are usually used to parse HTML to DOM in browsers. 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. See this bug: https://bugzilla.mozilla/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. The best bet seems to be JSDOM, but you'll need to figure out how to browserify it.


Here's my failed attempt with DOMParser, I keep it for future experiments on this topic: https://jsfiddle/svaqb2wn/2/

You can use one of the DOM implementations running in a Web Worker:

  • Domino
  • JSDOM
  • XMLDOM
  • WorkerDOM
  • Via.js

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. However, in the current web worker spec we don't have access to XMLHttpRequest. An alternative is to use one of a few popular XML parsing libraries:

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

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

HTML is just XML, so you can parse your data in this manner. An example using xmljs from npmjs above:

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; }));
});
发布评论

评论列表(0)

  1. 暂无评论