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
+10 -3
View File
@@ -67,17 +67,24 @@ async function image(body, dir) {
const page = await context.newPage();
page.setDefaultTimeout(20_000);
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.
const login = page.getByRole("button", { name: /^(Log in|เข้าสู่ระบบ)$/i }).first();
if (await page.getByTestId("login-button").isVisible() || await login.isVisible()) {
throw new Error("ChatGPT login required: run node scripts/chatgpt-login.mjs");
}
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.press("Enter");
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 });
await downloadButton.click();
const download = await downloadEvent;
@@ -120,7 +127,7 @@ const server = createServer(async (req, res) => {
else {
const message = String(error?.message || "");
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"
: /image.*(?:download|export|visible)|download.*finish/i.test(message) ? "IMAGE_EXPORT_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();
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));