@@ -10,22 +10,28 @@ program
10
10
. showHelpAfterError ( )
11
11
. showSuggestionAfterError ( )
12
12
. argument ( '<url>' , 'URL to the web page to render' )
13
+ . option (
14
+ '-f, --full' ,
15
+ 'capture the entire page' ,
16
+ )
13
17
. option (
14
18
'-w, --width <width>' ,
15
19
'set the viewport width in pixels' ,
16
- '1920' ,
20
+ validateNumber ,
21
+ 1920 ,
17
22
)
18
23
. option (
19
24
'-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 ,
22
28
)
23
29
. option (
24
30
'-f, --format <format>' ,
25
31
'set the output format, should one of these values: svg, pdf' ,
26
32
'svg' ,
27
33
)
28
- . action ( async ( url , { format , width, height } ) => {
34
+ . action ( async ( url , { full , width, height, format } ) => {
29
35
const mode = getMode ( format )
30
36
31
37
app . dock ?. hide ( )
@@ -36,9 +42,9 @@ program
36
42
await app . whenReady ( )
37
43
38
44
const page = new BrowserWindow ( {
45
+ width,
46
+ height,
39
47
show : false ,
40
- width : parseInt ( width , 10 ) ,
41
- height : parseInt ( height , 10 ) ,
42
48
webPreferences : { sandbox : false } ,
43
49
} )
44
50
@@ -64,29 +70,36 @@ program
64
70
. catch ( reject ) ,
65
71
)
66
72
67
- const result = await page . webContents . executeJavaScript (
73
+ await page . webContents . executeJavaScript (
68
74
`
69
75
new Promise(resolve => {
70
76
const style = document.createElement('style')
71
- const policy = trustedTypes.createPolicy('html2svg/scrollbar-css', { createHTML: x => x })
72
77
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
+ \`)
78
87
79
88
document.head.appendChild(style)
80
89
scrollTo({ top: document.body.scrollHeight })
81
90
82
91
requestAnimationFrame(() => {
83
92
scrollTo({ top: 0 })
84
93
85
- setTimeout(() => {
86
- requestAnimationFrame(resolve)
87
- }, 1000)
94
+ requestAnimationFrame(resolve)
88
95
})
89
- }).then(() => getPageContentsAsSVG(${ mode } , document.title))
96
+ }).then(() =>
97
+ getPageContentsAsSVG(
98
+ ${ full ? 0 : height } * devicePixelRatio,
99
+ ${ mode } ,
100
+ document.title,
101
+ )
102
+ )
90
103
` ,
91
104
)
92
105
} finally {
@@ -112,3 +125,13 @@ function getMode(format: string) {
112
125
throw new Error ( `Unsupported output format: ${ format } ` )
113
126
}
114
127
}
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