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

#47 Enhancement to take screenshot only successful status code (2xx, 3xx) #96

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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ Few rules:
<dt>cookies</dt>
<dd>Configure <a href="http://phantomjs.org/api/phantom/property/cookies.html">cookies</a> that will be contained in request. HTTP message body is the easiest way for sending cookies to Manet (ex: using JSON format).</dd>

<dt>onlySuccessfulStatusCode</dt>
<dd>Take screenshot only when http status code is below 400 (a.k.a. 2xx, 3xx). (default is `false`)</dd>

</dl>


Expand Down
4 changes: 2 additions & 2 deletions public/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
'user', 'password',
'callback', 'headers', 'clipRect',
'force', 'selector','selectorCrop','selectorCropPadding',
'engine'
'engine', 'onlySuccessfulStatusCode'
];

function cleanBoolValue(name, value) {
return ((value && (name === 'js' || name === 'images')) ||
(!value && (name === 'force' || name === 'selectorCrop'))) ? null : value;
(!value && (name === 'force' || name === 'selectorCrop' || name === 'onlySuccessfulStatusCode'))) ? null : value;
}

function readOptions() {
Expand Down
5 changes: 5 additions & 0 deletions public/usage.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ <h2>Website screenshot service</h2>
<input id="force" name="force" type="checkbox">
</div>

<div class="pure-control-group">
<label for="onlySuccessfulStatusCode">Only Successful Status Code</label>
<input id="onlySuccessfulStatusCode" name="onlySuccessfulStatusCode" type="checkbox">
</div>

<div class="pure-control-group">
<label for="selector">Wait for DOM element</label>
<input id="selector" name="selector" type="text" maxlength="1024" placeholder="CSS selector">
Expand Down
49 changes: 32 additions & 17 deletions src/capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const _ = require('lodash'),
fs = require('fs-extra'),
logger = require('winston'),
path = require('path'),
request = require('request'),
squirrel = require('squirrel'),
crypto = require('crypto'),
utils = require('./utils'),
Expand Down Expand Up @@ -87,24 +88,38 @@ function minimizeImage(src, dest, cb) {
/* Screenshot capturing runner */

function runCapturingProcess(options, config, outputFile, base64, onFinish) {
const scriptFile = utils.filePath(SCRIPT_FILE),
command = cliCommand(config).split(/[ ]+/),
cmd = _.union(command, [scriptFile, base64, outputFile]),
opts = {
timeout: config.timeout
};

logger.debug(
'Options for script: %s, base64: %s, command: %s',
JSON.stringify(options), base64, JSON.stringify(cmd)
);

utils.execProcess(cmd, opts, (error) => {
if (config.compress) {
minimizeImage(outputFile, config.storage, () => onFinish(error));
} else {
request({
uri: options.url,
method: 'HEAD'
}, function (error, response) {
if (error) {
onFinish(error);
}

if (!!options.onlySuccessfulStatusCode && response.statusCode >= 400) {
logger.error(`URL Status code is ${response.statusCode}`);
return onFinish(new Error(`URL Status code is ${response.statusCode}`));
}

const scriptFile = utils.filePath(SCRIPT_FILE),
command = cliCommand(config).split(/[ ]+/),
cmd = _.union(command, [scriptFile, base64, outputFile]),
opts = {
timeout: config.timeout
};

logger.debug(
'Options for script: %s, base64: %s, command: %s',
JSON.stringify(options), base64, JSON.stringify(cmd)
);

utils.execProcess(cmd, opts, (error) => {
if (config.compress) {
minimizeImage(outputFile, config.storage, () => onFinish(error));
} else {
onFinish(error);
}
});
});
}

Expand All @@ -130,7 +145,7 @@ function screenshot(options, config, onFinish) {

logger.info('Capture site screenshot: "%s"', options.url);

if (options.force || !conf.cache) {
if (options.force || !conf.cache || options.onlySuccessfulStatusCode) {
retrieveImageFromSite();
} else {
fs.exists(file, (exists) =>
Expand Down
1 change: 1 addition & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const _ = require('lodash'),
function createSchema() {
return joi.object().keys({
force: joi.boolean(),
onlySuccessfulStatusCode: joi.boolean(),
url: joi.string().trim().required(),
agent: joi.string().trim(),
headers: joi.string().trim(),
Expand Down