feat: 更新用户数据目录路径以避免与 Chrome 冲突

This commit is contained in:
2026-03-02 16:38:06 +08:00
parent 71eb6bea7f
commit 33112ea695
4 changed files with 9 additions and 11 deletions

View File

@@ -38,7 +38,7 @@ export const main = async (opts?: {
// Chrome 路径和配置 // Chrome 路径和配置
let executablePath = opts?.executablePath || getExecutablePath(); let executablePath = opts?.executablePath || getExecutablePath();
// 使用独立的用户数据目录,避免与 Chrome 冲突 // 使用独立的用户数据目录,避免与 Chrome 冲突
const userDataDir = opts?.userDataDir || path.join(process.cwd(), 'browser-context'); const userDataDir = opts?.userDataDir || path.join(process.cwd(), 'browser-context', 'chrome-user-data');
const debugPort = opts?.debugPort || 9223; const debugPort = opts?.debugPort || 9223;
const headless = opts?.headless || false; const headless = opts?.headless || false;

View File

@@ -3,7 +3,6 @@ import { execSync } from 'node:child_process';
import path from 'node:path'; import path from 'node:path';
import { EventEmitter } from 'eventemitter3' import { EventEmitter } from 'eventemitter3'
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
import { main } from "./browser.ts";
import { stealthMode } from './stealth/index.ts'; import { stealthMode } from './stealth/index.ts';
type RequestObject = { type RequestObject = {
url: string; url: string;
@@ -55,9 +54,6 @@ export class Core<T = {}> {
} }
this.useCDPConnect = opts?.useCDPConnect || true; this.useCDPConnect = opts?.useCDPConnect || true;
} }
async createBrowser() {
const chrome = await main({ debugPort: this.debugPort, headless: this.headless });
}
async init() { async init() {
const debugPort = this.debugPort; const debugPort = this.debugPort;
try { try {
@@ -100,9 +96,8 @@ export class Core<T = {}> {
this.status = 'connecting'; this.status = 'connecting';
for (let i = 0; i < 6; i++) { for (let i = 0; i < 6; i++) {
if (i === 3) { if (i === 3) {
console.log('尝试启动浏览器实例...'); console.log('启动浏览器实例...');
await this.createBrowser(); break;
await sleep(5000);
} }
try { try {
await this.init(); await this.init();

View File

@@ -4,7 +4,7 @@ import path from 'node:path';
const checkUrl = 'https://pg.zwpyyds.com/pindou' const checkUrl = 'https://pg.zwpyyds.com/pindou'
const userDataDir = path.join(process.cwd(), 'browser-data-zwpy'); const userDataDir = path.join(process.cwd(), 'browser-data-zwpy');
// const chromeProcess = await main({ // const chromeProcess = await main({
// userDataDir: path.join(process.cwd(), 'browser-data-zwpy'), // userDataDir: path.join(process.cwd(), 'browser-context', 'browser-data-zwpy'),
// debugPort: 9223, // debugPort: 9223,
// }); // });

View File

@@ -1,3 +1,6 @@
import { main } from "./src/playwright/browser.ts"; import { main } from './src/playwright/browser.ts';
await main() import path from 'path';
await main({
userDataDir: path.join(process.cwd(), 'browser-context', 'xhs-browser'),
});