Prepare dedicated Playwright image browser and login checks
This commit is contained in:
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user