import { chromium } from "playwright";
const base = process.env.FLUX_API_BASE ?? "http://localhost:8026/api/v1";
const apiKey = process.env.FLUX_API_KEY!;
const envId = process.env.FLUX_ENV_ID!;
async function launchAndConnect() {
const launchRes = await fetch(`${base}/open/browser/launch`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ apiKey, envIds: [envId] }),
});
const launchJson = await launchRes.json();
if (!launchJson.succeed) throw new Error(launchJson.msg ?? "launch failed");
const item = launchJson.data?.results?.[0];
if (!item?.success || !item.webSocketDebuggerUrl) {
throw new Error(item?.error ?? "no webSocketDebuggerUrl");
}
const browser = await chromium.connectOverCDP(item.webSocketDebuggerUrl);
const context = browser.contexts()[0] ?? (await browser.newContext());
const page = context.pages()[0] ?? (await context.newPage());
await page.goto("https://example.com");
// …你的自动化逻辑
await browser.close();
}
launchAndConnect().catch(console.error);