diff --git a/lib/webshot.js b/lib/webshot.js index 5b8a21c..8f1303a 100644 --- a/lib/webshot.js +++ b/lib/webshot.js @@ -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)]; diff --git a/lib/webshot.phantom.js b/lib/webshot.phantom.js index 7147cdb..3e615b3 100644 --- a/lib/webshot.phantom.js +++ b/lib/webshot.phantom.js @@ -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 diff --git a/test/options/pageClipRectFn.js b/test/options/pageClipRectFn.js new file mode 100644 index 0000000..967d222 --- /dev/null +++ b/test/options/pageClipRectFn.js @@ -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(); + }); + }); + }); +}); +