single-file-cli 2.0.34 → 2.0.35

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
  #!/bin/sh
2
2
 
3
- deno vendor "npm:single-file-core@1.3.30"
3
+ deno vendor "npm:single-file-core@1.4.0"
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.34",
3
+ "version": "2.0.35",
4
4
  "description": "SingleFile CLI",
5
5
  "nodeModulesDir": true,
6
6
  "exports": {
package/lib/cdp-client.js CHANGED
@@ -32,6 +32,8 @@ const NETWORK_STATES = ["InteractiveTime", "networkIdle", "networkAlmostIdle", "
32
32
  const MINIMIZED_WINDOW_STATE = "minimized";
33
33
  const SINGLE_FILE_WORLD_NAME = "singlefile";
34
34
  const EMPTY_PAGE_URL = "about:blank";
35
+ const CAPTURE_SCREENSHOT_FUNCTION_NAME = "captureScreenshot";
36
+ const SET_SCREENSHOT_FUNCTION_NAME = "setScreenshot";
35
37
 
36
38
  export { initialize, getPageData, closeBrowser };
37
39
 
@@ -106,12 +108,15 @@ async function getPageData(options) {
106
108
  setTimeout(() => resolve, options.browserWaitDelay);
107
109
  }
108
110
  if (options.embedScreenshot && options.compressContent) {
109
- const { data } = await Page.captureScreenshot({ format: "png", captureBeyondViewport: true });
110
- const dataURI = "data:image/png;base64," + data;
111
- options.embeddedImage = Array.from(new Uint8Array(await (await fetch(dataURI)).arrayBuffer()));
111
+ await Runtime.addBinding({ name: CAPTURE_SCREENSHOT_FUNCTION_NAME, executionContextId: contextId });
112
+ Runtime.addEventListener("bindingCalled", ({ params }) => {
113
+ if (params.name === CAPTURE_SCREENSHOT_FUNCTION_NAME) {
114
+ onCaptureScreenshot({ Page, Runtime }, contextId);
115
+ }
116
+ });
112
117
  }
113
118
  const { result } = await Runtime.evaluate({
114
- expression: `singlefile.getPageData(${JSON.stringify(options)})`,
119
+ expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify(SET_SCREENSHOT_FUNCTION_NAME)})`,
115
120
  awaitPromise: true,
116
121
  returnByValue: true,
117
122
  contextId
@@ -140,6 +145,31 @@ async function getPageData(options) {
140
145
  }
141
146
  }
142
147
 
148
+ async function onCaptureScreenshot({ Page, Runtime }, contextId) {
149
+ const { data } = await Page.captureScreenshot({ format: "png", captureBeyondViewport: true });
150
+ const dataURI = "data:image/png;base64," + data;
151
+ const screenshotData = Array.from(new Uint8Array(await (await fetch(dataURI)).arrayBuffer()));
152
+ await Runtime.evaluate({
153
+ expression: `globalThis.${SET_SCREENSHOT_FUNCTION_NAME}(${JSON.stringify(screenshotData)})`,
154
+ contextId
155
+ });
156
+ }
157
+
158
+ function getPageDataScriptSource(options, SET_SCREENSHOT_FUNCTION_NAME) {
159
+ if (options.embedScreenshot && options.compressContent) {
160
+ let screenshot = new Promise(resolve => globalThis[SET_SCREENSHOT_FUNCTION_NAME] = resolve);
161
+ let pendingCapture;
162
+ options.onprogress = async event => {
163
+ if (event.type == event.RESOURCES_INITIALIZING && globalThis.captureScreenshot && window == top && !pendingCapture) {
164
+ pendingCapture = true;
165
+ globalThis.captureScreenshot("");
166
+ options.embeddedImage = await screenshot;
167
+ }
168
+ };
169
+ }
170
+ return singlefile.getPageData(options);
171
+ }
172
+
143
173
  async function loadPage({ Page, Runtime }, options) {
144
174
  await Runtime.enable();
145
175
  await Page.enable();