// 복사한 실제 Chrome 프로필에서 세션 쿠키를 추출해 storageState로 저장한다. // Playwright E2E에서만 쓰는 임시 스크립트다. import { chromium } from "@playwright/test"; import fs from "node:fs"; const profile = process.argv[2]; const outFile = process.argv[3]; const headed = process.env.HEADLESS === "0"; const context = await chromium.launchPersistentContext(profile, { channel: "chrome", headless: !headed, viewport: headed ? { width: 1280, height: 800 } : undefined, ignoreDefaultArgs: ["--enable-automation"], args: ["--disable-blink-features=AutomationControlled"], }); async function findSessionCookie() { const cookies = await context.cookies("https://api-vignette.chanpaca.net"); return cookies.find((c) => c.name === "__Host-vignette_sid") ?? null; } let cookie = await findSessionCookie(); if (!cookie) { // 로그인 페이지를 한 번 열어 세션을 촉진하고(이미 로그인돼 있으면 자동 인증), // 쿠키가 여전히 없으면 headed 로그인 대기로 넘어간다. const page = context.pages()[0] ?? (await context.newPage()); await page.goto("https://vignette.chanpaca.net/login", { waitUntil: "domcontentloaded" }); await page.waitForTimeout(4000); cookie = await findSessionCookie(); } if (cookie && process.env.WAIT_LOGIN === "1") { // 세션이 있어도 headed 모드에서는 로그인 상태 페이지에 접속해 쿠키를 갱신한다. const page = context.pages()[0] ?? (await context.newPage()); await page.goto("https://vignette.chanpaca.net/", { waitUntil: "domcontentloaded" }); await page.waitForTimeout(3000); cookie = await findSessionCookie(); } if (!cookie) { console.log("NO_SESSION_COOKIE"); if (!headed) { await context.close(); process.exit(2); } // headed 모드: 사용자가 로그인을 마칠 때까지 최대 10분 대기한다. const page = context.pages()[0]; await page.goto("https://vignette.chanpaca.net/login", { waitUntil: "domcontentloaded" }); for (let i = 0; i < 120; i += 1) { await page.waitForTimeout(5000); if (await findSessionCookie()) break; } cookie = await findSessionCookie(); if (!cookie) { console.log("LOGIN_TIMEOUT"); await context.close(); process.exit(3); } } const allCookies = await context.cookies(); fs.writeFileSync( outFile, JSON.stringify({ cookies: allCookies, origins: [] }, null, 2), ); console.log(`SAVED ${outFile} cookies=${allCookies.length}`); await context.close(); process.exit(0);