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

More flexibility for single URL captures #2

Open
wants to merge 3 commits 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
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -55,16 +59,23 @@ function capture(urls, options, callback) {

var urlParts = urlUtil.parse(url, true),
filename = urlParts.pathname,
auth = urlParts.auth;
auth = urlParts.auth,
filePath;

if (S(filename).endsWith("/")) filename += "index"; // Append
// 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

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];
Expand Down
4 changes: 4 additions & 0 deletions phantomjs/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion test/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,27 @@ 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();
});
});

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) {
var temp = path.join(__dirname, './tmp');
wrench.rmdirSyncRecursive(temp);
done();
});

});