Skip to content

Commit fabb29a

Browse files
Merge pull request #128 from tomasc/feature/screenshot_selector
Feature/screenshot selector
2 parents 51723bf + d177c4a commit fabb29a

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

.vscode/launch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"program": "${workspaceFolder}/src/index.js",
12+
"env": {
13+
"NODE_ENV": "development",
14+
"PORT": "9000",
15+
"ALLOW_HTTP": "true",
16+
}
17+
}
18+
]
19+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ The only required parameter is `url`.
161161
Parameter | Type | Default | Description
162162
----------|------|---------|------------
163163
url | string | - | URL to render as PDF. (required)
164-
output | string | pdf | Specify the output format. Possible values: `pdf` or `screenshot`.
164+
output | string | pdf | Specify the output format. Possible values: `pdf` , `screenshot` or `html`.
165165
emulateScreenMedia | boolean | `true` | Emulates `@media screen` when rendering the PDF.
166166
enableGPU | boolean | `false` | When set, enables chrome GPU. For windows user, this will always return false. See https://developers.google.com/web/updates/2017/04/headless-chrome
167167
ignoreHttpsErrors | boolean | `false` | Ignores possible HTTPS errors when navigating to a page.

src/core/render-core.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,14 @@ async function render(_opts = {}) {
165165
if (clipContainsSomething) {
166166
screenshotOpts.clip = opts.screenshot.clip;
167167
}
168-
169-
data = await page.screenshot(screenshotOpts);
168+
if (_.isNil(opts.screenshot.selector)) {
169+
data = await page.screenshot(screenshotOpts);
170+
} else {
171+
const selElement = await page.$(opts.screenshot.selector);
172+
if (!_.isNull(selElement)) {
173+
data = await selElement.screenshot();
174+
}
175+
}
170176
}
171177
} catch (err) {
172178
logger.error(`Error when rendering page: ${err}`);
@@ -178,7 +184,7 @@ async function render(_opts = {}) {
178184
await browser.close();
179185
}
180186
}
181-
187+
182188
return data;
183189
}
184190

src/http/render-http.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ function getOptsFromQuery(query) {
186186
width: query['screenshot.clip.width'],
187187
height: query['screenshot.clip.height'],
188188
},
189+
selector: query['screenshot.selector'],
189190
omitBackground: query['screenshot.omitBackground'],
190191
},
191192
};

src/util/validation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const sharedQuerySchema = Joi.object({
6262
'screenshot.clip.y': Joi.number(),
6363
'screenshot.clip.width': Joi.number(),
6464
'screenshot.clip.height': Joi.number(),
65+
'screenshot.selector': Joi.string().regex(/(#|\.).*/),
6566
'screenshot.omitBackground': Joi.boolean(),
6667
});
6768

@@ -124,6 +125,7 @@ const renderBodyObject = Joi.object({
124125
width: Joi.number(),
125126
height: Joi.number(),
126127
},
128+
selector: Joi.string().regex(/(#|\.).*/),
127129
omitBackground: Joi.boolean(),
128130
}),
129131
failEarly: Joi.string(),

0 commit comments

Comments
 (0)