Skip to content

Instantly share code, notes, and snippets.

@arisetyo
Last active February 26, 2019 07:36
Show Gist options
  • Save arisetyo/ccbfadba7a3248ea5cea to your computer and use it in GitHub Desktop.
Save arisetyo/ccbfadba7a3248ea5cea to your computer and use it in GitHub Desktop.
Webpage Screen Capture Using NodeJS and PhantomJS
var page = require('webpage').create();
var url = 'http://phantomjs.org/quick-start.html';
//SPECIFY VIEWPORT SIZE OF THE SCREENCAPTURE
page.viewportSize = {
width: 1280,
height: 900
};
//SPECIFY CLIP RECTANGLE SIZE OF THE SCREENCAPTURE
page.clipRect = {
top: 0,
left: 0,
width: 1280,
height: 1280
};
//CAPTURE CONSOLE MESSAGES
page.onConsoleMessage = function(msg) {
console.log("console message: " + msg);
};
page.open(url, function(status) {
page.evaluate(function() {
console.log(document.title);
});
page.render('screenshot.png');
phantom.exit();
});
var page = require('webpage').create();
var url = 'http://phantomjs.org/quick-start.html';
page.onConsoleMessage = function(msg) {
console.log("console message: " + msg);
};
page.open(url, function(status) {
page.evaluate(function() {
console.log(document.title);
});
phantom.exit();
});
var phantom = require('phantom');
var pageUrl = "https://medium.com/i-m-h-o/sick-of-sfx-89768a5ac3a7";
phantom.create("--ignore-ssl-errors=yes", "--ssl-protocol=any", function (ph) {//mMAKE SURE WE CAN RENDER https
ph.createPage(function (page) {
//CREATE PAGE OBJECT
page.set('viewportSize', {width:1280,height:900}, function(){
page.set('clipRect', {top:0,left:0,width:1280,height:900}, function(){
//OPEN PAGE
page.open(pageUrl, function(status) {
//WAIT 15 SECS FOR WEBPAGE TO BE COMPLETELY LOADED
setTimeout(function(){
page.render('screenshot.png', function(finished){
console.log('rendering '+pageUrl+' done');
ph.exit();
});
}, 15000);
});
//END OF: OPEN PAGE
});
});
//END OF: CREATE PAGE OBJECT
});
});
var phantom = require('phantom');
phantom.create(function (ph) {
ph.createPage(function (page) {
page.open("http://blog.arisetyo.com/", function (status) {
console.log("opened page? ", status);
page.evaluate(function () { return document.title; }, function (result) {
console.log('Page title is: ' + result);
ph.exit();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment