Proo1931
Proo1931

Reputation: 103

JSdom canvas - how to take screenshot of a page?

VERSIONS:

node v8.11.4
"express": "^4.16.3",
"jsdom": "^11.3.0",
"request": "^2.88.0",

CODE:

var express = require('express');
var app = express();
var router = express.Router();
var request = require('request');
var jsdom = require("jsdom/lib/old-api.js");

var page = request('https://www.ggole.com', function(err, resp, content) {
    jsdom.env(
    {
        html: content,
        done: function (err, window)
        {
            if (err) {console.log(err);}
            var document = window.document;
            document.body.appendChild(canvas);
            var data = canvas.toDataURL();
            window.close();
        }
    });
}

PROBLEM:

I would like to take a screenshot of google page. Is this possible with JSdom? Content of the page is in canvas tag so how to make a picture from it and send it to the user/edit it?

var data = canvas.toDataURL(); is not working as expected. Any help is appreciated.

Upvotes: 1

Views: 1716

Answers (1)

Roman Simuta
Roman Simuta

Reputation: 96

may be it's late :) jsdom uses nodejs canvas as the external dependency their documentation say

Canvas support jsdom includes support for using the canvas package to extend any elements with the canvas API. To make this work, you need to include canvas as a dependency in your project, as a peer of jsdom. If jsdom can find the canvas package, it will use it, but if it's not present, then elements will behave like s. Since jsdom v13, version 2.x of canvas is required; version 1.x is no longer supported

and you can find examples how to use canvas here https://www.npmjs.com/package/canvas

Upvotes: 1

Related Questions