Skip to content

Commit 8b9fd13

Browse files
committed
feat: add --full option
1 parent 4e46fcf commit 8b9fd13

File tree

1 file changed

+40
-17
lines changed

1 file changed

+40
-17
lines changed

src/html2svg.ts

+40-17
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,28 @@ program
1010
.showHelpAfterError()
1111
.showSuggestionAfterError()
1212
.argument('<url>', 'URL to the web page to render')
13+
.option(
14+
'-f, --full',
15+
'capture the entire page',
16+
)
1317
.option(
1418
'-w, --width <width>',
1519
'set the viewport width in pixels',
16-
'1920',
20+
validateNumber,
21+
1920,
1722
)
1823
.option(
1924
'-h, --height <height>',
20-
"set the viewport height in pixels (this should not affect the export height)",
21-
'1080',
25+
'set the viewport height in pixels',
26+
validateNumber,
27+
1080,
2228
)
2329
.option(
2430
'-f, --format <format>',
2531
'set the output format, should one of these values: svg, pdf',
2632
'svg',
2733
)
28-
.action(async (url, { format, width, height }) => {
34+
.action(async (url, { full, width, height, format }) => {
2935
const mode = getMode(format)
3036

3137
app.dock?.hide()
@@ -36,9 +42,9 @@ program
3642
await app.whenReady()
3743

3844
const page = new BrowserWindow({
45+
width,
46+
height,
3947
show: false,
40-
width: parseInt(width, 10),
41-
height: parseInt(height, 10),
4248
webPreferences: { sandbox: false },
4349
})
4450

@@ -64,29 +70,36 @@ program
6470
.catch(reject),
6571
)
6672

67-
const result = await page.webContents.executeJavaScript(
73+
await page.webContents.executeJavaScript(
6874
`
6975
new Promise(resolve => {
7076
const style = document.createElement('style')
71-
const policy = trustedTypes.createPolicy('html2svg/scrollbar-css', { createHTML: x => x })
7277
73-
style.innerHTML = policy.createHTML(\`
74-
*::-webkit-scrollbar, *::-webkit-scrollbar-track, *::-webkit-scrollbar-thumb {
75-
display: none;
76-
}
77-
\`)
78+
style.innerHTML = trustedTypes
79+
.createPolicy('html2svg/scrollbar-css', { createHTML: x => x })
80+
.createHTML(\`
81+
*::-webkit-scrollbar,
82+
*::-webkit-scrollbar-track,
83+
*::-webkit-scrollbar-thumb {
84+
display: none;
85+
}
86+
\`)
7887
7988
document.head.appendChild(style)
8089
scrollTo({ top: document.body.scrollHeight })
8190
8291
requestAnimationFrame(() => {
8392
scrollTo({ top: 0 })
8493
85-
setTimeout(() => {
86-
requestAnimationFrame(resolve)
87-
}, 1000)
94+
requestAnimationFrame(resolve)
8895
})
89-
}).then(() => getPageContentsAsSVG(${mode}, document.title))
96+
}).then(() =>
97+
getPageContentsAsSVG(
98+
${full ? 0 : height} * devicePixelRatio,
99+
${mode},
100+
document.title,
101+
)
102+
)
90103
`,
91104
)
92105
} finally {
@@ -112,3 +125,13 @@ function getMode(format: string) {
112125
throw new Error(`Unsupported output format: ${format}`)
113126
}
114127
}
128+
129+
function validateNumber(string: string) {
130+
const number = parseInt(string, 10)
131+
132+
if(Number.isNaN(number)) {
133+
throw new Error(`Invalid number value: ${string}`)
134+
}
135+
136+
return number
137+
}

0 commit comments

Comments
 (0)