import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { doctor, openSession } from './browser-session.mjs';
function parse(argv) {
const result = { command: argv[0] ?? 'help' };
for (let i = 1; i < argv.length; i++) {
const key = argv[i];
if (key === '--headless') { result.headless = true; continue; }
if (!key.startsWith('--') || !argv[i + 1] || argv[i + 1].startsWith('--')) throw new Error(`Expected a value after ${key}`);
result[key.slice(2)] = argv[++i];
}
return result;
}
const usage = `Browser debugger (Node.js 22+)
node scripts/browser.mjs doctor
node scripts/browser.mjs capture --url https://example.com --seconds 60 --out C:/path/work/capture
node scripts/browser.mjs capture --endpoint http://127.0.0.1:9222 --seconds 60
node scripts/browser.mjs run --script C:/path/work/task.mjs --out C:/path/work/capture
Options: --browser edge|chrome|chromium, --headless, --profile ABS_PATH,
--work ABS_PATH, --executable ABS_PATH, --max-body-bytes NUMBER.
run scripts export a default async function({ session, page, context, outputDir }).
The capture starts before --url navigation. Ctrl+C stops and flushes the capture.
The launched dedicated browser closes on completion; an attached existing browser stays open.`;
async function main() {
const args = parse(process.argv.slice(2));
if (args.command === 'help') { console.log(usage); return; }
if (args.command === 'doctor') {
const status = await doctor();
console.log(JSON.stringify(status, null, 2));
if (!status.ready) process.exitCode = 1;
return;
}
if (!['capture', 'run'].includes(args.command)) throw new Error(usage);
const allowed = new Set(['command', 'url', 'seconds', 'out', 'endpoint', 'script', 'browser', 'headless', 'profile', 'work', 'executable', 'max-body-bytes']);
for (const key of Object.keys(args)) if (!allowed.has(key)) throw new Error(`Unknown option: --${key}`);
const seconds = Number(args.seconds ?? 60);
if (!Number.isFinite(seconds) || seconds < 0) throw new Error('--seconds must be nonnegative.');
if (args.command === 'run' && !args.script) throw new Error('run requires --script ABS_PATH.');
const session = await openSession({
endpoint: args.endpoint, browser: args.browser, headless: args.headless,
profileDir: args.profile, workDir: args.work, outputDir: args.out,
executablePath: args.executable,
...(args['max-body-bytes'] !== undefined ? { maxBodyBytes: Number(args['max-body-bytes']) } : {}),
});
console.log(JSON.stringify({ ready: true, outputDir: session.outputDir, endpoint: session.endpoint, captureStartedAt: session.recorder.startedAt }));
let interrupted = false;
let wake;
const signal = () => { interrupted = true; wake?.(); };
process.on('SIGINT', signal);
process.on('SIGTERM', signal);
try {
if (args.url) await session.page.goto(args.url, { waitUntil: 'domcontentloaded' });
if (args.command === 'run') {
const task = await import(pathToFileURL(path.resolve(args.script)).href);
if (typeof task.default !== 'function') throw new Error('Task module must export a default async function.');
await task.default({ session, page: session.page, context: session.context, outputDir: session.outputDir });
} else if (!interrupted) {
await new Promise(resolve => {
const timer = setTimeout(resolve, seconds * 1000);
wake = () => { clearTimeout(timer); resolve(); };
});
}
} finally {
const summary = await session.close();
process.off('SIGINT', signal);
process.off('SIGTERM', signal);
console.log(JSON.stringify({ saved: session.outputDir, requestCount: summary.requestCount, warnings: summary.warnings.length }));
}
}
main().catch(error => { console.error(error.stack ?? error); process.exitCode = 1; });
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { doctor, openSession } from './browser-session.mjs';
function parse(argv) {
const result = { command: argv[0] ?? 'help' };
for (let i = 1; i < argv.length; i++) {
const key = argv[i];
if (key === '--headless') { result.headless = true; continue; }
if (!key.startsWith('--') || !argv[i + 1] || argv[i + 1].startsWith('--')) throw new Error(`Expected a value after ${key}`);
result[key.slice(2)] = argv[++i];
}
return result;
}
const usage = `Browser debugger (Node.js 22+)
node scripts/browser.mjs doctor
node scripts/browser.mjs capture --url https://example.com --seconds 60 --out C:/path/work/capture
node scripts/browser.mjs capture --endpoint http://127.0.0.1:9222 --seconds 60
node scripts/browser.mjs run --script C:/path/work/task.mjs --out C:/path/work/capture
Options: --browser edge|chrome|chromium, --headless, --profile ABS_PATH,
--work ABS_PATH, --executable ABS_PATH, --max-body-bytes NUMBER.
run scripts export a default async function({ session, page, context, outputDir }).
The capture starts before --url navigation. Ctrl+C stops and flushes the capture.
The launched dedicated browser closes on completion; an attached existing browser stays open.`;
async function main() {
const args = parse(process.argv.slice(2));
if (args.command === 'help') { console.log(usage); return; }
if (args.command === 'doctor') {
const status = await doctor();
console.log(JSON.stringify(status, null, 2));
if (!status.ready) process.exitCode = 1;
return;
}
if (!['capture', 'run'].includes(args.command)) throw new Error(usage);
const allowed = new Set(['command', 'url', 'seconds', 'out', 'endpoint', 'script', 'browser', 'headless', 'profile', 'work', 'executable', 'max-body-bytes']);
for (const key of Object.keys(args)) if (!allowed.has(key)) throw new Error(`Unknown option: --${key}`);
const seconds = Number(args.seconds ?? 60);
if (!Number.isFinite(seconds) || seconds < 0) throw new Error('--seconds must be nonnegative.');
if (args.command === 'run' && !args.script) throw new Error('run requires --script ABS_PATH.');
const session = await openSession({
endpoint: args.endpoint, browser: args.browser, headless: args.headless,
profileDir: args.profile, workDir: args.work, outputDir: args.out,
executablePath: args.executable,
...(args['max-body-bytes'] !== undefined ? { maxBodyBytes: Number(args['max-body-bytes']) } : {}),
});
console.log(JSON.stringify({ ready: true, outputDir: session.outputDir, endpoint: session.endpoint, captureStartedAt: session.recorder.startedAt }));
let interrupted = false;
let wake;
const signal = () => { interrupted = true; wake?.(); };
process.on('SIGINT', signal);
process.on('SIGTERM', signal);
try {
if (args.url) await session.page.goto(args.url, { waitUntil: 'domcontentloaded' });
if (args.command === 'run') {
const task = await import(pathToFileURL(path.resolve(args.script)).href);
if (typeof task.default !== 'function') throw new Error('Task module must export a default async function.');
await task.default({ session, page: session.page, context: session.context, outputDir: session.outputDir });
} else if (!interrupted) {
await new Promise(resolve => {
const timer = setTimeout(resolve, seconds * 1000);
wake = () => { clearTimeout(timer); resolve(); };
});
}
} finally {
const summary = await session.close();
process.off('SIGINT', signal);
process.off('SIGTERM', signal);
console.log(JSON.stringify({ saved: session.outputDir, requestCount: summary.requestCount, warnings: summary.warnings.length }));
}
}
main().catch(error => { console.error(error.stack ?? error); process.exitCode = 1; });