27

I am trying to manipulate a remote HTML and return it manipulated. I decided to use JSDOM but cannot figure out how to get the manipulated HTML back. Any ideas?

  jsdom.env({
        url: "http://www.cnn.com",
        scripts: ["http://code.jquery.com/jquery.js"],
        done: function (err, window) {
            var $ = window.$;
            console.log("HN Links");
            var src = $(".ghciTopStoryImage1 img").attr('src','http://lorempixel.com/396/220/');
            var headline = $(".blkbigheader span").html('header');
            var description = $(".blkbigheader").parent().find("p a:eq(0)").html('text');

           // not working....
            content =$(window.document).html();

        }
    });
Guy
  • 12,488
  • 16
  • 79
  • 119
  • 2
    In something similar I used: `window.document.querySelector( 'html' ).outerHTML` to get the whole document. – Sirko Aug 05 '15 at 09:33

2 Answers2

44

From the jsdom readme:

var JSDOM = require("jsdom").JSDOM;

var jsdom = new JSDOM("<!DOCTYPE html>hello");

jsdom.serialize() === "<!DOCTYPE html><html><head></head><body>hello</body></html>";
doc.documentElement.outerHTML === "<html><head></head><body>hello</body></html>";

Adapting your above example would then just be content = jsdom.serialize().

Ninjakannon
  • 3,751
  • 7
  • 53
  • 76
Domenic
  • 110,262
  • 41
  • 219
  • 271
18

sometimes jQuery isn't the answer

content = window.document.documentElement.outerHTML;
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87