开发者

Is it possible to load the CSS stylesheets applied to a DOM in node.js?

开发者 https://www.devze.com 2023-01-26 18:11 出处:网络
This is a snippet of what I\'m trying to do at the moment: var sys= require(\"sys\"), request开发者_如何转开发 = require(\"request\"),

This is a snippet of what I'm trying to do at the moment:

var sys     = require("sys"),
    request开发者_如何转开发 = require("request"),
    $       = require("jquery"),
    uri     = 'http://' + process.argv[2];

request({uri:uri}, function (error, response, context) {
  if (!error && response.statusCode == 200) {
    console.log($('body', context).css('background'));
  }
})

Obviously, this just returns undefined; is there a way to apply the stylesheets defined in the link elements so that I can query them with jQuery?


As Pointy already pointed out (scnr) this is by no means trivial.

You need to:

  1. Parse the CSS
  2. Apply the rules to the DOM
  3. Compute the current style based on the rules

Basically you need a browser rendering engine without the actual drawing, that will take a lot of code in JavaScript (50k LOC+ if one's lucky), you need to implement all the CSS versions, check the document type etc.

Right now the best thing one could do is to grab that stuff out of WebKit and bind it with V8 for a Node C extension. A pretty huge job, so unless you're willing to start this on your own, I guess you're out of luck.

Binding the things will take quite some effort, but it will still take way less time then writing the whole thing in JS from scratch (which will be horrible slow, even with the current increase in JavaScript performance).


Although not Node.js, this looks to now be possible via PhantomJS.

0

精彩评论

暂无评论...
验证码 换一张
取 消