fix multi session
This commit is contained in:
@@ -6,3 +6,4 @@ test.js
|
|||||||
test.mjs
|
test.mjs
|
||||||
config.js
|
config.js
|
||||||
config.mjs
|
config.mjs
|
||||||
|
browser_profiles
|
||||||
+11
-2
@@ -1,5 +1,10 @@
|
|||||||
import { connect } from "puppeteer-real-browser";
|
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) {
|
async function initSessions(config) {
|
||||||
console.log(`本项目依赖Chrome浏览器,请勿关闭弹出的浏览器窗口。如果出现错误请检查是否已安装Chrome浏览器。`);
|
console.log(`本项目依赖Chrome浏览器,请勿关闭弹出的浏览器窗口。如果出现错误请检查是否已安装Chrome浏览器。`);
|
||||||
@@ -30,12 +35,16 @@ console.log(`已添加 ${Object.keys(sessions).length} 个有效cookie,开始
|
|||||||
|
|
||||||
for (var username of Object.keys(sessions)) {
|
for (var username of Object.keys(sessions)) {
|
||||||
var session = sessions[username];
|
var session = sessions[username];
|
||||||
|
createDirectoryIfNotExists(path.join(__dirname, "browser_profiles", username));
|
||||||
await connect({
|
await connect({
|
||||||
headless: 'auto',
|
headless: 'auto',
|
||||||
turnstile: true,
|
turnstile: true,
|
||||||
|
customConfig: {
|
||||||
|
//userDataDir: path.join(__dirname, "browser_profiles", username),
|
||||||
|
},
|
||||||
}).then(async (response) => {
|
}).then(async (response) => {
|
||||||
const { page, browser, setTarget } = 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 });
|
page.goto("https://you.com", { timeout: 60000 });
|
||||||
await sleep(5000); // 等待加载完毕
|
await sleep(5000); // 等待加载完毕
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
import cookie from "cookie";
|
import cookie from "cookie";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
|
||||||
|
function createDirectoryIfNotExists(dirPath) {
|
||||||
|
if (!fs.existsSync(dirPath)) {
|
||||||
|
fs.mkdirSync(dirPath, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function extractCookie(cookies) {
|
function extractCookie(cookies) {
|
||||||
var jwtSession = null;
|
var jwtSession = null;
|
||||||
@@ -62,4 +70,4 @@ function sleep(ms) {
|
|||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
export { sleep, extractCookie, getSessionCookie };
|
export { createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie };
|
||||||
|
|||||||
Reference in New Issue
Block a user