Skip to content

Commit

Permalink
add --browser-capture-max-time option (see #135)
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas-lormeau committed Nov 12, 2024
1 parent 02512e2 commit b5c4b0c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
46 changes: 30 additions & 16 deletions lib/cdp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { getScriptSource, getHookScriptSource } from "./single-file-script.js";
import { CDP, options } from "simple-cdp";

const LOAD_TIMEOUT_ERROR = "ERR_LOAD_TIMEOUT";
const CAPTURE_TIMEOUT_ERROR = "ERR_CAPTURE_TIMEOUT";
const NETWORK_STATES = ["InteractiveTime", "networkIdle", "networkAlmostIdle", "load", "DOMContentLoaded"];
const MINIMIZED_WINDOW_STATE = "minimized";
const SINGLE_FILE_WORLD_NAME = "singlefile";
Expand Down Expand Up @@ -319,18 +320,31 @@ async function getPageData(options) {
}
await new Promise(resolve => setTimeout(resolve, options.browserWaitDelay));
}
if (options.debugMessagesFile) {
debugMessages.push([Date.now(), ["Capturing page"]]);
}
const { result } = await Runtime.evaluate({
expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PDF_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME, CAPTURE_SCREENSHOT_FUNCTION_NAME, PRINT_TO_PDF_FUNCTION_NAME])})`,
awaitPromise: true,
returnByValue: true,
contextId
});
const { subtype, description } = result;
if (subtype === "error") {
throw new Error(description);
const captureTimeoutAbortController = new AbortController();
const captureTimeoutAbortSignal = captureTimeoutAbortController.signal;
try {
if (options.debugMessagesFile) {
debugMessages.push([Date.now(), ["Capturing page"]]);
}
const { result } = await Promise.race([
Runtime.evaluate({
expression: `(${getPageDataScriptSource.toString()})(${JSON.stringify(options)},${JSON.stringify([SET_SCREENSHOT_FUNCTION_NAME, SET_PDF_FUNCTION_NAME, SET_PAGE_DATA_FUNCTION_NAME, CAPTURE_SCREENSHOT_FUNCTION_NAME, PRINT_TO_PDF_FUNCTION_NAME])})`,
awaitPromise: true,
returnByValue: true,
contextId
}),
waitForTimeout(captureTimeoutAbortSignal, options.browserCaptureMaxTime, "Capture timeout", CAPTURE_TIMEOUT_ERROR)
]);
const { subtype, description } = result;
if (subtype === "error") {
throw new Error(description);
}
} finally {
if (!captureTimeoutAbortSignal.aborted) {
captureTimeoutAbortController.abort();
}
await Runtime.disable();
await Page.disable();
}
const pageData = await pageDataPromise;
if (options.consoleMessagesFile) {
Expand Down Expand Up @@ -429,7 +443,7 @@ async function loadPage({ Page, Runtime }, options, debugMessages) {
}
const [contextId] = await Promise.race([
Promise.all([getTopFrameContextId({ Page, Runtime }, options, debugMessages), Page.navigate({ url: options.url })]),
waitForLoadTimeout(loadTimeoutAbortSignal, options.browserLoadMaxTime)
waitForTimeout(loadTimeoutAbortSignal, options.browserLoadMaxTime, "Load timeout", LOAD_TIMEOUT_ERROR)
]);
return contextId;
} finally {
Expand Down Expand Up @@ -555,14 +569,14 @@ async function getTopFrameContextId({ Page, Runtime }, options, debugMessages) {
}
}

function waitForLoadTimeout(abortSignal, maxDelay) {
function waitForTimeout(abortSignal, maxDelay, errorMessage, errorCode) {
return new Promise((resolve, reject) => {
const ABORT_EVENT = "abort";
abortSignal.addEventListener(ABORT_EVENT, onAbort);
const timeoutId = setTimeout(() => {
abortSignal.removeEventListener(ABORT_EVENT, onAbort);
const error = new Error("Load timeout");
error.code = LOAD_TIMEOUT_ERROR;
const error = new Error(errorMessage);
error.code = errorCode;
reject(error);
}, maxDelay);

Expand Down
3 changes: 2 additions & 1 deletion options.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ const OPTIONS_INFO = {
"browser-executable-path": { description: "Path to chrome/chromium executable", type: "string" },
"browser-width": { description: "Width of the browser viewport in pixels", type: "number", defaultValue: 1280 },
"browser-height": { description: "Height of the browser viewport in pixels", type: "number", defaultValue: 720 },
"browser-load-max-time": { description: "Maximum delay of time to wait for page loading in ms", type: "number", defaultValue: 60000 },
"browser-load-max-time": { description: "Maximum delay of time to wait for loading the page in ms", type: "number", defaultValue: 60000 },
"browser-capture-max-time": { description: "Maximum delay of time to wait for capturing the page in ms", type: "number", defaultValue: 60000 },
"browser-wait-delay": { description: "Time to wait before capturing the page in ms", type: "number" },
"browser-wait-until": { description: "When to consider the page is loaded (InteractiveTime, networkIdle, networkAlmostIdle, load, domContentLoaded)", type: "string", defaultValue: "networkIdle" },
"browser-wait-until-delay": { description: "Delay of time in ms to wait before considering the page is loaded when the value of --browser-wait-until is reached", type: "number", defaultValue: 1000 },
Expand Down

0 comments on commit b5c4b0c

Please sign in to comment.