Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can specify a pageClipRectFn to dynamically grab page.clipRect() from page data #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/webshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ function spawnPhantom(site, path, streaming, options, cb) {
filteredOptions.site = site;
filteredOptions.path = path;
filteredOptions.streaming = streaming;
if (options.pageClipRectFn) {
filteredOptions.pageClipRectFn = options.pageClipRectFn.toString();
}

var phantomArgs = [phantomScript, JSON.stringify(filteredOptions)];

Expand Down
5 changes: 5 additions & 0 deletions lib/webshot.phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ var _takeScreenshot = function(status) {
throw new Error("Unable to fetch bounds for element " + selector);
}
}, options.captureSelector, options.zoomFactor);
} else if (options.pageClipRectFn) {
// A hack to get the script into the page
page.clipRect = page.evaluate(function(fn) {
return eval('('+fn+')')();
}, options.pageClipRectFn);
} else {

//Set the rectangle of the page to render
Expand Down
34 changes: 34 additions & 0 deletions test/options/pageClipRectFn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var webshot = require('../../lib/webshot')
, should = require('should')
, fs = require('fs')
, im = require('imagemagick')
, helper = require('../helper')
, pngOutput = helper.pngOutput
, fixtures = helper.fixtures;

describe('pageClipRectFn', function() {
this.timeout(20000);

it('screenshots the page given the rect returned from pageClipRectFn', function(done) {
var fixture = fixtures[2];
// width and height here aren't important, but they're different from fixture.width and fixture.height
var options = {
pageClipRectFn: function () {
return {top: 0, left: 0, width: 500, height: 600};
}
};

webshot(fixture.path, pngOutput, options, function(err) {
if (err) return done(err);

im.identify(pngOutput, function(err, features) {
if (err) return done(err);

features.width.should.equal(500);
features.height.should.equal(600);
done();
});
});
});
});