forked from sindresorhus/capture-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
303 lines (224 loc) · 8.24 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/// <reference lib="dom"/>
/// <reference types="puppeteer"/>
import {SetCookie, LaunchOptions, Page, Browser} from 'puppeteer';
declare namespace captureWebsite {
interface Authentication {
readonly username: string;
readonly password?: string;
}
type BeforeScreenshot = (page: Page, browser: Browser) => void;
interface ScrollToElementOptions {
/**
A [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
*/
readonly element: string;
/**
Offset origin.
*/
readonly offsetFrom: 'top' | 'right' | 'bottom' | 'left';
/**
Offset in pixels.
*/
readonly offset: number;
}
interface Options {
/**
Input type.
@default url
*/
readonly inputType?: 'url' | 'html';
/**
Page width.
@default 1280
*/
readonly width?: number;
/**
Page height.
@default 800
*/
readonly height?: number;
/**
Image type.
@default png
*/
readonly type?: 'png' | 'jpeg';
/**
Image quality. Only for {type: 'jpeg'}.
@default 1
*/
readonly quality?: number;
/**
Scale the webpage `n` times.
The default is what you would get if you captured a normal screenshot on a computer with a retina (High DPI) screen.
@default 2
*/
readonly scaleFactor?: number;
/**
Make it look like the screenshot was taken on the specified device.
This overrides the `width`, `height`, `scaleFactor`, and `userAgent` options.
*/
readonly emulateDevice?: string;
/**
Capture the full scrollable page, not just the viewport.
@default false
*/
readonly fullPage?: boolean;
/**
Include the default white background.
Disabling this lets you capture screenshots with transparency.
@default true
*/
readonly defaultBackground?: boolean;
/**
The number of seconds before giving up trying to load the page.
Specify `0` to disable the timeout.
@default 60
*/
readonly timeout?: number;
/**
The number of seconds to wait after the page finished loading before capturing the screenshot.
This can be useful if you know the page has animations that you like it to finish before capturing the screenshot.
@default 0
*/
readonly delay?: number;
/**
Wait for a DOM element matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors) to appear in the page and to be visible before capturing the screenshot. It times out after `options.timeout` seconds.
*/
readonly waitForElement?: string;
/**
Capture the DOM element matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors). It will wait for the element to appear in the page and to be visible. It times out after `options.timeout` seconds
*/
readonly element?: string;
/**
Hide DOM elements matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
Can be useful for cleaning up the page.
This sets [`visibility: hidden`](https://stackoverflow.com/a/133064/64949) on the matched elements.
*/
readonly hideElements?: string[];
/**
Remove DOM elements matching the given [CSS selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
This sets [`display: none`](https://stackoverflow.com/a/133064/64949) on the matched elements, so it could potentially break the website layout.
*/
readonly removeElements?: string[];
/**
Click the DOM element matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
*/
readonly clickElement?: string;
/**
Scroll to the DOM element matching the given [CSS selector](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).
*/
readonly scrollToElement?: string | captureWebsite.ScrollToElementOptions
/**
Disable CSS [animations](https://developer.mozilla.org/en-US/docs/Web/CSS/animation) and [transitions](https://developer.mozilla.org/en-US/docs/Web/CSS/transition).
@default false
*/
readonly disableAnimations?: boolean;
/**
Whether JavaScript on the website should be executed.
This does not affect the `scripts` and `modules` options.
@default true
*/
readonly isJavaScriptEnabled?: boolean;
/**
Inject [JavaScript modules](https://developers.google.com/web/fundamentals/primers/modules) into the page.
Accepts an array of inline code, absolute URLs, and local file paths (must have a .js extension).
*/
readonly modules?: string[];
/**
Same as the `modules` option, but instead injects the code as [`<script>` instead of `<script type="module">`](https://developers.google.com/web/fundamentals/primers/modules). Prefer the `modules` option whenever possible.
*/
readonly scripts?: string[];
/**
Inject CSS styles into the page.
Accepts an array of inline code, absolute URLs, and local file paths (must have a `.css` extension).
*/
readonly styles?: string[];
/**
Set custom [HTTP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers).
@default {}
*/
readonly headers?: Headers;
/**
Set a custom [user agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent).
*/
readonly userAgent?: string;
/**
Set cookies in [browser string format](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies) or [object format](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagesetcookiecookies).
Tip: Go to the website you want a cookie for and [copy-paste it from DevTools](https://stackoverflow.com/a/24961735/64949).
*/
readonly cookies?: (string | SetCookie)[];
/**
Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
*/
readonly authentication?: Authentication;
/**
The specified function is called right before the screenshot is captured. It gives you a lot of power to do custom stuff. The function can be async.
Note: Make sure to not call `page.close()` or `browser.close()`.
*/
readonly beforeScreenshot?: BeforeScreenshot;
/**
Show the browser window so you can see what it's doing, redirect page console output to the terminal, and slow down each Puppeteer operation.
Note: This overrides `launchOptions` with `{headless: false, slowMo: 100}`.
@default false
*/
readonly debug?: boolean;
/**
Emulate preference of dark color scheme ([`prefers-color-scheme`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme)).
@default false
*/
readonly darkMode?: boolean;
/**
Options passed to [`puppeteer.launch()`](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions).
Note: Some of the launch options are overridden by the `debug` option.
@default {}
*/
readonly launchOptions?: LaunchOptions;
}
interface FileOptions extends Options {
/**
Overwrite the destination file if it exists instead of throwing an error.
@default false
*/
readonly overwrite?: boolean;
}
}
declare const captureWebsite: {
/**
Devices supported by the `emulateDevice` option.
*/
readonly devices: string[];
/**
Capture a screenshot of the given `input` and save it to the given `outputFilePath`.
@param input - The URL, file URL, data URL, local file path to the website, or HTML.
@param outputFilePath - The path to write the screenshot.
@returns A promise that resolves when the screenshot is written to the given file path.
@example
```
import captureWebsite = require('capture-website');
(async () => {
await captureWebsite.file('https://sindresorhus.com', 'screenshot-url.png');
await captureWebsite.file('<h1>Awesome!</h1>', 'screenshot-html.png', {
inputType: 'html'
});
})();
```
*/
file(
input: string,
outputFilePath: string,
options?: captureWebsite.FileOptions
): Promise<void>;
/**
Capture a screenshot of the given `input`.
@param input - The URL, file URL, data URL, local file path to the website, or HTML.
@returns The screenshot as binary.
*/
buffer(input: string, options?: captureWebsite.Options): Promise<Buffer>;
/**
Capture a screenshot of the given `input`.
@param input - The URL, file URL, data URL, local file path to the website, or HTML.
@returns The screenshot as [Base64](https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding).
*/
base64(input: string, options?: captureWebsite.Options): Promise<string>;
};
export = captureWebsite;