single-file-cli 2.0.60 → 2.0.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build.sh +1 -1
- package/deno.json +1 -1
- package/lib/cdp-client.js +63 -10
- package/lib/single-file-bundle.js +1 -1
- package/lib/version.js +1 -1
- package/options.js +2 -0
- package/package.json +1 -1
package/build.sh
CHANGED
package/deno.json
CHANGED
package/lib/cdp-client.js
CHANGED
|
@@ -33,7 +33,9 @@ const MINIMIZED_WINDOW_STATE = "minimized";
|
|
|
33
33
|
const SINGLE_FILE_WORLD_NAME = "singlefile";
|
|
34
34
|
const EMPTY_PAGE_URL = "about:blank";
|
|
35
35
|
const CAPTURE_SCREENSHOT_FUNCTION_NAME = "captureScreenshot";
|
|
36
|
+
const CAPTURE_PDF_FUNCTION_NAME = "capturePdf";
|
|
36
37
|
const SET_SCREENSHOT_FUNCTION_NAME = "setScreenshot";
|
|
38
|
+
const SET_PDF_FUNCTION_NAME = "setPdf";
|
|
37
39
|
const SET_PAGE_DATA_FUNCTION_NAME = "setPageData";
|
|
38
40
|
const REDIRECT_STATUS_CODES = [301, 302, 303, 307, 308];
|
|
39
41
|
|
|
@@ -51,7 +53,7 @@ async function getPageData(options) {
|
|
|
51
53
|
let targetInfo;
|
|
52
54
|
try {
|
|
53
55
|
targetInfo = await CDP.createTarget(EMPTY_PAGE_URL);
|
|
54
|
-
const { Browser, Security, Page, Emulation, Fetch, Network, Runtime, Debugger } = new CDP(targetInfo);
|
|
56
|
+
const { Browser, Security, Page, Emulation, Fetch, Network, Runtime, Debugger, IO } = new CDP(targetInfo);
|
|
55
57
|
if (options.browserStartMinimized) {
|
|
56
58
|
const { windowId, bounds } = await Browser.getWindowForTarget({ targetId: targetInfo.id });
|
|
57
59
|
if (bounds.windowState !== MINIMIZED_WINDOW_STATE) {
|
|
@@ -203,14 +205,44 @@ async function getPageData(options) {
|
|
|
203
205
|
});
|
|
204
206
|
if (options.embedScreenshot && options.compressContent) {
|
|
205
207
|
await Runtime.addBinding({ name: CAPTURE_SCREENSHOT_FUNCTION_NAME, executionContextId: contextId });
|
|
206
|
-
Runtime.addEventListener("bindingCalled", ({ params }) => {
|
|
208
|
+
Runtime.addEventListener("bindingCalled", async ({ params }) => {
|
|
207
209
|
if (params.name === CAPTURE_SCREENSHOT_FUNCTION_NAME) {
|
|
208
|
-
|
|
210
|
+
try {
|
|
211
|
+
await onCaptureScreenshot({ Page, Runtime }, contextId);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
await Runtime.evaluate({
|
|
214
|
+
expression: `globalThis.${SET_SCREENSHOT_FUNCTION_NAME}(${JSON.stringify([])})`,
|
|
215
|
+
contextId
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
if (options.embedPdf) {
|
|
222
|
+
await Runtime.addBinding({ name: CAPTURE_PDF_FUNCTION_NAME, executionContextId: contextId });
|
|
223
|
+
Runtime.addEventListener("bindingCalled", async ({ params }) => {
|
|
224
|
+
if (params.name === CAPTURE_PDF_FUNCTION_NAME) {
|
|
225
|
+
let pdfOptions;
|
|
226
|
+
if (options.embedPdfOptions) {
|
|
227
|
+
try {
|
|
228
|
+
pdfOptions = JSON.parse(options.embedPdfOptions);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
// ignored
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
await onCapturePdf({ Page, Runtime }, contextId, pdfOptions);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
await Runtime.evaluate({
|
|
237
|
+
expression: `globalThis.${SET_PDF_FUNCTION_NAME}ƒƒ(${JSON.stringify([])})`,
|
|
238
|
+
contextId
|
|
239
|
+
});
|
|
240
|
+
}
|
|
209
241
|
}
|
|
210
242
|
});
|
|
211
243
|
}
|
|
212
244
|
const { result } = await Runtime.evaluate({
|
|
213
|
-
expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME])})`,
|
|
245
|
+
expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PDF_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME])})`,
|
|
214
246
|
awaitPromise: true,
|
|
215
247
|
returnByValue: true,
|
|
216
248
|
contextId
|
|
@@ -248,15 +280,36 @@ async function onCaptureScreenshot({ Page, Runtime }, contextId) {
|
|
|
248
280
|
});
|
|
249
281
|
}
|
|
250
282
|
|
|
251
|
-
function
|
|
252
|
-
|
|
253
|
-
|
|
283
|
+
async function onCapturePdf({ Page, Runtime }, contextId, options = {}) {
|
|
284
|
+
const { data } = await Page.printToPDF(options);
|
|
285
|
+
const pdfData = Array.from(new Uint8Array(await (await fetch("data:application/pdf;base64," + data)).arrayBuffer()));
|
|
286
|
+
await Runtime.evaluate({
|
|
287
|
+
expression: `globalThis.${SET_PDF_FUNCTION_NAME}(${JSON.stringify(pdfData)})`,
|
|
288
|
+
contextId
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function getPageDataScriptSource(options, [SET_SCREENSHOT_FUNCTION_NAME, SET_PDF_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME]) {
|
|
293
|
+
if ((options.embedScreenshot && options.compressContent) || options.embedPdf) {
|
|
294
|
+
let screenshot, pdf;
|
|
295
|
+
if (options.embedScreenshot && options.compressContent) {
|
|
296
|
+
screenshot = new Promise(resolve => globalThis[SET_SCREENSHOT_FUNCTION_NAME] = resolve);
|
|
297
|
+
}
|
|
298
|
+
if (options.embedPdf) {
|
|
299
|
+
pdf = new Promise(resolve => globalThis[SET_PDF_FUNCTION_NAME] = resolve);
|
|
300
|
+
}
|
|
254
301
|
let pendingCapture;
|
|
255
302
|
options.onprogress = async event => {
|
|
256
|
-
if (event.type == event.RESOURCES_INITIALIZING &&
|
|
303
|
+
if (event.type == event.RESOURCES_INITIALIZING && window == top && !pendingCapture) {
|
|
257
304
|
pendingCapture = true;
|
|
258
|
-
globalThis.captureScreenshot
|
|
259
|
-
|
|
305
|
+
if (globalThis.captureScreenshot) {
|
|
306
|
+
globalThis.captureScreenshot("");
|
|
307
|
+
options.embeddedImage = await screenshot;
|
|
308
|
+
}
|
|
309
|
+
if (globalThis.capturePdf) {
|
|
310
|
+
globalThis.capturePdf("");
|
|
311
|
+
options.embeddedPdf = await pdf;
|
|
312
|
+
}
|
|
260
313
|
}
|
|
261
314
|
};
|
|
262
315
|
}
|