Steve
Steve

Reputation: 5853

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

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?

Upvotes: 0

Views: 165

Answers (2)

Steve
Steve

Reputation: 5853

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

Upvotes: 0

Ivo Wetzel
Ivo Wetzel

Reputation: 46745

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

Upvotes: 3

Related Questions