diff --git a/.gitignore b/.gitignore index 21b1b76..11edd86 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ config test.js test.mjs config.js -config.mjs \ No newline at end of file +config.mjs +browser_profiles \ No newline at end of file diff --git a/provider.mjs b/provider.mjs index ccc1675..587aa92 100644 --- a/provider.mjs +++ b/provider.mjs @@ -1,5 +1,10 @@ import { connect } from "puppeteer-real-browser"; -import { sleep, extractCookie, getSessionCookie } from "./utils.mjs"; +import path from "path"; +import { fileURLToPath } from 'url'; +import { createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie } from "./utils.mjs"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); async function initSessions(config) { console.log(`本项目依赖Chrome浏览器,请勿关闭弹出的浏览器窗口。如果出现错误请检查是否已安装Chrome浏览器。`); @@ -30,12 +35,16 @@ console.log(`已添加 ${Object.keys(sessions).length} 个有效cookie,开始 for (var username of Object.keys(sessions)) { var session = sessions[username]; + createDirectoryIfNotExists(path.join(__dirname, "browser_profiles", username)); await connect({ headless: 'auto', turnstile: true, + customConfig: { + //userDataDir: path.join(__dirname, "browser_profiles", username), + }, }).then(async (response) => { const { page, browser, setTarget } = response; - await page.setCookie(...getSessionCookie(jwtSession, jwtToken)); + await page.setCookie(...getSessionCookie(session.jwtSession, session.jwtToken)); page.goto("https://you.com", { timeout: 60000 }); await sleep(5000); // 等待加载完毕 diff --git a/utils.mjs b/utils.mjs index c447325..cfc4084 100644 --- a/utils.mjs +++ b/utils.mjs @@ -1,4 +1,12 @@ import cookie from "cookie"; +import fs from "fs"; + + +function createDirectoryIfNotExists(dirPath) { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath, { recursive: true }); + } +} function extractCookie(cookies) { var jwtSession = null; @@ -62,4 +70,4 @@ function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } -export { sleep, extractCookie, getSessionCookie }; +export { createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie };