Skip to content

Commit

Permalink
Add screenshot selector. Feature related to #125 request
Browse files Browse the repository at this point in the history
  • Loading branch information
nkimadusanka committed May 8, 2020
1 parent d921820 commit 4563773
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.js",
"env": {
"NODE_ENV": "development",
"PORT": "9000",
"ALLOW_HTTP": "true",
}
}
]
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The only required parameter is `url`.
Parameter | Type | Default | Description
----------|------|---------|------------
url | string | - | URL to render as PDF. (required)
output | string | pdf | Specify the output format. Possible values: `pdf` or `screenshot`.
output | string | pdf | Specify the output format. Possible values: `pdf` , `screenshot` or `html`.
emulateScreenMedia | boolean | `true` | Emulates `@media screen` when rendering the PDF.
ignoreHttpsErrors | boolean | `false` | Ignores possible HTTPS errors when navigating to a page.
scrollPage | boolean | `false` | Scroll page down before rendering to trigger lazy loading elements.
Expand Down
12 changes: 9 additions & 3 deletions src/core/render-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,14 @@ async function render(_opts = {}) {
if (clipContainsSomething) {
screenshotOpts.clip = opts.screenshot.clip;
}

data = await page.screenshot(screenshotOpts);
if(_.isNil(opts.screenshot.selector)) {
data = await page.screenshot(screenshotOpts);
}else {
const selElement = await page.$(opts.screenshot.selector);
if (!_.isNull(selElement)) {
data = await selElement.screenshot();
}
}
}
} catch (err) {
logger.error(`Error when rendering page: ${err}`);
Expand All @@ -175,7 +181,7 @@ async function render(_opts = {}) {
await browser.close();
}
}

return data;
}

Expand Down
1 change: 1 addition & 0 deletions src/http/render-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function getOptsFromQuery(query) {
width: query['screenshot.clip.width'],
height: query['screenshot.clip.height'],
},
selector: query['screenshot.selector'],
omitBackground: query['screenshot.omitBackground'],
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/util/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const sharedQuerySchema = Joi.object({
'screenshot.clip.y': Joi.number(),
'screenshot.clip.width': Joi.number(),
'screenshot.clip.height': Joi.number(),
'screenshot.selector': Joi.string().regex(/(#|\.).*/),
'screenshot.omitBackground': Joi.boolean(),
});

Expand Down Expand Up @@ -123,6 +124,7 @@ const renderBodyObject = Joi.object({
width: Joi.number(),
height: Joi.number(),
},
selector: Joi.string().regex(/(#|\.).*/),
omitBackground: Joi.boolean(),
}),
failEarly: Joi.string(),
Expand Down

0 comments on commit 4563773

Please sign in to comment.