single-file-cli 2.0.59 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- deno vendor "npm:single-file-core@1.5.25"
3
+ deno vendor "npm:single-file-core@1.5.29"
4
4
 
5
5
  echo "
6
6
  import { build } from 'npm:esbuild';
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@single-file/single-file-cli",
3
- "version": "2.0.59",
3
+ "version": "2.0.61",
4
4
  "description": "SingleFile CLI",
5
5
  "nodeModulesDir": true,
6
6
  "exports": {
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) {
@@ -89,12 +91,9 @@ async function getPageData(options) {
89
91
  });
90
92
  }
91
93
  if (options.browserMobileEmulation || options.platform || options.acceptLanguage) {
92
- let browserUserAgent;
93
- if (!options.userAgent) {
94
- ({ browserUserAgent } = await Browser.getVersion());
95
- }
94
+ const { userAgent, product } = await Browser.getVersion();
96
95
  const agentOptions = {
97
- userAgent: options.userAgent || (options.browserMobileEmulation ? "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome Mobile Safari/537.36" : browserUserAgent)
96
+ userAgent: options.userAgent || (options.browserMobileEmulation ? "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) " + product + " Mobile Safari/537.36" : userAgent)
98
97
  };
99
98
  if (options.acceptLanguage) {
100
99
  agentOptions.acceptLanguage = options.acceptLanguage;
@@ -206,14 +205,44 @@ async function getPageData(options) {
206
205
  });
207
206
  if (options.embedScreenshot && options.compressContent) {
208
207
  await Runtime.addBinding({ name: CAPTURE_SCREENSHOT_FUNCTION_NAME, executionContextId: contextId });
209
- Runtime.addEventListener("bindingCalled", ({ params }) => {
208
+ Runtime.addEventListener("bindingCalled", async ({ params }) => {
210
209
  if (params.name === CAPTURE_SCREENSHOT_FUNCTION_NAME) {
211
- onCaptureScreenshot({ Page, Runtime }, contextId);
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
+ }
212
241
  }
213
242
  });
214
243
  }
215
244
  const { result } = await Runtime.evaluate({
216
- 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])})`,
217
246
  awaitPromise: true,
218
247
  returnByValue: true,
219
248
  contextId
@@ -251,15 +280,36 @@ async function onCaptureScreenshot({ Page, Runtime }, contextId) {
251
280
  });
252
281
  }
253
282
 
254
- function getPageDataScriptSource(options, [SET_SCREENSHOT_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME]) {
255
- if (options.embedScreenshot && options.compressContent) {
256
- let screenshot = new Promise(resolve => globalThis[SET_SCREENSHOT_FUNCTION_NAME] = resolve);
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
+ }
257
301
  let pendingCapture;
258
302
  options.onprogress = async event => {
259
- if (event.type == event.RESOURCES_INITIALIZING && globalThis.captureScreenshot && window == top && !pendingCapture) {
303
+ if (event.type == event.RESOURCES_INITIALIZING && window == top && !pendingCapture) {
260
304
  pendingCapture = true;
261
- globalThis.captureScreenshot("");
262
- options.embeddedImage = await screenshot;
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
+ }
263
313
  }
264
314
  };
265
315
  }