single-file-cli 2.9.1 → 2.10.0

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
@@ -2,7 +2,7 @@
2
2
 
3
3
  set -e
4
4
 
5
- CORE_PACKAGE="npm:single-file-core@1.5.123"
5
+ CORE_PACKAGE="npm:single-file-core@1.5.126"
6
6
  ESBUILD_PACKAGE="npm:esbuild@0.27.7"
7
7
  WEB_STREAMS_PACKAGE="npm:web-streams-polyfill@4.3.0"
8
8
 
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@single-file/single-file-cli",
3
- "version": "2.9.1",
3
+ "version": "2.10.0",
4
4
  "description": "SingleFile CLI",
5
5
  "exports": {
6
6
  ".": "./single-file-cli-api.js"
@@ -34,6 +34,8 @@ import {
34
34
  FETCH_FUNCTION_NAME,
35
35
  RESOLVE_FETCH_FUNCTION_NAME,
36
36
  REJECT_FETCH_FUNCTION_NAME,
37
+ SCRIPT_READY_PROPERTY_NAME,
38
+ SCRIPT_ERROR_PROPERTY_NAME,
37
39
  getScriptSource,
38
40
  getHookScriptSource,
39
41
  getPageDataScriptSource
@@ -65,8 +67,7 @@ const PRINT_TO_PDF_FUNCTION_NAME = "printToPDF";
65
67
  const SET_SCREENSHOT_FUNCTION_NAME = "setScreenshot";
66
68
  const SET_PDF_FUNCTION_NAME = "setPDF";
67
69
  const SET_PAGE_DATA_FUNCTION_NAME = "setPageData";
68
- const SINGLE_FILE_GLOBAL_DECLARATION = /var singlefile\s*=\s*/;
69
- const SINGLE_FILE_GLOBAL_ASSIGNMENT = "globalThis.singlefile=window.singlefile=";
70
+ const SINGLE_FILE_GLOBAL_ASSIGNMENT = "globalThis.singlefile=window.singlefile=singlefile;";
70
71
  const SERVER_CONNECT_TIMEOUT = 30000;
71
72
  const SESSION_TIMEOUT = 120000;
72
73
  const SERVER_SESSION_PATH = "/session";
@@ -404,7 +405,7 @@ async function setupCookies(pageContext) {
404
405
 
405
406
  async function setupScriptInjection(pageContext) {
406
407
  const { options, context, preloadScripts } = pageContext;
407
- const scriptSource = (await getScriptSource(options)).replace(SINGLE_FILE_GLOBAL_DECLARATION, SINGLE_FILE_GLOBAL_ASSIGNMENT);
408
+ const scriptSource = await getScriptSource(options, SINGLE_FILE_GLOBAL_ASSIGNMENT);
408
409
  const hookScript = await session.send("script.addPreloadScript", {
409
410
  functionDeclaration: "() => {" + getHookScriptSource() + "}",
410
411
  contexts: [context]
@@ -1100,7 +1101,7 @@ function waitForPageReadyState(state, waitUntil, pageContext) {
1100
1101
  }
1101
1102
 
1102
1103
  async function checkSingleFileContext(pageContext) {
1103
- const SINGLE_FILE_DETECTION_TEST = "typeof singlefile !== 'undefined'";
1104
+ const SINGLE_FILE_DETECTION_TEST = "typeof singlefile !== 'undefined' && globalThis." + SCRIPT_READY_PROPERTY_NAME + " === true";
1104
1105
  const NO_VALID_CONTEXT_ERROR_MESSAGE = "No valid SingleFile execution context found";
1105
1106
  const { context } = pageContext;
1106
1107
  logData(["Getting execution context"], pageContext);
@@ -1110,10 +1111,19 @@ async function checkSingleFileContext(pageContext) {
1110
1111
  awaitPromise: false
1111
1112
  });
1112
1113
  if (!result || result.value !== true) {
1113
- throw new Error(NO_VALID_CONTEXT_ERROR_MESSAGE);
1114
+ throw new Error(NO_VALID_CONTEXT_ERROR_MESSAGE + await getInjectionErrorSuffix(context));
1114
1115
  }
1115
1116
  }
1116
1117
 
1118
+ async function getInjectionErrorSuffix(context) {
1119
+ const { result } = await session.send("script.evaluate", {
1120
+ expression: "globalThis." + SCRIPT_ERROR_PROPERTY_NAME,
1121
+ target: getSingleFileTarget(context),
1122
+ awaitPromise: false
1123
+ }).catch(() => ({}));
1124
+ return result && result.value ? ": " + result.value : "";
1125
+ }
1126
+
1117
1127
  async function capturePageData(pageContext) {
1118
1128
  const CAPTURE_TIMEOUT_ERROR_MESSAGE = "Capture timeout";
1119
1129
  const EXCEPTION_TYPE = "exception";
package/lib/cdp-client.js CHANGED
@@ -37,6 +37,8 @@ import {
37
37
  FETCH_FUNCTION_NAME,
38
38
  RESOLVE_FETCH_FUNCTION_NAME,
39
39
  REJECT_FETCH_FUNCTION_NAME,
40
+ SCRIPT_READY_PROPERTY_NAME,
41
+ SCRIPT_ERROR_PROPERTY_NAME,
40
42
  getScriptSource,
41
43
  getHookScriptSource,
42
44
  getPageDataScriptSource
@@ -670,7 +672,7 @@ function createFrameNavigatedHandler(state, timeoutState, reject, cleanup, { opt
670
672
  }
671
673
 
672
674
  async function getSingleFileContext({ Page, Runtime }, topFrameId, { options, debugMessages }) {
673
- const SINGLE_FILE_DETECTION_TEST = "typeof singlefile !== 'undefined'";
675
+ const SINGLE_FILE_DETECTION_TEST = "typeof singlefile !== 'undefined' && globalThis." + SCRIPT_READY_PROPERTY_NAME + " === true";
674
676
  const NO_VALID_CONTEXT_ERROR_MESSAGE = "No valid SingleFile execution context found";
675
677
  logData(["Getting execution context"], { options, debugMessages });
676
678
  // the world already exists, so asking for it by name returns the context the
@@ -687,11 +689,19 @@ async function getSingleFileContext({ Page, Runtime }, topFrameId, { options, de
687
689
  contextId: executionContextId
688
690
  });
689
691
  if (result.value !== true) {
690
- throw new Error(NO_VALID_CONTEXT_ERROR_MESSAGE);
692
+ throw new Error(NO_VALID_CONTEXT_ERROR_MESSAGE + await getInjectionErrorSuffix(Runtime, executionContextId));
691
693
  }
692
694
  return executionContextId;
693
695
  }
694
696
 
697
+ async function getInjectionErrorSuffix(Runtime, executionContextId) {
698
+ const { result } = await Runtime.evaluate({
699
+ expression: "globalThis." + SCRIPT_ERROR_PROPERTY_NAME,
700
+ contextId: executionContextId
701
+ }).catch(() => ({}));
702
+ return result && result.value ? ": " + result.value : "";
703
+ }
704
+
695
705
  function setupPageDataCapture({ Runtime }, { options, debugMessages }) {
696
706
  return new Promise((resolve, reject) => {
697
707
  let pageDataResponse = "";