Use real DevTools port for share debugging
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import { chromium } from "playwright";
|
import { chromium } from "playwright";
|
||||||
|
import { spawn } from "node:child_process";
|
||||||
|
import { mkdtemp, rm } from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
const sourceUrl = process.argv[2];
|
const sourceUrl = process.argv[2];
|
||||||
const holdMs = Number(process.env.DEBUG_HOLD_MS || "900000");
|
const holdMs = Number(process.env.DEBUG_HOLD_MS || "900000");
|
||||||
@@ -11,6 +15,8 @@ if (!sourceUrl) {
|
|||||||
|
|
||||||
let browser;
|
let browser;
|
||||||
let context;
|
let context;
|
||||||
|
let browserProcess;
|
||||||
|
let userDataDir;
|
||||||
|
|
||||||
function now() {
|
function now() {
|
||||||
return new Date().toISOString();
|
return new Date().toISOString();
|
||||||
@@ -19,19 +25,45 @@ function now() {
|
|||||||
async function close() {
|
async function close() {
|
||||||
if (context) await context.close().catch(() => undefined);
|
if (context) await context.close().catch(() => undefined);
|
||||||
if (browser) await browser.close().catch(() => undefined);
|
if (browser) await browser.close().catch(() => undefined);
|
||||||
|
if (browserProcess && !browserProcess.killed) browserProcess.kill("SIGTERM");
|
||||||
|
if (userDataDir) await rm(userDataDir, { recursive: true, force: true }).catch(() => undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on("SIGINT", () => close().finally(() => process.exit(130)));
|
process.on("SIGINT", () => close().finally(() => process.exit(130)));
|
||||||
process.on("SIGTERM", () => close().finally(() => process.exit(143)));
|
process.on("SIGTERM", () => close().finally(() => process.exit(143)));
|
||||||
|
|
||||||
browser = await chromium.launch({
|
userDataDir = await mkdtemp(path.join(os.tmpdir(), "aishare-debug-"));
|
||||||
headless: true,
|
const executablePath = chromium.executablePath();
|
||||||
args: [
|
browserProcess = spawn(executablePath, [
|
||||||
`--remote-debugging-address=0.0.0.0`,
|
"--headless",
|
||||||
`--remote-debugging-port=${port}`
|
"--no-sandbox",
|
||||||
]
|
"--disable-dev-shm-usage",
|
||||||
|
"--disable-gpu",
|
||||||
|
"--hide-scrollbars",
|
||||||
|
"--mute-audio",
|
||||||
|
"--window-size=1360,900",
|
||||||
|
"--remote-debugging-address=0.0.0.0",
|
||||||
|
`--remote-debugging-port=${port}`,
|
||||||
|
`--user-data-dir=${userDataDir}`,
|
||||||
|
"about:blank"
|
||||||
|
], {
|
||||||
|
stdio: ["ignore", "pipe", "pipe"]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
browserProcess.stdout.on("data", (data) => process.stdout.write(data));
|
||||||
|
browserProcess.stderr.on("data", (data) => process.stderr.write(data));
|
||||||
|
browserProcess.on("exit", (code, signal) => {
|
||||||
|
console.log(`[${now()}] Chromium exited code=${code} signal=${signal}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||||
|
const response = await fetch(`http://127.0.0.1:${port}/json/version`).catch(() => null);
|
||||||
|
if (response?.ok) break;
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 250));
|
||||||
|
}
|
||||||
|
|
||||||
|
browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`);
|
||||||
|
|
||||||
context = await browser.newContext({
|
context = await browser.newContext({
|
||||||
viewport: { width: 1360, height: 900 },
|
viewport: { width: 1360, height: 900 },
|
||||||
deviceScaleFactor: 1,
|
deviceScaleFactor: 1,
|
||||||
@@ -44,8 +76,11 @@ page.setDefaultTimeout(30_000);
|
|||||||
console.log(`[${now()}] Chromium DevTools port: ${port}`);
|
console.log(`[${now()}] Chromium DevTools port: ${port}`);
|
||||||
console.log(`[${now()}] Opening ${sourceUrl}`);
|
console.log(`[${now()}] Opening ${sourceUrl}`);
|
||||||
|
|
||||||
await page.goto(sourceUrl, { waitUntil: "domcontentloaded", timeout: 45_000 });
|
await page.goto(sourceUrl, { waitUntil: "commit", timeout: 45_000 }).catch((error) => {
|
||||||
await page.waitForLoadState("networkidle", { timeout: 20_000 }).catch(() => undefined);
|
console.log(`[${now()}] goto did not complete: ${error.message}`);
|
||||||
|
});
|
||||||
|
await page.waitForLoadState("domcontentloaded", { timeout: 20_000 }).catch(() => undefined);
|
||||||
|
await page.waitForLoadState("networkidle", { timeout: 10_000 }).catch(() => undefined);
|
||||||
await page.waitForTimeout(3000);
|
await page.waitForTimeout(3000);
|
||||||
|
|
||||||
const summary = await page.evaluate(() => {
|
const summary = await page.evaluate(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user