Bound ChatGPT DOM extraction time
This commit is contained in:
+15
-2
@@ -207,6 +207,19 @@ async function waitForCdp(port: number, process: ChildProcess): Promise<void> {
|
||||
throw new Error("Chromium CDP endpoint did not become ready.");
|
||||
}
|
||||
|
||||
async function withTimeout<T>(promise: Promise<T>, timeoutMs: number, label: string): Promise<T> {
|
||||
let timeout: NodeJS.Timeout | undefined;
|
||||
const timeoutPromise = new Promise<never>((_resolve, reject) => {
|
||||
timeout = setTimeout(() => reject(new Error(`${label} timed out after ${timeoutMs}ms.`)), timeoutMs);
|
||||
});
|
||||
try {
|
||||
return await Promise.race([promise, timeoutPromise]);
|
||||
} finally {
|
||||
if (timeout) clearTimeout(timeout);
|
||||
promise.catch(() => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async function stopProcess(process: ChildProcess): Promise<void> {
|
||||
if (process.exitCode !== null || process.signalCode !== null) return;
|
||||
process.kill("SIGTERM");
|
||||
@@ -274,7 +287,7 @@ async function extractConversation(sourceUrl: string): Promise<ExtractedConversa
|
||||
await page.waitForLoadState("networkidle", { timeout: 5_000 }).catch(() => undefined);
|
||||
await page.waitForTimeout(1000);
|
||||
|
||||
const extracted = await page.evaluate(() => {
|
||||
const extracted = await withTimeout(page.evaluate(() => {
|
||||
function cleanHtml(root: Element): string {
|
||||
const clone = root.cloneNode(true) as Element;
|
||||
clone.querySelectorAll("script, style, iframe, object, embed, form, button, textarea, input, nav").forEach((node) => node.remove());
|
||||
@@ -331,7 +344,7 @@ async function extractConversation(sourceUrl: string): Promise<ExtractedConversa
|
||||
}
|
||||
|
||||
return { title, messages };
|
||||
});
|
||||
}), config.captureTimeoutMs, "Extracting ChatGPT share DOM");
|
||||
|
||||
if (!extracted.messages.length) {
|
||||
throw new Error("Unsupported ChatGPT share page: no conversation messages were found.");
|
||||
|
||||
Reference in New Issue
Block a user