Prepare dedicated Playwright image browser and login checks

This commit is contained in:
Loma Bot Ops
2026-11-23 22:36:22 +07:00
parent df20406cf5
commit ac4d3bf7a5
3 changed files with 14 additions and 7 deletions
+3 -3
View File
@@ -33,9 +33,9 @@ AI_AUTOMATION_HOST=127.0.0.1
AI_AUTOMATION_PORT=4318 AI_AUTOMATION_PORT=4318
CHATGPT_PROFILE_DIR=.data/chatgpt-profile CHATGPT_PROFILE_DIR=.data/chatgpt-profile
CHATGPT_BROWSER_CHANNEL=chrome CHATGPT_BROWSER_CHANNEL=chrome
# Windows: control an already-open normal Chrome via desktop accessibility. # Playwright controls a dedicated Chrome profile without foreground keyboard access.
# Other platforms: playwright uses the dedicated profile above. # Run node scripts/chatgpt-login.mjs interactively once, then close that browser.
CHATGPT_DRIVER=windows CHATGPT_DRIVER=playwright
CHATGPT_SURFACE=extension CHATGPT_SURFACE=extension
CHATGPT_IMAGE_EXPORT=download CHATGPT_IMAGE_EXPORT=download
# Durable full-post queue. Same secret in the web app and queue polling worker. # Durable full-post queue. Same secret in the web app and queue polling worker.
+10 -3
View File
@@ -67,17 +67,24 @@ async function image(body, dir) {
const page = await context.newPage(); const page = await context.newPage();
page.setDefaultTimeout(20_000); page.setDefaultTimeout(20_000);
await page.goto("https://chatgpt.com/", { waitUntil: "domcontentloaded" }); await page.goto("https://chatgpt.com/", { waitUntil: "domcontentloaded" });
if (await page.title() === "Just a moment..." || page.url().includes("__cf_chl_")) {
throw new Error("ChatGPT browser verification required: run node scripts/chatgpt-login.mjs and complete verification");
}
// Check login before waiting for the composer: the logged-out page has a different layout. // Check login before waiting for the composer: the logged-out page has a different layout.
const login = page.getByRole("button", { name: /^(Log in|เข้าสู่ระบบ)$/i }).first(); const login = page.getByRole("button", { name: /^(Log in|เข้าสู่ระบบ)$/i }).first();
if (await page.getByTestId("login-button").isVisible() || await login.isVisible()) { if (await page.getByTestId("login-button").isVisible() || await login.isVisible()) {
throw new Error("ChatGPT login required: run node scripts/chatgpt-login.mjs"); throw new Error("ChatGPT login required: run node scripts/chatgpt-login.mjs");
} }
const editor = page.locator('#prompt-textarea'); const editor = page.locator('#prompt-textarea');
await editor.waitFor({ state: "visible" }); await editor.waitFor({ state: "visible", timeout: 30_000 }).catch(() => {
throw new Error("ChatGPT composer unavailable: verify the dedicated browser profile");
});
await editor.fill(`Create an image now. Return the generated image, not a prompt. Aspect ratio: ${body.size === "landscape" ? "3:2" : "1:1"}.\n${body.prompt}`); await editor.fill(`Create an image now. Return the generated image, not a prompt. Aspect ratio: ${body.size === "landscape" ? "3:2" : "1:1"}.\n${body.prompt}`);
await editor.press("Enter"); await editor.press("Enter");
const downloadButton = page.locator('[data-message-author-role="assistant"]').getByRole("button", { name: /download|ดาวน์โหลด/i }).last(); const downloadButton = page.locator('[data-message-author-role="assistant"]').getByRole("button", { name: /download|ดาวน์โหลด/i }).last();
await downloadButton.waitFor({ state: "visible", timeout: 200_000 }); await downloadButton.waitFor({ state: "visible", timeout: 200_000 }).catch(() => {
throw new Error("ChatGPT image generation or download unavailable");
});
const downloadEvent = page.waitForEvent("download", { timeout: 20_000 }); const downloadEvent = page.waitForEvent("download", { timeout: 20_000 });
await downloadButton.click(); await downloadButton.click();
const download = await downloadEvent; const download = await downloadEvent;
@@ -120,7 +127,7 @@ const server = createServer(async (req, res) => {
else { else {
const message = String(error?.message || ""); const message = String(error?.message || "");
const code = /foreground focus|Open normal Chrome/i.test(message) ? "DESKTOP_UNAVAILABLE" const code = /foreground focus|Open normal Chrome/i.test(message) ? "DESKTOP_UNAVAILABLE"
: /login required/i.test(message) ? "LOGIN_REQUIRED" : /login required|verification required/i.test(message) ? "LOGIN_REQUIRED"
: /composer unavailable|side panel did not open|extension side panel first/i.test(message) ? "CHAT_UNAVAILABLE" : /composer unavailable|side panel did not open|extension side panel first/i.test(message) ? "CHAT_UNAVAILABLE"
: /image.*(?:download|export|visible)|download.*finish/i.test(message) ? "IMAGE_EXPORT_FAILED" : /image.*(?:download|export|visible)|download.*finish/i.test(message) ? "IMAGE_EXPORT_FAILED"
: "AUTOMATION_FAILED"; : "AUTOMATION_FAILED";
+1 -1
View File
@@ -8,5 +8,5 @@ const context = await chromium.launchPersistentContext(path.resolve(process.env.
}); });
const page = await context.newPage(); const page = await context.newPage();
await page.goto("https://chatgpt.com/"); await page.goto("https://chatgpt.com/");
console.log("Log in to ChatGPT, then close the browser. Start the worker afterward."); console.log("Complete browser verification and log in to ChatGPT in this dedicated Chrome window. Confirm that the chat composer is visible, then close this window. Start the worker afterward.");
await new Promise((resolve) => context.once("close", resolve)); await new Promise((resolve) => context.once("close", resolve));