From ab2771c562fdd4e125d8403bf38b9b25ef968522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Cox?= Date: Sun, 5 May 2013 13:02:37 +0200 Subject: [PATCH 1/3] Modified code base so screenshots can be put in specific files, rather than auto generated. --- index.js | 23 +++++++++++++++-------- test/capture.js | 9 ++++++++- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 88c4ef5..8768256 100644 --- a/index.js +++ b/index.js @@ -55,16 +55,23 @@ function capture(urls, options, callback) { var urlParts = urlUtil.parse(url, true), filename = urlParts.pathname, - auth = urlParts.auth; + auth = urlParts.auth, + filePath; + + // The outPath is a file, so don't generate the URLs automatically + if ([".pdf", ".jpg", ".png", ".gif"].indexOf(outPath.substr(-4)) !== -1) { + filePath = outPath; + } else { + if (S(filename).endsWith("/")) filename += "index"; // Append - if (S(filename).endsWith("/")) filename += "index"; // Append + filePath = path.resolve( + process.cwd(), + outPath, + S(urlParts.hostname).replaceAll("\\.", "-").s, + "./" + filename + "." + format); + } - var filePath = path.resolve( - process.cwd(), - outPath, - S(urlParts.hostname).replaceAll("\\.", "-").s, - "./" + filename + "." + format), - args = [captureScript, url, filePath, '--username', username, + var args = [captureScript, url, filePath, '--username', username, '--password', password, '--paper-orientation', paperOrientation, '--paper-margin', paperMargin, '--paper-format', paperFormat, '--viewport-width', viewportWidth, '--viewport-height', viewportHeight]; diff --git a/test/capture.js b/test/capture.js index eaed3f8..fa3b00a 100644 --- a/test/capture.js +++ b/test/capture.js @@ -33,6 +33,13 @@ describe('capture', function() { }); }); + it('should capture screenshot of 1 page to a specific file', function(done) { + capture(['http://127.0.0.1:8899/'], { out: './test/tmp/screenshot.png' }, function(err) { + assert.ok(fs.statSync(path.join(__dirname, 'tmp/screenshot.png')).isFile()); + done(); + }); + }); + }); after(function(done) { @@ -40,7 +47,7 @@ describe('capture', function() { wrench.rmdirSyncRecursive(temp); done(); }); - + }); From 2497aa220e88b80bb34f66c1ba66d71fb2c9df65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Cox?= Date: Sun, 5 May 2013 13:06:11 +0200 Subject: [PATCH 2/3] A URL can now be specified as a string, rather than an array. --- index.js | 6 +++++- test/capture.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 8768256..2010522 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,10 @@ var MAX_PHANTOMJS_SPAWNS = 10, function capture(urls, options, callback) { + if (typeof urls === 'string') { + urls = new Array(urls); + } + if (typeof options === 'function') { callback = options; options = {}; @@ -57,7 +61,7 @@ function capture(urls, options, callback) { filename = urlParts.pathname, auth = urlParts.auth, filePath; - + // The outPath is a file, so don't generate the URLs automatically if ([".pdf", ".jpg", ".png", ".gif"].indexOf(outPath.substr(-4)) !== -1) { filePath = outPath; diff --git a/test/capture.js b/test/capture.js index fa3b00a..c0b4274 100644 --- a/test/capture.js +++ b/test/capture.js @@ -40,6 +40,12 @@ describe('capture', function() { }); }); + it('should capture screenshot of 1 page specified as a string', function(done) { + capture('http://127.0.0.1:8899/', { out: './test/tmp' }, function(err) { + assert.ok(fs.statSync(path.join(__dirname, 'tmp/127-0-0-1/index.png')).isFile()); + done(); + }); + }); }); after(function(done) { From 9fdb3347074c621f38daf8943950a10b800354c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Cox?= Date: Tue, 11 Jun 2013 12:29:47 +0200 Subject: [PATCH 3/3] Added zoomFactor to fix some weird Windows bug. --- phantomjs/capture.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/phantomjs/capture.js b/phantomjs/capture.js index 03daf02..c48b21a 100644 --- a/phantomjs/capture.js +++ b/phantomjs/capture.js @@ -32,6 +32,10 @@ output = options.output; page.settings.userName = options.username || ''; page.settings.password = options.password || ''; +// This fixes some weird bug on Windows where a large border is +// displayed along the page. +page.zoomFactor = 1.2; + page.viewportSize = { width: options.viewportWidth || 1024, height: options.viewportHeight || 768,