Use dedicated Chrome profile for image automation
This commit is contained in:
@@ -11,6 +11,7 @@ const eslintConfig = defineConfig([
|
|||||||
".next/**",
|
".next/**",
|
||||||
"out/**",
|
"out/**",
|
||||||
"build/**",
|
"build/**",
|
||||||
|
".data/**",
|
||||||
"next-env.d.ts",
|
"next-env.d.ts",
|
||||||
]),
|
]),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -75,16 +75,19 @@ async function image(body, dir) {
|
|||||||
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, [role="textbox"][contenteditable="true"]').first();
|
||||||
await editor.waitFor({ state: "visible", timeout: 30_000 }).catch(() => {
|
await editor.waitFor({ state: "visible", timeout: 45_000 }).catch(() => {
|
||||||
throw new Error("ChatGPT composer unavailable: verify the dedicated browser profile");
|
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 generatedImage = page.getByTestId("generated-image-preview").last();
|
||||||
await downloadButton.waitFor({ state: "visible", timeout: 200_000 }).catch(() => {
|
await generatedImage.waitFor({ state: "visible", timeout: 200_000 }).catch(() => {
|
||||||
throw new Error("ChatGPT image generation or download unavailable");
|
throw new Error("ChatGPT image generation or download unavailable");
|
||||||
});
|
});
|
||||||
|
await generatedImage.click();
|
||||||
|
const downloadButton = page.getByRole("button", { name: /^(download|ดาวน์โหลด)$/i }).last();
|
||||||
|
await downloadButton.waitFor({ state: "visible", timeout: 20_000 });
|
||||||
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;
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { chromium } from "playwright";
|
import { spawn } from "node:child_process";
|
||||||
|
import { existsSync } from "node:fs";
|
||||||
|
|
||||||
// Run interactively before starting the PM2 worker. Never share your everyday Chrome profile.
|
// Log in using ordinary Chrome before Playwright opens this dedicated profile.
|
||||||
const context = await chromium.launchPersistentContext(path.resolve(process.env.CHATGPT_PROFILE_DIR || ".data/chatgpt-profile"), {
|
const profile = path.resolve(process.env.CHATGPT_PROFILE_DIR || ".data/chatgpt-profile");
|
||||||
channel: process.env.CHATGPT_BROWSER_CHANNEL || "chrome", headless: false,
|
const chrome = process.env.CHATGPT_CHROME_BIN || (process.platform === "win32"
|
||||||
|
? ["C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"].find(existsSync)
|
||||||
|
: "google-chrome");
|
||||||
|
if (!chrome) throw new Error("Chrome executable not found; set CHATGPT_CHROME_BIN");
|
||||||
|
console.log("Log in and finish verification in this dedicated Chrome window. Confirm the chat composer is visible, then close Chrome before restarting the worker.");
|
||||||
|
const child = spawn(chrome, [`--user-data-dir=${profile}`, "--no-first-run", "https://chatgpt.com/"], { stdio: "inherit", windowsHide: false });
|
||||||
|
await new Promise((resolve, reject) => {
|
||||||
|
child.once("error", reject);
|
||||||
|
child.once("exit", (code) => code === 0 ? resolve() : reject(new Error(`Chrome exited with code ${code}`)));
|
||||||
});
|
});
|
||||||
const page = await context.newPage();
|
|
||||||
await page.goto("https://chatgpt.com/");
|
|
||||||
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));
|
|
||||||
|
|||||||
Reference in New Issue
Block a user